Thank you very much Remiah! That was exactly what I was looking for.

For those that come after, here is the final code I used to get my desired output:

#!/usr/bin/perl use 5.016; use strict; use warnings; use XML::Twig; { open (my $OFILE, '>:utf8', './bits.xml') or die "Could not open [. +/bits.xml]:\n$!\n$^E"; ## ## An aproximation of 'twig_roots' and 'twig_print_outside_roots' +that ## keeps pretty printing and does not print superfluous blank line +s ## my $t = XML::Twig->new( twig_handlers => { 'statement' => sub { _statement(@_, $OFILE); 1; }, 'statement//*' => sub { 1; }, # skip descendants of statem +ent '_default_' => sub { $_[0]->flush($OFILE); 1; }, # just + flush }, pretty_print => 'indented', ); $t->parse(*DATA); close $OFILE; } sub _statement { my ($_twig, $stmt_element, $OFILE) = @_; my $acct = $stmt_element->find_nodes('primary/acct', 0)->trimmed_t +ext(); if ( $acct eq '903264' ) { $_twig->flush($OFILE); } else { $_twig->purge(); } return; } __DATA__ <batch> <header> <foo>1</foo> <bar>2</bar> <baz>3</baz> </header> <statement> <primary> <acct>000000</acct> </primary> <bits>Im an apple!</bits> </statement> <statement> <primary> <acct>000000</acct> </primary> <bits>Im a carrot!</bits> </statement> <statement> <!-- I only want this statement --> <primary> <acct>903264</acct> </primary> <bits>Im a potato!</bits> </statement> <statement> <primary> <acct>000000</acct> </primary> <bits>Im a pear!</bits> </statement> <statement> <primary> <acct>000000</acct> </primary> <bits>Im a pickle!</bits> </statement> <statement> <primary> <acct>000000</acct> </primary> <bits>Im a banana!</bits> </statement> <statement> <primary> <acct>000000</acct> </primary> <bits>Im an eggplant!</bits> </statement> <trailer> <stuff>Oh god how did this get here i am not good with compute +r!</stuff> </trailer> </batch>


In reply to Re^2: XML::Twig's twig_print_outside_roots adds extra blank lines to output by ateague
in thread [SOLVED] XML::Twig's twig_print_outside_roots adds extra blank lines to output by ateague

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.