in reply to XML::Twig parsing
You can use ignore on an element in a handler to skip the element. The element needs to be the current one or one of its ancestors. That means that you can set a handler on level3/name, check whether you want to keep the element or not there, and skip the entire level3 element if you want to:
#!/usr/bin/perl use strict; use warnings; use XML::Twig; XML::Twig->new( twig_handlers => { 'level3/name' => sub { if( $_->text + !~ m{^include}) { $_->parent->ignore; } }, level3 => sub { print "eleme +nt processed:\n", $_->sprint, "\n"; } }, ) ->parse( \*DATA); __END__ <level1> <level2> <level3> <name>include</name> <rule>included rule 1</rule> <rule>included rule 2</rule> </level3> <level3> <name>exclude</name> <rule>excluded rule</rule> <rule>excluded rule</rule> </level3> </level2> </level1>
Does this help?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML::Twig parsing
by oilerfan21 (Initiate) on Jun 12, 2008 at 18:03 UTC |