in reply to Sort xml based on attribute

That's something I've had to do in the past, so as usual it ended up in XML::Twig. The sort_children method gets called on the parent, and gets passed a function, which will be called on each child in turn. That function will return the sort criteria. The method also takes options to specify the type of sort (numeric or alpha) and the order.

This leads to the code below:

#!/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; }

Note that this code relies on a couple of assumptions:

Does this help?