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)


In reply to Re: Ignore elements using twig module by ikegami
in thread Ignore elements using twig module by basalto

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.