#!/usr/bin/perl use strict; use warnings; use XML::Twig; XML::Twig->parse( pretty_print => 'indented', shift @ARGV) ->root ->sort_children( \&get_pui, type => 'numeric') ->print; sub get_pui { my( $item)= @_; return $item->first_descendant( 'itemid[@idtype="PUI"]')->text; }