Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I think this is working as designed, la handler is called before its parent snot , before siblings are parsed and added to parent, so naturally next_sibling_text returns empty string until the parent snot handler is called

So the best and simples solution is to do this testing in a handler for the parent

#!/usr/bin/perl -- use strict; use warnings; use XML::Twig; my $xml = <<'__XML__'; <?xml version="1.0" encoding="UTF-8"?> <root> <snot>the <la>snot</la> balls are made of snot </snot> <snot>the <la>snot</la> bells are made of snot </snot> <snot>the <la>snot</la> bowls are made of snot </snot> </root> __XML__ #~ Handlers are triggered in fixed order, sorted by their type #~ (xpath expressions first, then regexps, then level), then by #~ whether they specify a full path (starting at the root element) #~ or not, then by by number of steps in the expression , then #~ number of predicates, then number of tests in predicates. #~ Handlers where the last step does not specify a step #~ ("foo/bar/*") are triggered after other XPath handlers. Finally #~ "_all_" handlers are triggered last. { my @snot; my $t = XML::Twig->new( twig_handlers => { 'snot' => sub { warn $_->path, "\n"; push @snot, $_->text; return !!1; }, ## la , triggered before snot 'la' => sub { warn $_->path, "\n"; push @snot, [ $_->text , ## doesn't contain next_sibling_text because not parsed yet, as expect +ed $_->parent->text , ]; return !!1; }, }, ); $t->parse($xml); undef $t; use Data::Dumper(); print Data::Dumper->new([ \@snot ])->Indent(1)->Dump; } __END__ /root/snot/la /root/snot /root/snot/la /root/snot /root/snot/la /root/snot $VAR1 = [ [ 'snot', 'the snot' ], 'the snot balls are made of snot ', [ 'snot', 'the snot' ], 'the snot bells are made of snot ', [ 'snot', 'the snot' ], 'the snot bowls are made of snot ' ];

In reply to Re^2: XML:: Twig - can you check for text following the element being handled? by Anonymous Monk
in thread XML:: Twig - can you check for text following the element being handled? by mertserger

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (4)
    As of 2018-01-22 10:58 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (233 votes). Check out past polls.