OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: [xml-dev] Converting xml to csv with xslt

[ Lists Home | Date Index | Thread Index ]

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>




 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS