Update: I couldn't leave it at that, so I looked deeper...
Foiled! The docs say "XPath expressions are limited to using the child and descendant axis". While it also handles the attribute axis (via "@"), I verified that the parent axis (even via "..") isn't supported. It doesn't seem to understand text() either, which also requires advance knowledge of the node's children.
All in all, that makes sense. How can you skip parsing something based on something that hasn't been parsed yet.
A better schema for your needs would have been
<doc> <data type="xxx"> <vars>a</vars> </data> <data type="yyy"> <vars>b</vars> </data> </doc>
Then it would be easy:
use XML::Twig; my $twig = XML::Twig->new( twig_roots => { 'data[@type != "yyy"]' => 1, }, ); $twig->parse(\*DATA); $twig->print(); __DATA__ <doc> <data type="xxx"> <vars>a</vars> </data> <data type="yyy"> <vars>b</vars> </data> </doc>
<doc><data type="xxx"><vars>a</vars></data></doc>
(!= didn't work with v3.26, but worked after upgrading to v3.32)
In reply to Re: Ignore elements using twig module
by ikegami
in thread Ignore elements using twig module
by basalto
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |