in reply to Using XML::XSLT to convert XML to CSV

I'm using XML::Parser 2.34 and XML::XSLT 0.48 (that's the current version). And my error message is a little more specific (note that I changed the names of the input files):

perl test.pl Error while parsing: not well-formed (invalid token) at line 9, column 0, byte 244 at /usr/ +local/lib/perl5/site_perl/5.8.5/i686-linux-thread-multi/XML/Parser.pm + line 187 input.xsl at /usr/local/lib/perl5/site_perl/5.8.8/XML/XSLT.pm line 150 +7.
the line just before line 9 in the xsl file is:
</xsl:template
If I fix that tag I get
Argument syntax of call to XML::XSLT::transform deprecated. See the d +ocumentation for XML::XSLT::transform at test.pl line 9 Error while parsing: syntax error at line 1, column 0, byte 0 at /usr/local/lib/perl5/site_ +perl/5.8.5/i686-linux-thread-multi/XML/Parser.pm line 187 XMLFile at /usr/local/lib/perl5/site_perl/5.8.8/XML/XSLT.pm line 1507.
That lead me to fix the code like this:
use strict; use XML::XSLT; my $xmlFilename = 'input.xml'; my $xslFilename = 'input.xsl'; (my $outFilename = $xmlFilename) =~ s/\.xml$/\.csv/i; my $xslt = XML::XSLT->new($xslFilename, warnings => 1); $xslt->transform($xmlFilename); print $xslt->toString;
Which comes kind of close to the desired output:

KCNU2073828479792101708/20/200714:0008/20/2007 09:45:00 KCOU10071877975751901308/20/200714:0008/20/2007 09:45:00
Personally, I would use XML::Twig and Text::xSV / Text::CSV for this kind of thing.

update: in any case you should probably make sure you've got the latest stable versions of both XML::XSTL and XML::Parser.

Replies are listed 'Best First'.
Re^2: Using XML::XSLT to convert XML to CSV
by Photius (Sexton) on Aug 21, 2007 at 22:03 UTC
    Joost, thanks for spotting the missing > and for your advice.

    I was still getting the same error after fixing that, though.
    I found the cause. It works fine as long as the input.xml filename and input.xsl filename do *not* have a path in front of them. I am on a Windows platform and have tried the formats:
    'C:/mypath/input.xml'
    'C:\mypath\input.xml'
    "C:/mypath/input.xml"
    "C:\\mypath\\input.xml"

    It seems it can only accept files in the current directory??
      I'm not sure what the problem could be (and I don't have a windows perl available at the moment) but this works for me on linux:
      use strict; use XML::XSLT; my $xmlFilename = '/home/joost/input.xml'; my $xslFilename = '/home/joost/input.xsl'; (my $outFilename = $xmlFilename) =~ s/\.xml$/\.csv/i; my $xslt = XML::XSLT->new($xslFilename, warnings => 1); $xslt->transform($xmlFilename); print $xslt->toString;
      Can't help you much further than that, though.

      update: did you update the XML modules, or where they already at the latest version?

      You can check by doing

      perl -MXML::XSLT -e'print "$XML::XSLT::VERSION\n"'
      and similar for XML::Parser
        When I paste the above in my Windows XP command line to check the version, I get:
        Can't find string terminator "'" anywhere before EOF at -e line 1.
        However, I put this in a script file:
        use XML::XSLT; print "$XML::XSLT::VERSION\n";
        and then it prints:
        0.48
      On the one hand, the documentation says you should use "base" as an argument to the transform call.

      On the other hand, I tried that and I still get that error.



      Nobody says perl looks like line-noise any more
      kids today don't know what line-noise IS ...

      hey,

      i had the same problem using parser.pm on windows xp machine for absolute paths.

      solved by adding "file:///" on the beginning of the path

      ex: XML::XSLT->new ("file:///C:/mypath/to/something.xsl" );