#!/usr/bin/perl use warnings; use strict; use XML::Twig; my $twig = new XML::Twig( pretty_print => 'indented', ); $twig->parse(<<'XML');

first

second

third

fourth

fifth

sixth

XML my @labels = $twig->get_xpath('/article/fig/label'); my @captions = $twig->get_xpath('/article/fig/caption'); for my $i (0..$#labels) { $labels[$i]->move('first_child', $captions[$i]); } $twig->print;