XML::LibXML: How to write a xpath with qualified name?
- by sid_com
I've found this on http://www.perlmonks.org/?node_id=606909
looking by qualified name ...
In this case you can call findnodes method on any node, you don't need the XML::LibXML::XPathContext with its prefix = namespace mapping:
$doc-findnodes('///info/fooTransaction/transactionDetail/[name() = "histFile:transactionSummary"]/*');
In which way I have to edit my xpath to get my script working without XPathContext?
#!/usr/bin/env perl
use warnings; use strict;
use 5.012;
use XML::LibXML;
my $parser = XML::LibXML->new;
$parser->recover_silently( 1 );
my $doc = $parser->parse_file( 'http://www.heise.de/' );
my $xc = XML::LibXML::XPathContext->new( $doc->getDocumentElement );
$xc->registerNs( 'xmlns', 'http://www.w3.org/1999/xhtml' );
my $nodes = $xc->findnodes( '//xmlns:h2/xmlns:a' );
for my $node ( $nodes->get_nodelist ) {
say $_->getName, '=', $_->getValue for $node->attributes;
}