#!/usr/bin/perl use warnings; use strict; use XML::Twig; my $twig = new XML::Twig( twig_handlers => { '/article/fig' => sub { my $label = $_->first_child('label') or return; my $caption = $_->first_child('caption') or return; $label->move('first_child', $caption); $_->flush; } }, pretty_print => 'indented', ); $twig->parse(<<'XML');

first

second

third

fourth

fifth

sixth

XML $twig->print;