Page 1 of 1

How to add column names to XML

Posted: Wed Nov 04, 2009 9:11 pm
by johntoh
I am a newbie to XML and I have an assignment to create CSV file using XML? I have the below XSLT code and it returns the values in CSV, now I want to add the column headings to the file as well, how would I go about doing this? Below is my code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="NewDataSet">
<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>


Any help is GREATLY appreciated!!

Re: How to add column names to XML

Posted: Thu Nov 05, 2009 11:18 am
by sorin_ristache
Hello,

I think first you should go through an XSLT tutorial.


Regards,
Sorin

Re: How to add column names to XML

Posted: Thu Nov 05, 2009 11:40 am
by george
Note tested as you did not provide a sample XML but something like below:

Code: Select all


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="NewDataSet">
<xsl:apply-templates select="Table" mode="header"/>
<xsl:apply-templates select="Table"/>
</xsl:template>

<xsl:template match="Table" mode="header">
<xsl:for-each select="*[1]">
<xsl:value-of select="name()"/>
<xsl:if test="position() != last()">
<xsl:value-of select="','"/>
</xsl:if>
</xsl:for-each>
<xsl:text>&#10;</xsl:text>
</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>
Regards,
George