xsl-list
[Top] [All Lists]

Re: [xsl] CSV to XML

2007-11-13 12:28:18
Forgive me if this response is due to my extreme ignorance, but I'm
not certain you can use XSL to translate CSV -> XML. Of course, you
could do the reverse.

-Steve

On Nov 13, 2007 2:13 PM, chun ji <cji_work(_at_)yahoo(_dot_)com> 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/

--~------------------------------------------------------------------
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>
--~--