I have a modest amount of Perl experience but I have never used XSLT. My forehead is getting pretty sore from frequent encounters with the keyboard. I need to convert an XML file into CSV for input to another system. Various articles on the internet made XML::XSLT sound like just the ticket, but I haven't been able to make it work.

My XML file looks like this (only larger):

<?xml version="1.0" encoding="UTF-8"?> <WEATHER_DATA> <HOUR_DATA> <LOCATION_NAME>KCNU</LOCATION_NAME> <CLC>20</CLC> <DPT>73</DPT> <HUM>82</HUM> <HIX>84</HIX> <DBT>79</DBT> <WCH>79</WCH> <WDR>210</WDR> <WSP>17</WSP> <EFFECTIVE_DATE>08/20/2007</EFFECTIVE_DATE> <HOUR>14:00</HOUR> <TIME_GENERATED>08/20/2007 09:45:00</TIME_GENERATED> </HOUR_DATA> <HOUR_DATA> <LOCATION_NAME>KCOU</LOCATION_NAME> <CLC>100</CLC> <DPT>71</DPT> <HUM>87</HUM> <HIX>79</HIX> <DBT>75</DBT> <WCH>75</WCH> <WDR>190</WDR> <WSP>13</WSP> <EFFECTIVE_DATE>08/20/2007</EFFECTIVE_DATE> <HOUR>14:00</HOUR> <TIME_GENERATED>08/20/2007 09:45:00</TIME_GENERATED> </HOUR_DATA> </WEATHER_DATA>

I need to transform it to CSV like this:

# relation, date, time, CloudCoverPct, DewPointF, Humidity, TempF, Win +dDirection, WindMPH, HeatIndexF, WindChillF KCNU.CHANUTE.KS,08/20/2007,14:00,20,73,82,79,210,17,84,79 KCOU.COLUMBIA.MO,08/20/2007,14:00,100,71,87,75,190,13,79,75

Note that the .csv columns are not in the same order as the tags in the .xml file and the site (a.k.a. relation) name will need to be expanded later.
I realize I will have to enhance the .xls file and add more logic in the script, but at this point, I have not even been able to get XML::XSLT->new() to work.

The skeletal beginning of my .xsl file (realizing it doesn't do everything yet) is:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Tr +ansform"> <xsl:output method="text"/> <xsl:template match="WEATHER_DATA"> <xsl:apply-templates select="HOUR_DATA"/> </xsl:template <xsl:template match="HOUR_DATA"> <xsl:for-each select="*"> <xsl:value-of select="."/> <xsl:if test="position() != last()"> <xsl:value-of select="','"/> </xsl:if> </xsl:for-each> <xsl:text>&#10;</xsl:text> </xsl:template> </xsl:stylesheet>

The skeletal beginning of my .pl script (realizing it doesn't do everything yet) is:

use strict; use XML::XSLT; my $xmlFilename = 'C:/Perldev/xslt/WEATHER_ACTUAL_20070820144516.XML'; my $xslFilename = 'C:/Perldev/xslt/StyleSheet2.xsl'; (my $outFilename = $xmlFilename) =~ s/\.xml$/\.csv/i; my $xslt = XML::XSLT->new($xslFilename, warnings => 1); my $result = $xslt->transform(XMLFile => $xmlFilename, XSLFile => $xslFilename, OutFile => $outFilename );

The error it returns is:
Error while parsing: no element found at line 1, column 0, byte -1 at C:/Perl/lib/XML/Parse +r.pm line 187

What am I doing wrong?

In reply to Using XML::XSLT to convert XML to CSV by Photius

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.