http://www.perlmonks.org?node_id=870153


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