Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: XML:: Twig - can you check for text following the element being handled?

by Anonymous Monk
on Nov 08, 2011 at 17:17 UTC ( #936830=note: print w/replies, xml ) Need Help??


in reply to Re: XML:: Twig - can you check for text following the element being handled?
in thread XML:: Twig - can you check for text following the element being handled?

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 ' ];
  • Comment on Re^2: XML:: Twig - can you check for text following the element being handled?
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://936830]
help
Chatterbox?
[ambrus]: MLX: if it's a work email, then it's probably not Uncle Sam that matters, but what the account managing server at work thinks your name is. Those can differ. For example, we've had two co-workers with identical real name at one point,
[ambrus]: so one got a stupid suffix in the email account (people have email address based on their real name here usually).

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2017-01-19 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (170 votes). Check out past polls.