I am using XML::Twig with twig_roots in a script to filter out elements and print them to a separate file. The script is using $twig->flush() and $twig->purge() to further filter elements and only print the ones that match certain criteria. I am running into issues where twig_print_outside_roots is adding extra blank lines to the output.

Am I doing something wrong? Is there some option ("replace_flushed_nodes_with_blank_line => 0" perhaps?) that I need to set to disable this? I am still learning how to use XML::Twig, so any critiques, comments, and idiom suggestions would greatly be appreciated!

Thank you for your time.

Sample code and 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"; my $t = XML::Twig->new( twig_roots => { 'statement' => sub { push @_, $OFILE; &_statement; 1; }, }, twig_print_outside_roots => $OFILE, 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>

Output:

<batch> <header> <foo>1</foo> <bar>2</bar> <baz>3</baz> </header> <statement> <!-- I only want this statement --> <primary> <acct>903264</acct> </primary> <bits>Im a potato!</bits> </statement> <trailer> <stuff>Oh god how did this get here i am not good with compute +r!</stuff> </trailer> </batch>>

Expected output:

<batch> <header> <foo>1</foo> <bar>2</bar> <baz>3</baz> </header> <statement> <!-- I only want this statement --> <primary> <acct>903264</acct> </primary> <bits>Im a potato!</bits> </statement> <trailer> <stuff>Oh god how did this get here i am not good with compute +r!</stuff> </trailer> </batch>


In reply to [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.