Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-18 12:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found