#!/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; #### #!/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; ##
## #!/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;