XML::LibXML: How to write a xpath with qualified name?

Posted by sid_com on Stack Overflow See other posts from Stack Overflow or by sid_com
Published on 2010-04-21T08:26:18Z Indexed on 2010/04/21 8:33 UTC
Read the original article Hit count: 441

Filed under:
|
|

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;
}

© Stack Overflow or respective owner

Related posts about perl

Related posts about Xml