Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by ikegami (Patriarch)
on Sep 03, 2011 at 15:39 UTC ( [id://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="http://www.wow.com/BlahML" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocati +on="http://www.wow.com/BlahML 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 => 'http://www.wow.com/BlahML'); 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://924001]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-23 19:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found