Solved

Include Headers in csv spreadsheet from XML to CSV with XSLT

Posted on 2004-10-18
3
583 Views
Last Modified: 2006-11-17
I am doing an XML to CSV conversion successfully with the XSL below.
I want to know how to modify this XSL so that my CSV spreadsheet will have column headers Class, Last Name, First Name etc.

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output encoding="iso-8859-1" method="text" version="1.0"/>
      <xsl:template match="/">
            <xsl:apply-templates select="NewDataSet"/>
      </xsl:template>
      <xsl:template match="NewDataSet">
            <xsl:for-each select="Students">
                  <xsl:value-of select="Class"/>
                  <xsl:text>,</xsl:text>
                  <xsl:value-of select="Last_x0020_Name"/>
                  <xsl:text>,</xsl:text>
                  <xsl:value-of select="First_x0020_Name"/>
                  <xsl:text>,</xsl:text>
                  <xsl:value-of select="Level"/>
                  <xsl:text>,</xsl:text>
                  <xsl:value-of select="Previous_x0020_Score"/>
                  <xsl:text>,</xsl:text>
                  <xsl:value-of select="MaxScore"/>
                  <xsl:text>&#013;</xsl:text>
            </xsl:for-each>
      </xsl:template>
</xsl:stylesheet>

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Thanks.
0
Comment
Question by:nathanpz
3 Comments
 
LVL 19

Assisted Solution

by:ramazanyich
ramazanyich earned 300 total points
ID: 12344784
Just before <xsl:for-each> put:
<xsl:text>Class,Last Name,first name,Level,Previous_Score,MaxScore&x013;</xsl:text>

or you can do it dynamically (also put before xsl:for-each):
<xsl:for-each select="Students::child">
<xsl:text><xsl:value-of select="local-name(.)"/></xsl:text>
<xsl:switch>
<xsl:when test="position()=last()">
<xsl:text>&#13;</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>,</xsl:text>
</xsl:otherwise>
</xsl:switch>
</xsl:for-each>
0
 
LVL 7

Accepted Solution

by:
J_Mak earned 200 total points
ID: 12344801
Adding column headers is just a matter of leaving a row for them. That is, before reading the data, generate the headers. Check this out:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output encoding="iso-8859-1" method="text" version="1.0"/>
     <xsl:template match="/">
          <xsl:call-template name="CreateHeaders"/>
          <xsl:apply-templates select="NewDataSet"/>
     </xsl:template>
     <xsl:template name="CreateHeaders"/>
          <xsl:text>Class</xsl:text>
          <xsl:text>,</xsl:text>
          <xsl:text>Last Name</xsl:text>
          <xsl:text>,</xsl:text>
          <xsl:text>First Name</xsl:text>
          <xsl:text>,</xsl:text>
          <xsl:text>Level</xsl:text>
          <xsl:text>,</xsl:text>
          <xsl:text>Previous Score</xsl:text>
          <xsl:text>,</xsl:text>
          <xsl:text>MaxScore</xsl:text>
          <xsl:text>&#013;</xsl:text>
     </xsl:template>
     <xsl:template match="NewDataSet">
          <xsl:for-each select="Students">
               <xsl:value-of select="Class"/>
               <xsl:text>,</xsl:text>
               <xsl:value-of select="Last_x0020_Name"/>
               <xsl:text>,</xsl:text>
               <xsl:value-of select="First_x0020_Name"/>
               <xsl:text>,</xsl:text>
               <xsl:value-of select="Level"/>
               <xsl:text>,</xsl:text>
               <xsl:value-of select="Previous_x0020_Score"/>
               <xsl:text>,</xsl:text>
               <xsl:value-of select="MaxScore"/>
               <xsl:text>&#013;</xsl:text>
          </xsl:for-each>
     </xsl:template>
</xsl:stylesheet>

Hope that helps
0
 

Author Comment

by:nathanpz
ID: 12345132
Thanks for that ramazanyich and J_Mak. I actually meant for the top response to be the accepted answer but they were both good answers.

Cheers.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question