Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: LibXML, Namespaces, xpath expression - This should be simple.

by ikegami (Pope)
on Sep 03, 2011 at 15:39 UTC ( #924001=note: print w/replies, xml ) Need Help??

in reply to LibXML, Namespaces, xpath expression - This should be simple.

First, there's an error in the XML.

<MyImportantNode> xsi:type="foo" GeneralID="Random1" >
should be
<MyImportantNode xsi:type="foo" GeneralID="Random1" >

(Same goes for the other two.)

4 of the 5 paths you tried in the loop start with "/". That means they start looking at the root of the tree. That instantly makes them wrong since you obviously want something that's in or below $nodeybits.

Another major problem is that you look for the nodes in the wrong namespace throughout the body of the loop. You don't even use the xpc!

<?xml version="1.0" encoding="UTF-8"?> <RootTag SchemaVersion="1.1" xmlns="" xmlns:xsi="" xsi:schemaLocati +on=" BlahML1_1.xsd"> <MyTagA Tag="XXX">Some Text A</MyTagA> <MyTagB Tag="XXX">Some Text A</MyTagB> <MiddleTag xsi:type="foo" magicvalue="wow"> <MyImportantNode xsi:type="foo" GeneralID="Random1" > <CannotGetTagA>YYYYYYYYYYY</CannotGetTagA> <CannotGetTagB>ZZZZZZZZZZZ</CannotGetTagB> </MyImportantNode> <MyImportantNode xsi:type="foo" GeneralID="Random2" > <CannotGetTagA>YYYYYYYYYY22</CannotGetTagA> <CannotGetTagB>ZZZZZZZZZZ22</CannotGetTagB> </MyImportantNode> <MyImportantNode xsi:type="foo" GeneralID="Random3" > <CannotGetTagA>YYYYYYYYY333</CannotGetTagA> <CannotGetTagB>ZZZZZZZZZ333</CannotGetTagB> </MyImportantNode> </MiddleTag> </RootTag>
use strict; use warnings; use feature qw( say ); use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file('test.xml'); my $xpc = XML::LibXML::XPathContext->new($doc); $xpc->registerNs(theNS => ''); for my $important_node ( $xpc->findnodes('//theNS:MiddleTag/theNS:MyImportantNode') ) { say $important_node->getAttribute('GeneralID'); for my $cannot_get_a_node ( $xpc->findnodes('theNS:CannotGetTagA', $important_node) ) { say $cannot_get_a_node->textContent(); } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://924001]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2018-06-18 02:39 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.