?
Solved

Include Headers in csv spreadsheet from XML to CSV with XSLT

Posted on 2004-10-18
3
Medium Priority
?
613 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 1200 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 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

831 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