Dorel Pureca Dorel Pureca - 3 years ago 109
SQL Question

Create a CSV from a SQL query using XSL

I need help to create a CSV file based on a SQL query.

This is how the current XSL looks like:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="iso-8859-1"/>
<xsl:param name="fieldNames" select="'yes'" />
<xsl:template match="NewDataSet">
<xsl:text></xsl:text>
<xsl:apply-templates select="Table"/>
</xsl:template>
<xsl:template match="Table">
<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>


Everything works fine, except the fact that I need to add a header row in the CSV with predefined values for each value. How can I do that?

This is how it looks now: enter image description here

This is how I want the CSV to look like: enter image description here

Answer Source

Add e.g.

<xsl:template match="/">
  <xsl:text>foo,bar,baz
</xsl:text>
  <xsl:apply-templates/>
</xsl:template>

where foo,bar,baz would be your column names.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download