use strict; use warnings; use XML::Twig; my $xml = < XML my $twig = XML::Twig->new (twig_handlers => {'[@name]' => \&showNesting}); $twig->parse ($xml); sub showNesting { my @path; for my $elt (reverse $_->ancestors_or_self ()) { next unless defined $elt->att ('name'); push @path, $elt->att ('name'); } print join ('.', @path), "\n" if @path; }