How to add column names to XML

Questions about XML that are not covered by the other forums should go here.
johntoh
Posts: 1
Joined: Mon Nov 02, 2009 7:10 pm
Contact:

How to add column names to XML

Post 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!!
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: How to add column names to XML

Post by sorin_ristache »

Hello,

I think first you should go through an XSLT tutorial.


Regards,
Sorin
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: How to add column names to XML

Post 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
George Cristian Bina
Post Reply