Solved

Include Headers in csv spreadsheet from XML to CSV with XSLT

Posted on 2004-10-18
3
596 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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