Subject: Re: CSV to XML
From: mozer <xmlizer@xxxxxxxxx>
Date: Tue, 13 Nov 2007 20:18:54 +0100
|
Two hints :
* Take a look to <xsl:analyze-string>
* look at http://andrewjwelch.com/code/xslt/csv/csv-to-xml_v2.html
Xmlizer
On Nov 13, 2007 8:13 PM, chun ji <cji_work@xxxxxxxxx> wrote:
>
> Hi all,
>
> I know how to convert this CSV file into Xml one by
> this XSL file in XSLT2.0.
>
> 1. CSV file:
> X1,X1,X2,X2,X2,X3
> a1,a2,a3,a4,a5,a6
>
> 2. XSL file:
> <xsl:stylesheet
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="2.0">
> <xsl:variable name="csv"
> select="unparsed-text('target.csv')"/>
> <xsl:output method="xml" indent="yes"/>
> <xsl:variable name="splitLines"
> select="tokenize($csv, '
> ')"/>
> <xsl:variable name="columnNames"
> select="tokenize($splitLines[1], ',')"/>
>
> <xsl:template match="/">
> <users>
> <xsl:for-each
> select="$splitLines[position() > 2]">
> <xsl:variable name="a"
> select="position()"/>
> <xsl:call-template name="Line">
> <xsl:with-param
> name="columnNames" select="$columnNames" />
> <xsl:with-param name="cells"
> select="$splitLines[$a+1]" />
> </xsl:call-template>
> </xsl:for-each>
> </users>
> </xsl:template>
>
> <xsl:template name="Line">
> <xsl:param name="columnNames"/>
> <xsl:param name="cells"/>
> <xsl:variable name="cellValues"
> select="tokenize($cells, ',')"/>
>
> <xsl:for-each
> select="$cellValues[position()]">
> <xsl:variable name="a"
> select="position()"/>
> <tab>
> <xsl:attribute name="name">
> <xsl:value-of
> select="normalize-space($columnNames[$a])"/>
> </xsl:attribute>
> <sub>
> <xsl:value-of
> select="normalize-space($cellValues[$a])"/>
> </sub>
> </tab>
> </xsl:for-each>
> </xsl:template>
> </xsl:stylesheet>
>
> 3. XML output.
> <?xml version="1.0" encoding="UTF-8"?>
> <users>
> <tab name="X1">
> <sub>a1</sub>
> </tab>
> <tab name="X1">
> <sub>a2</sub>
> </tab>
> <tab name="X2">
> <sub>a3</sub>
> </tab>
> <tab name="X2">
> <sub>a4</sub>
> </tab>
> <tab name="X2">
> <sub>a5</sub>
> </tab>
> <tab name="X3">
> <sub>a6</sub>
> </tab>
> </users>
>
> Now the CSV file has been changed to as:
> "
> X1,,X2,,,X3
> a1,a2,a3,a4,a5,a6
> "
> and is expeting the same XML output... I am blocked.
> So can someone give me some help for this ?
>
>
>
> Thanks a lot
>
>
> Chun
>
>
> ____________________________________________________________________________________
> Be a better pen pal.
> Text or chat with friends inside Yahoo! Mail. See how. http://overview.mail.yahoo.com/
|