first
second
third
fourth
fifth
sixth
first
second
third
fourth
fifth
sixth
##
#!/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;