You're asking to match "request" elements in the null namespace, but the closest your XML contains are "request" elements in the "http://www.somedomain.tld/market_reg/admin_server/1.0" namespace.
What you do is create is a prefix, associate it with that namespace, and use that prefix in the XPath.
use strict; use warnings; use XML::LibXML qw( ); use XML::LibXML::XPathContext qw( ); ( my $file = $0 ) =~ s/\.pl\z/.xml/i; my $parser = XML::LibXML->new (); my $dom = $parser->load_xml( location => $file ); my $root = $dom->getDocumentElement(); my $xpc = XML::LibXML::XPathContext->new($root); $xpc->registerNs( p => 'http://www.somedomain.tld/market_reg/admin_server/1.0' ); for my $node ( $xpc->findnodes('//p:request/*/p:action') ) { print $node->textContent(), "\n"; }
START STOP GET_INFO
Note that "p" is arbitrary. You can use something more meaningful. Or not.
By the way, you never have to call setContextNode. You can simply pass the context node as the second argument to find* as the following demonstrates:
use strict; use warnings; use XML::LibXML qw( ); use XML::LibXML::XPathContext qw( ); ( my $file = $0 ) =~ s/\.pl\z/.xml/i; my $parser = XML::LibXML->new (); my $dom = $parser->load_xml( location => $file ); my $root = $dom->getDocumentElement(); my $xpc = XML::LibXML::XPathContext->new($root); $xpc->registerNs( p => 'http://www.somedomain.tld/market_reg/admin_server/1.0' ); for my $req_node ( $xpc->findnodes('//p:request') ) { for my $action_node ( $xpc->findnodes('*/p:action', $req_node) ) { print $action_node->textContent(), "\n"; } }
In reply to Re: XML::LibXML - parsing question!!
by ikegami
in thread XML::LibXML - parsing question!!
by MarkovChain
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |