#!/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 $count = $twig->get_xpath('/article/fig//label'); for my $c (0..($count-1)) { my $t = $twig->get_xpath('/article/fig/label', 0); #//fig/label -here problem comes my $pl = $twig->get_xpath('/article/fig//caption', $c); my $cut = $t->cut ; $cut->paste('first_child', $pl) ; } $twig->print;