use strict; use warnings; use XML::Twig; my $t= XML::Twig->new ( twig_roots => {'figure' => \&doSwap}, twig_print_outside_roots => 1, ); my $source = do {local $/ = ''; }; $t->set_pretty_print ('record'); $t->parse ($source); sub doSwap { my ($t, $figure)= @_; my @title = $figure->cut_children ('title'); $title[0]->paste ('last_child', $figure); $figure->print; }