my $t= parse XML::Twig::Elt(\*DATA); $t->sort_children( sub { my $elt = shift; my $top = $elt->att('attribute'); my $kid = $elt->first_child->tag; return $top . $kid; }); $_->print for $t->children;