in reply to XML::LibXML - parsing question!!
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"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML::LibXML - parsing question!!
by MarkovChain (Sexton) on Dec 19, 2009 at 05:37 UTC | |
by ikegami (Patriarch) on Dec 19, 2009 at 07:30 UTC | |
by MarkovChain (Sexton) on Dec 19, 2009 at 15:40 UTC | |
by ikegami (Patriarch) on Dec 19, 2009 at 17:34 UTC | |
by MarkovChain (Sexton) on Dec 19, 2009 at 16:06 UTC | |
by ikegami (Patriarch) on Dec 19, 2009 at 17:43 UTC | |
by MarkovChain (Sexton) on Dec 19, 2009 at 18:45 UTC | |
by ikegami (Patriarch) on Dec 19, 2009 at 19:50 UTC | |
| |
by ikegami (Patriarch) on Dec 19, 2009 at 19:43 UTC |