Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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...


Comment on Re: XML::XPath - node-to-xpath reverse lookup
Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2015-07-29 10:44 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