Even if i dunno exactly what happen with your code, i'm sure it is wrong. the way you set up the handler made me immediatly suspicious. In fact I never seen a syntax like your

'/shipment/box' => sub { _move(@_); 1; }

Even the SYSNOPSIS for the XML::Twig reports the example on how to call a subroutine to process the part intercepted by th handler:

list    => \&my_list_process,

The, perfectly correct and valid, style to call a sub with &subname is described in perlsub where is stated &NAME;       # Makes current @_ visible to called subroutine. If someone tell you is Perl4 rubbish.. let me know.. ;)

So changing your

# '/shipment/box' => sub { _move(@_); 1; } #wrong # to '/shipment/box' => \&_move,

makes everything much clearer and Twiggy.

For the rest I can say that, also you have an unneeded 1; as last thing in your sub. Seems more logical to have an exit condition if no slip are found (..ooops):

sub _move { unless ($_[1]->descendants('slip')){ $_[0]->purge(); return; } foreach my $descendant ...

This way the program runs as we expected:

#!/usr/bin/perl # use 5.020; unneeded aside from the say use strict; use warnings; use XML::Twig; ## ## This works as expected ## my $working_xml = XML::Twig->new( twig_handlers => { #'/shipment/box' => sub { _move(@_); 1; }, #why? and why 1; a +t the end? '/shipment/box' => \&_move, }, pretty_print => 'indented', )->safe_parse( <<"XML" <?xml version="1.0" encoding="UTF-8"?> <shipment> <box> <packing> <slip>potato</slip> <slip>pear</slip> <slip>peach</slip> </packing> </box> <box> <packing> <slip>apple</slip> </packing> </box> </shipment> XML ); print "\n\n---------------------\n\n"; ## ## This ALSO WORKS FINE NOW!! ## my $broken_xml = XML::Twig->new( twig_handlers => { '/shipment/box' => \&_move, }, pretty_print => 'indented', )->safe_parse( <<"XML" <?xml version="1.0" encoding="UTF-8"?> <shipment> <box> <packing> <slip>potato</slip> <slip>pear</slip> <slip>peach</slip> </packing> </box> <box> <packing> <note>nothing here!</note> </packing> </box> </shipment> XML ); # ---------- sub _move { unless ($_[1]->descendants('slip')){ $_[0]->purge(); return; } foreach my $descendant ( $_[1]->descendants('slip') ) { $descendant->set_att('_TYPE' => 'produce'); $descendant->move('before', $_[1]); } $_[1]->delete; $_[0]->flush(); #1; # why? } ## OUTPUT <?xml version="1.0" encoding="UTF-8"?> <shipment> <slip _TYPE="produce">potato</slip> <slip _TYPE="produce">pear</slip> <slip _TYPE="produce">peach</slip> <slip _TYPE="produce">apple</slip> </shipment> --------------------- <?xml version="1.0" encoding="UTF-8"?> <shipment> <slip _TYPE="produce">potato</slip> <slip _TYPE="produce">pear</slip> <slip _TYPE="produce">peach</slip> </shipment>

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Moving elements with XML::Twig: Document root closing tag is duplicated on output -- solution by Discipulus
in thread [SOLVED] Moving elements with XML::Twig: Document root closing tag is duplicated on output by ateague

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.