xsl-list
[Top] [All Lists]

RE: Transforming XML to CSV

2002-10-15 06:05:33
The following transform will do what you want:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="text"/>

<xsl:template match="/">
        <xsl:apply-templates select="root" />
</xsl:template>

<xsl:template match="root">
        <xsl:apply-templates select="row" />
</xsl:template>

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

<xsl:template match="column">
        <xsl:text>"</xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>"</xsl:text>
        <xsl:if test="position() != last()">
                <xsl:text>,</xsl:text>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>

This assumes the document root in <root>.

The main thing here is the use of the text output method, along with the
&#xA; (New line) character.  The xsl:if is used to determine if a comma is
needed after a column.

I hope this helps,
        Steve

-----Original Message-----
From: Ryan(_dot_)Asleson(_at_)stpaul(_dot_)com 
[mailto:Ryan(_dot_)Asleson(_at_)stpaul(_dot_)com]
Sent: Tuesday, October 15, 2002 8:31 AM
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

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



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