Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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="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(); } }


Comment on Re: LibXML, Namespaces, xpath expression - This should be simple.
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2015-07-29 10:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls