[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Converting xml to csv with xslt


xml to csv
Jerry Lake wrote:
> I'm still working on my xslt. Can someone give me an example or point me
> in the right direction to convert my XML to CSV

A good place to ask XSLT questions is http://www.mulberrytech.com/xsl/xsl-list/, but you 
can still ask XSLT questions here and get a response or two.

For a general solution, see Sal Mangano’s XSLT Cookbook (O’Reilly, 2002), pages 155–170. 
http://www.oreilly.com/catalog/xsltckbk/; you can download an archive of all his examples 
from there.

The following is not a generalized solution, but it may prove instructive. Given the 
document inventory.xml, you can apply csv.xsl (below), and get csv output.

C:\Hacks\examples>cat inventory.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<Inventory>
  <Line>
   <ItemNumber>line</ItemNumber>
   <Description>desc</Description>
   <Quantity>quan</Quantity>
   <Date>date</Date>
  </Line>
  <Line>
   <ItemNumber>1</ItemNumber>
   <Description>Oak chairs</Description>
   <Quantity>6</Quantity>
   <Date>31 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>2</ItemNumber>
   <Description>Dining tables</Description>
   <Quantity>1</Quantity>
   <Date>31 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>3</ItemNumber>
   <Description>Folding chairs</Description>
   <Quantity>4</Quantity>
   <Date>29 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>4</ItemNumber>
   <Description>Couch</Description>
   <Quantity>1</Quantity>
   <Date>31 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>5</ItemNumber>
   <Description>Overstuffed chair</Description>
   <Quantity>1</Quantity>
   <Date>30 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>6</ItemNumber>
   <Description>Ottoman</Description>
   <Quantity>1</Quantity>
   <Date>31 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>7</ItemNumber>
   <Description>Floor lamp</Description>
   <Quantity>1</Quantity>
   <Date>20 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>8</ItemNumber>
   <Description>Oak bookshelves</Description>
   <Quantity>1</Quantity>
   <Date>31 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>9</ItemNumber>
   <Description>Computer desk</Description>
   <Quantity>1</Quantity>
   <Date>31 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>10</ItemNumber>
   <Description>Folding tables</Description>
   <Quantity>3</Quantity>
   <Date>31 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>11</ItemNumber>
   <Description>Oak writing desk</Description>
   <Quantity>1</Quantity>
   <Date>28 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>12</ItemNumber>
   <Description>Table lamps</Description>
   <Quantity>5</Quantity>
   <Date>26 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>13</ItemNumber>
   <Description>Pine night tables</Description>
   <Quantity>3</Quantity>
   <Date>26 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>14</ItemNumber>
   <Description>Oak dresser</Description>
   <Quantity>1</Quantity>
   <Date>30 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>15</ItemNumber>
   <Description>Pine dressers</Description>
   <Quantity>1</Quantity>
   <Date>31 Dec 2004</Date>
  </Line>
  <Line>
   <ItemNumber>16</ItemNumber>
   <Description>Pine armoire</Description>
   <Quantity>1</Quantity>
   <Date>31 Dec 2004</Date>
  </Line>
</Inventory>

C:\Hacks\examples>cat csv.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

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

<xsl:template match="Line">
  <xsl:for-each select="*">
   <xsl:value-of select="."/>
   <xsl:if test="position() != last()">
    <xsl:value-of select="','"/>
   </xsl:if>
  </xsl:for-each>
  <xsl:text>&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>

C:\Hacks\examples>xalan inventory.xml csv.xsl
line,desc,quan,date
1,Oak chairs,6,31 Dec 2004
2,Dining tables,1,31 Dec 2004
3,Folding chairs,4,29 Dec 2004
4,Couch,1,31 Dec 2004
5,Overstuffed chair,1,30 Dec 2004
6,Ottoman,1,31 Dec 2004
7,Floor lamp,1,20 Dec 2004
8,Oak bookshelves,1,31 Dec 2004
9,Computer desk,1,31 Dec 2004
10,Folding tables,3,31 Dec 2004
11,Oak writing desk,1,28 Dec 2004
12,Table lamps,5,26 Dec 2004
13,Pine night tables,3,26 Dec 2004
14,Oak dresser,1,30 Dec 2004
15,Pine dressers,1,31 Dec 2004
16,Pine armoire,1,31 Dec 2004

C:\Hacks\examples>

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.