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 | |
by ateague (Monk) on Jun 12, 2014 at 18:11 UTC | |
by Anonymous Monk on Jun 26, 2014 at 23:34 UTC | |
|
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 | |
by ateague (Monk) on Jun 30, 2014 at 00:22 UTC |