You could try the following. It's a bit messy because group-starting-with
only works on nodes, not atomic values, so you have to wrap the strings in
nodes first.
<xsl:variable name="columnNamesAsElements" as="element()*">
<xsl:for-each select="$columnNames">
<column><xsl:value-of select="."/></column>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="expandedColumnNames" as="xs:string()*">
<xsl:for-each-group select="$columnNamesAsElements"
group-starting-with="column[string(.)]">
<xsl:for-each select="current-group()">
<xsl:sequence select="string(current-group()[1])"/>
</xsl:for-each>
</xsl:for-each-group>
</xsl:variable>
then use expandedColumnNames where you used columnNames before.
Michael Kay
http://www.saxonica.com/
-----Original Message-----
From: chun ji [mailto:cji_work(_at_)yahoo(_dot_)com]
Sent: 13 November 2007 19:13
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] CSV to XML
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/
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail:
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--