in reply to Anomalous double spaced output from XML::Twig

After your updates it is unclear to me if you still have the problem or not. Could you confirm that there is still something to be investigated here?

Especially as the code runs fine on my machine (XML::Twig v 3.23 on Linux).

  • Comment on Re: Anomalous double spaced output from XML::Twig

Replies are listed 'Best First'.
Re^2: Anomalous double spaced output from XML::Twig
by GrandFather (Saint) on Jul 22, 2006 at 00:22 UTC

    The issue is that the original line ends are retained, but that new lines are expanded to OS specific line endings. One work around for modest size files is to slurp the file then use parse.

    This code demonstrates the parsing problem:

    use warnings; use strict; use XML::Twig; my $str = <<"XML"; <?xml version="1.0" encoding="utf-8"?>\r <experiment title="Blood Pressure" version="1.2" softwareVersion="1.1" +>\r <sourceFiles>\r </sourceFiles>\r <dataFiles>\r <dataFile>cuff practice.dat</dataFile>\r </dataFiles>\r </experiment>\r XML # Troublesome code print "******* Troublesome version\n"; my @fileList = qw(first second); my $sub = bindArgs (\&insertFiles, \@fileList); my $twig = XML::Twig->new ( twig_roots => {'sourceFiles' => $sub,}, # Insert file list twig_print_outside_roots => 1, # Print the rest ); $twig->parse ($str); sub bindArgs { my ($sub, @args) = @_; return sub {$sub->(@args, @_);}; } sub insertFiles { my ($sourceFiles, $twig, $node) = @_; for (@$sourceFiles) { my $eFile = $twig->root->new (sourcefile => $_); # create the +element $eFile->paste (first_child => $node); } $node->print (); }

    Prints:

    ******* Troublesome version <?xml version="1.0" encoding="utf-8"?> <experiment title="Blood Pressure" version="1.2" softwareVersion="1.1" +> <sourceFiles><sourcefile>second</sourcefile><sourcefile>first</sourcef +ile></sourceFiles> <dataFiles> <dataFile>cuff practice.dat</dataFile> </dataFiles> </experiment>

    It would be nice if XML::Twig or XML::Parser collapsed various line ending sequences to a single \n character or alternatively XML::Twig should ignore non-signficant line endings.


    DWIM is Perl's answer to Gödel
Re^2: Anomalous double spaced output from XML::Twig
by GrandFather (Saint) on May 22, 2006 at 10:01 UTC

    Yes I still have the problem that the output generated by twig_print_outside_roots (that is, the original XML) is generated with line endings as "\r\r\n" rather than the "\r\n" line endings that are conventional for windows.

    I would guess that the parser is striping "\n" characters rather than $/, but that print is generating OS specific line endings. Given that *nix uses "\n" line endings it is unsurprising that the sample code produces correct output on a *nix system.

    The problem remains, although I now understand why it is there somewhat. I still don't know how to fix it within XML::Twig.


    DWIM is Perl's answer to Gödel

      You seem be under the mistaken impression that $/ is different on Windows than it is elsewhere. $/ is just "\n" (by default) everywhere.

      This problem would happen on Windows if the input is read in binmode but the output is written without binmode.

      - tye