I got as far as this using "XML::Twig":
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use XML::Twig; my $twig = XML::Twig->parse( \*DATA ); print $twig ->get_xpath( '/root/fish/carrot[@colour="orange"]/pie', 0 +)->text, "\n"; foreach my $node ( $twig->get_xpath('//*') ) { my @path_tags; my @path_with_att; my $cursor = $node; while ($cursor) { unshift( @path_tags, $cursor->tag ); my $att_path = ""; if ( $cursor->atts ) { $att_path = join( "", map { "[@" . $_ . "=\"" . $cursor->att($_) . "\"]" } keys %{ $cursor->atts } ); } unshift( @path_with_att, $cursor->tag . $att_path ); $cursor = $cursor->parent; } print join( "/", @path_tags ), "\n"; my $xpath_with_atts = "/" . join( "/", @path_with_att ); print $xpath_with_atts, "\n"; print "Found:", $twig->get_xpath( $xpath_with_atts, 0 )->tag, "\n" +; } __DATA__ <root> <fish skin="scaly" home="pond"> <carrot colour="orange"> <pie>This value</pie> </carrot> </fish> </root>
It doesn't quite do what you want, unfortunately, as what it doesn't do is give you the numeric index of the node, and so whilst the XPATH is valid, it's not necessarily unique.
I'm going to try and think if there's a good way to do what you're wanting.
In reply to Re: How can I browse & list XPATH of a XML Message?
by Preceptor
in thread How can I browse & list XPATH of a XML Message?
by MDRI
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |