Solved

Include Headers in csv spreadsheet from XML to CSV with XSLT

Posted on 2004-10-18
3
558 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now