<update nr="4"> For the sake of completeness, here's a working script with the changes mentioned below:
use warnings;
use strict;
use XML::LibXML;
my $doc = XML::LibXML->load_xml( location => 'example.xhtml',
no_network=>1, recover=>1 );
my $xpc = XML::LibXML::XPathContext->new($doc);
$xpc->registerNs('html', 'http://www.w3.org/1999/xhtml');
my @ids2text = map { [ $_->getAttribute('id'), $_->textContent ] }
$xpc->findnodes(q{//html:div[@class='data']});
$_->[1] =~ s/\W+//g for @ids2text;
print join ", ", map sprintf("%s=%s", @$_), @ids2text;
</update>
Thanks very much for the reply! Your post inspired some more test cases for my file, and I'm sorry to say I broke your code :-( But here's the fix:
my @ids2text = map { [ $_->getAttribute('id'), $_->textContent ] }
$doc->findnodes(q{//div[@class='data']});
Update: And yes, it does seem that load_html doesn't like XHTML - load_xml seems to work a bit better, although fetching the DTD from the net is pretty slow at the moment; adding the options {no_network=>1,recover=>1} disables the network check. However, with load_xml one also has to start using XML::LibXML::XPathContext:
my $xpc = XML::LibXML::XPathContext->new($doc);
$xpc->registerNs('html', 'http://www.w3.org/1999/xhtml');
my @ids2text = map { [ $_->getAttribute('id'), $_->textContent ] }
$xpc->findnodes(q{//html:div[@class='data']});
Update 2: Even with network, XML::LibXML is still complaining about ("Entity 'nbsp' not defined"), I'm not entirely sure why yet, as it seems to be defined in the DTD... Update 3: The W3C Validator doesn't complain...
|