in reply to Ignore elements using twig module

I'm not familiar with XML::Twig, so I don't know if those paths are XPaths, but if they are, you could use child::type[text()!="yyy"]/.. (if <data> is context node) or //type[text()!="yyy"]/.. (independent of context).

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)

Replies are listed 'Best First'.
Re^2: Ignore elements using twig module
by basalto (Beadle) on Feb 23, 2008 at 08:41 UTC
    If type is defined as attribute it works, but changing the structure is not an option. Thanks anyway.