I'm using XML::Twig based on the "Building an XML Filter" example from the docs. It is a good fit, as I only need to modify or remove a small number of nodes, and everything else gets passed through.

However, I need to access some attributes from the root element. If I don't use twig_roots then it has to load the whole file and it's not as trivial to simply not print nodes to be removed. It kind of defeats the main feature of Twig.

Is there a good way to read the attributes from the top-level element, and then configure the twigs (and print outside roots) and callbacks based on what I found? How to get what was already read to print seemlessly with what it will start handling differently?

Another idea is to start processing, then quit after the first start tag. Is there a flag or something that can be set from the callback to "cancel" and return from parse() without reading any more from the file?

—John

2006-05-05 Retitled by GrandFather, as per Monastery guidelines
Original title: 'accessing root tags when XML::Twig and twig_roots'


In reply to accessing root tags when using XML::Twig and twig_roots by John M. Dlugosz

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.