I'm using this code to delete nodes from a file
my $parser = XML::LibXML->new(); $dom = $parser->parse_file( "OLDFILE" ); for my $matrix ( $dom->findnodes( q{ //version_matrix/vm[@type='br'] + } )) { my $version = $matrix->findvalue('./release/@version'); if( $version gt $brver ) { print "discarding version_matrix for $version\n"; my $version_matrix_node = $matrix->parentNode; $version_matrix_node->unbindNode; } } my $changed = $dom->toString; my $fh; open( $fh, '>', "NEWFILE" ); print { $fh } $changed;
where the input "OLDFILE" data is like this fragment:
<supported_version_matrix> <version_matrix> <app> <release version="10.1.e"> <fixed>125.2004</fixed> </release> </app> <vm type="br"> <release version="7.3.0"> </release> </vm> </version_matrix> <version_matrix> <app> <release version="10.1.e"> <fixed>125.2004</fixed> </release> </app> <vm type="br"> <release version="7.2.2"> </release> </vm> </version_matrix> . . . </supported_version_matrix>
The output file "NEWFILE" has gaps of a couple of lines in place of the discarded nodes, like this (where the version_matrix for br 7.3.0 was unbound):
<supported_version_matrix> <version_matrix> <app> <release version="10.1.e"> <fixed>125.2002</fixed> </release> </app>
The blank lines in the gaps each contain a couple of tab characters only. I think (but not 100% sure) that from an xml perspective this doesn't change the structure, but it looks bad. Am I doing something wrong, like maybe the "$matrix" child node has to be freed somehow before its parent can be unbound? Or is there an easy way to avoid it?

thanks for any illumination


In reply to XML::LibXML unbindNode leaves blank lines in the xml file by anadem

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.