in reply to Re^4: problem HTML::FormatText::WithLinks::AndTables
in thread problem HTML::FormatText::WithLinks::AndTables

Dumper is not especially useful for inspecting XML::LibXML's objects. You see, XML::LibXML is a wrapper for a C library (libxml2) and the real guts of the objects live within the C library.

This has frustrated me in the past too. The HTML5 spec differentiates between two kinds of xml:lang attributes (attributes called lang in the xml namespace, versus attributes called xml:lang in no namespace!) and toString doesn't distinguish between those. So this was tricky to debug when working on HTML::HTML5::Parser.

I wrote XML::LibXML::Debugging as a solution, though I rarely use it these days, and don't give it much attention maintenance-wise. The following example gives you a big Perlish tree of nested hashes and arrays:

use strict; use warnings; use Data::Dumper; use HTML::HTML5::Parser; use XML::LibXML::Debugging; my $document = HTML::HTML5::Parser->load_html(IO => \*DATA); print Dumper( $document->toDebuggingHash ); __DATA__ <!doctype html> <title lang="en">Example</title> <table><tr><td xml:lang="en">Hello world</table>

However the best ways of "navigating" the XML tree are to use querySelector/querySelectorAll provided by XML::LibXML::QuerySelector (which allow you to choose elements using CSS selectors) or if you need something more powerful, using XML::LibXML's built in XPath support.

You don't always need an id attribute to select the data you want. For example, say you want to select the third <table> on a page, you could just do:

my @all_tables = $document->querySelectorAll('table'); my $wanted_table = $table[2];

Or to select the first <table> within <div class="foo">:

my @all_tables = $document->querySelectorAll('div.foo table'); my $wanted_table = $table[0];

Or, because querySelector returns the first match, this is the same:

my $wanted_table = $document->querySelector('div.foo table');
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name