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

CSV to XML

Subject: CSV to XML
From: chun ji <cji_work@xxxxxxxxx>
Date: Tue, 13 Nov 2007 11:13:01 -0800 (PST)
 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, '&#xA;')"/>
        <xsl:variable name="columnNames"
select="tokenize($splitLines[1], ',')"/>

        <xsl:template match="/">
             <users>
                <xsl:for-each
select="$splitLines[position() &gt; 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/

Current Thread

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