Solved

Include Headers in csv spreadsheet from XML to CSV with XSLT

Posted on 2004-10-18
3
590 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

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…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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