convert xml document to comma delimited (CSV) file using xslt stylesheet.
- by Brad H
I need some assistance converting an xml document to a CSV file using an xslt stylesheet. I am trying to use the following xsl and I can't seem to get it right. I want my comma delimited file to include column headings, followed by the data. My biggest issues are removing the final comma after the last item and inserting a carriage return so each group of data appears on a separate line. I have been using XML Notepad.
<xsl:template match="/">
<xsl:element name="table">
<xsl:apply-templates select="/*/*[1]" mode="header" />
<xsl:apply-templates select="/*/*" mode="row" />
</xsl:element>
</xsl:template>
<xsl:template match="*" mode="header">
<xsl:element name="tr">
<xsl:apply-templates select="./*" mode="column" />
</xsl:element>
</xsl:template>
<xsl:template match="*" mode="row">
<xsl:element name="tr">
<xsl:apply-templates select="./*" mode="node" />
</xsl:element>
</xsl:template>
<xsl:template match="*" mode="column">
<xsl:element name="th">
<xsl:value-of select="translate(name(.),'qwertyuiopasdfghjklzxcvbnm_','QWERTYUIOPASDFGHJKLZXCVBNM ')" />
</xsl:element>,
</xsl:template>
<xsl:template match="*" mode="node">
<xsl:element name="td">
<xsl:value-of select="." />
</xsl:element>,
</xsl:template>