xsl-list
[Top] [All Lists]

RE: Transforming XML to CSV

2002-10-15 05:56:16

Ryan,

If you work through it logically - you want each column value encosed in
quotes:

<xsl:template match="column">
  <xsl:text/>"<xsl:apply-templates/>"<xsl:text/>
</xsl:template>

You also want to output a comma if there is another column after the one
you are on:

<xsl:template match="column">
  <xsl:text/>"<xsl:apply-templates/>"<xsl:text/>
  <xsl:if test="following-sibling::column">,</xsl:if>
</xsl:template>

After each row you want a carriage return:

<xsl:template match="row">
  <xsl:apply-templates/>
  <xsl:text>&#xa;</xsl:text>
</xsl:template>

Don't forget of course, that you are outputting text and not xml (the
default)

<xsl:output method="text"/>

And that you dont want any presentational whitespace (or should that be
FWS ;) messing up your nice output:

<xsl:strip-space elements="*"/>

I know that was kind of in reverse order, so to put it all the right way
around:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

 <xsl:strip-space elements="*"/>

 <xsl:output method="text"/>

 <xsl:template match="row">
   <xsl:apply-templates/>
   <xsl:text>&#xa;</xsl:text>
 </xsl:template>

 <xsl:template match="column">
   <xsl:text/>"<xsl:apply-templates/>"<xsl:text/>
   <xsl:if test="following-sibling::column">,</xsl:if>
 </xsl:template>
</xsl:stylesheet>

good luck

cheers
andrew




-----Original Message-----
From: Ryan(_dot_)Asleson(_at_)stpaul(_dot_)com 
[mailto:Ryan(_dot_)Asleson(_at_)stpaul(_dot_)com]
Sent: 15 October 2002 13:31
To: XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Transforming XML to CSV



Hello,

I wish to transform XML which looks like this:

<row>
      <column>Value 1</column>
      <column>Value 2</column>
      <column>Value 3</column>
</row>
<row>
      <column>Value 4</column>
      <column>Value 5</column>
      <column>Value 6</column>
</row>


Into a comma separated values (CSV) format looking like this:

"Value 1","Value 2", "Value 3"
"Value 4","Value 5", "Value 6"

so it can be read in a spreadsheet program.  What XSL will do 
this?  I'm
having trouble because the result is not a hierarchical result.

Thanks!!



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.401 / Virus Database: 226 - Release Date: 09/10/2002
 


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.401 / Virus Database: 226 - Release Date: 09/10/2002
 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>