#!/usr/bin/perl
use 5.020;
use strict;
use warnings;
use XML::Twig;
##
## This works as expected
##
my $working_xml = XML::Twig->new(
twig_handlers => {
'/shipment/box' => sub { _move(@_); 1; },
},
pretty_print => 'indented',
)->safe_parse(
<<"XML"
potato
pear
peach
apple
XML
);
say "\n\n---------------------\n\n";
##
## This 'breaks', printing the final '' tag twice
##
my $broken_xml = XML::Twig->new(
twig_handlers => {
'/shipment/box' => sub { _move(@_); 1; },
},
pretty_print => 'indented',
)->safe_parse(
<<"XML"
potato
pear
peach
nothing here!
XML
);
# ----------
sub _move {
foreach my $descendant ( $_[1]->descendants('slip') ) {
$descendant->set_att('_TYPE' => 'produce');
# Move the out of the section
#
# This appears to be the problem, even though
# this should never be reached in the second
# document (no descendants)
$descendant->move('before', $_[1]);
}
$_[1]->delete;
$_[0]->flush();
1;
}