Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: XML::XPath - node-to-xpath reverse lookup

by pajout (Curate)
on Nov 08, 2010 at 17:03 UTC ( #870153=note: print w/replies, xml ) Need Help??

in reply to XML::XPath - node-to-xpath reverse lookup

Do not beat me, this is my first experience with that module :>)

#!/usr/bin/perl use strict; use XML::XPath; my $xml = ' <status> <some_node/> <connector name="http-0"> <threadInfo currentThreadsBusy="0"/> <requestInfo bytesReceived="0" bytesSent="0" errorCount="0" maxTim +e="0" processingTime="0" requestCount="0"/> <workers></workers> </connector> <connector name="http-8080"> <threadInfo currentThreadCount="158" currentThreadsBusy="10" maxTh +reads="200"/> <requestInfo bytesReceived="297" bytesSent="19350704517" errorCoun +t="192504" maxTime="249349" processingTime="2242513592" requestCount= +"983650"/> <workers> </workers> </connector> </status> '; my $path = '/status/connector/threadInfo/@currentThreadsBusy'; my $xpath = XML::XPath->new( xml => $xml ); my $nodeset = $xpath->find($path); foreach my $node ($nodeset->get_nodelist) { print "FOUND: ", $xpath->getNodeText($node), "\n"; my $ret = '/@currentThreadsBusy'; my $parent = $node->getParentNode(); while ($parent and $parent->getParentNode()) { #$ret = '['.$xpath->find('position()',$parent)->value.']' . $ret; $ret = '['.($xpath->find('preceding-sibling::*[name()="'.$parent-> +getName().'"]',$parent)->size+1).']' . $ret; $ret = '/'.$parent->getName() . $ret; $parent = $parent->getParentNode(); } print $ret."\n"; }
I think that commented row which uses position() would work, but it does not...

Replies are listed 'Best First'.
Re^2: XML::XPath - node-to-xpath reverse lookup
by georgeh (Initiate) on Nov 11, 2010 at 02:26 UTC

    Thank you very much. This is exactly what I was looking for.

    My own XML::XPath knowledge is limited, and I could not have come with the $ret expression you've given here.

    Thanks again.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2017-09-20 17:53 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (237 votes). Check out past polls.