http://www.perlmonks.org?node_id=634160

Photius has asked for the wisdom of the Perl Monks concerning the following question:

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?