#!/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__ This value