ateague has asked for the wisdom of the Perl Monks concerning the following question:

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>

Replies are listed 'Best First'.
Re: XML::Twig's twig_print_outside_roots adds extra blank lines to output
by choroba (Cardinal) on Jun 12, 2014 at 17:12 UTC
    You told XML::Twig to print "outside roots". The whitespace around the statement tags is part of the XML, they are text() nodes, and they get printed as you wanted.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Thanks for the clarification

      Is there a way to filter out that text() whitespace when twig_print_outside_roots is enabled? Or is the better option to use twig_handlers in lieu of twig_roots?

      I'd like to know the answer to this also, please.
Re: XML::Twig's twig_print_outside_roots adds extra blank lines to output
by remiah (Hermit) on Jun 27, 2014 at 14:17 UTC

    hello ateague.
    Sometimes I met the case when pretty print doesn't work with XML::Twig. If pretty print is the problem, how about this?

    XML::Twig->new( twig_handlers => { 'statement' => sub { my($twig,$elt)=@_; if( $elt->find_nodes('primary/acct', 0)->trimmed_text() eq + '903264' ){ $twig->flush; }else { $twig->purge; } }, 'statement//*' =>sub {}, #skip descendants of statement '_default_' => sub { #just flush my($twig,$elt)=@_; $twig->flush; } }, #twig_print_outside_roots =>1, pretty_print => 'indented', )->parse(*DATA); __DATA__ same data
    This prints pretty with my XML::Twig
    C:\temp>perl -MXML::Twig -e "print $XML::Twig::VERSION" 3.42
    This example be "potato", becuase it kills the advantage to use twig_root.
    good luck.

      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: