Consider the following code (it works as expected):
#!/usr/bin/perl
use warnings;
use strict;
use XML::Twig::XPath;
my $twig = XML::Twig::XPath->new(
pretty_print => 'indented',
);
$twig->parse(<<'XML');
<?xml version="1.0"?>
<article>
<label>here label for testing</label>
<fig id="fig001" position="float"><label>Fig. 1.</label><caption><p>fi
+rst</p></caption></fig>
<fig id="fig002" position="float"><label>Fig. 2.</label><caption><p>se
+cond</p></caption></fig>
<fig id="fig003" position="float"><label>Fig. 3.</label><caption><p>th
+ird</p></caption></fig>
<fig id="fig004" position="float"><label>Fig. 4.</label><caption><p>fo
+urth</p></caption></fig>
<fig id="fig006" position="float"><caption><p>sixth</p></caption></fig
+>
<fig id="fig005" position="float"><label>Fig. 5.</label><caption><p>fi
+fth</p></caption></fig>
<fig id="fig006" position="float"><label>Fig. 6.</label><caption><p>si
+xth</p></caption></fig>
<fig id="fig006" position="float"><label>Fig. 6.</label></fig>
</article>
XML
my @labels = $twig->findnodes('/article/fig[label and caption]/label')
+;
my @captions = $twig->findnodes('/article/fig[label and caption]/capti
+on');
for my $i (0..$#labels) {
$labels[$i]->move('first_child', $captions[$i]);
}
$twig->print;
s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print