Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add column names to XML

Status
Not open for further replies.
Sep 10, 2009
37
US
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="<xsl:eek:utput 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!!
 
[1] Add a template constructed in parallel to existing one with mode attribute.
[tt]
<xsl:template match="Table" mode="heading">
<xsl:for-each select="*">
<xsl:value-of select="local-name()"/>
<xsl:if test="position() != last()">
<xsl:value-of select="','"/>
</xsl:if>
</xsl:for-each>
<xsl:text>&#10;</xsl:text>
</xsl:template>
[/tt]
[2] Then match it with the first encounter.
[tt]
<xsl:template match="NewDataSet">
<xsl:apply-templates select="Table[1]" mode="heading" />
<xsl:apply-templates select="Table"/>
</xsl:template>
[/tt]
 
Thanks, that adds the colunm headings but now it removes the commas in the dataset? Any advice on how to add those?

Thanks!!
 
Not sure why it did that, but I figured it out! Thanks a bunch for your help!!
 
Figure it out of what? It has nothing to do with what I posted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top