#! /usr/bin/perl use strict; use warnings; use XML::Twig; my $file = $ARGV[0]; my $twig= new XML::Twig( TwigRoots=>{geography=>1}, TwigHandlers=> { 'node' =>\&geog_node } ); $twig->parsefile($file); sub geog_node { my($twig, $node) = @_; # ancestors_or_self returns the node first, the ancestors from inner to outter # hence the reverse foreach my $node_to_output ( reverse $node->ancestors_or_self( 'node[!@output]')) { if($node_to_output->att('hidden') eq '0') { print node_summary( $node_to_output); } $node_to_output->set_att( output => 1); # mark so we don't try to output them again } $twig->purge; # now it's safe to purge } sub node_summary { my $node= shift; return sprintf "Data for node:\tID:\t%s\n\tDesc:\t%s\n", $node->att('id'), $node->field('description') ; }