I'm using XML::LibXML::Reader to parse a large document and have run into an issue whereby the attribute xmlns causes findnodes() to fail. I fixed it by added a regex to remove the xmls attribute but I was wondering if there was a more elegant solution involving no regexes. If you remove the regex line ($xml =~ s{xmlns...) you'll see that say "Loc = $loc" produces no results.
Here's the code:
use strict;
use warnings;
use feature qw( say );
use XML::LibXML::Reader qw( XML_READER_TYPE_ELEMENT );
my $xml = <<'__EOI__';
<url xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<loc>http://example.com</loc>
<lastmod>2018-10-19</lastmod>
</url>
__EOI__
$xml =~ s{xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"}{};
my $reader = XML::LibXML::Reader->new( string => $xml);
while ( $reader->read ) {
next unless $reader->nodeType == XML_READER_TYPE_ELEMENT;
next unless $reader->name eq 'url';
my $xml = $reader->readOuterXml;
my $doc = XML::LibXML->load_xml(string => $xml);
say "Doc = $doc";
my ($loc) = $doc->findnodes('//loc');
say "Loc = $loc";
}