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

Converting CSV to XML without hardcoding schema detail

Subject: Converting CSV to XML without hardcoding schema details in xsl
From: "Pantvaidya, Vishwajit" <vpantvai@xxxxxxxxxxxxx>
Date: Wed, 21 Jun 2006 00:16:46 -0700
xsl xml to csv indent
Hello,

I am trying to convert a CSV datafile into XMl format.
The headers for the CSV data are in a file header.csv e.g. Field1,Field2
The data is in a file Data.csv e.g.
Value11,Value12
Value21,Value22

I need to convert the CSV data into xml output by creating xml elements
using the names in the csv header and taking the corresponding values from
the data file, so that I get an xml as follows:

<doc>
<line>
<Field1>Value11</Field1>
<Field2>Value12</Field2>
</line>
<line>
<Field1>Value21</Field1>
<Field2>Value22</Field2>
</line>
</doc>

I was trying to see if I can do this without hardcoding the header names in
the xsl. I reached upto the point where my xsl looks as below:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:op="http://www.w3.org/2001/12/xquery-operators"
    xmlns:xf="http://www.w3.org/2001/12/xquery-functions" version="2.0">

    <xsl:output  name="xmlFormat" method="xml" indent="yes"
omit-xml-declaration="yes"/>

    <xsl:variable name="source1" select="'data.csv'"/>
    <xsl:variable name="elementNamesList" select="'Header.csv'"/>
    <xsl:variable name="encoding" select="'iso-8859-1'"/>

    <xsl:variable name="elementNames"
select="tokenize(unparsed-text($elementNamesList,$encoding),',')"/>
    <xsl:variable name="src">
        <doc>
            <xsl:for-each
select="tokenize(unparsed-text($source1,$encoding), '\r?\n')">
                <line>
                    <xsl:for-each select="tokenize(., ',')">
                        &lt;<xsl:value-of
select="op:item-at($elementNames,index-of(?parent of current
node?,.))"/>&gt;
                            <xsl:value-of select="."/>
                            &lt;/<xsl:value-of
select="item-at($elementNames,3)"/>&gt;
                    </xsl:for-each>
                </line>
            </xsl:for-each>
        </doc>
    </xsl:variable>

    <xsl:template match="/">
        <xsl:result-document format = "xmlFormat" href = "src1.xml">
            <xsl:copy-of select="$src"/>
        </xsl:result-document>
    </xsl:template>

</xsl:stylesheet>

In the yet-incomplete statement <xsl:value-of
select="op:item-at($elementNames,index-of(?parent of current node?,.))"/>, I
am trying to generate an xml element with the Nth field name from the
headers name list for the Nth field value. Couple of issues/questions here:

- I am getting the error "Cannot find a matching 2-argument
function named {http://www.w3.org/2001/12/xquery-operators}item-at()" when I
try to validate the xsl. What could be the reason?

- How can I get the ?parent of current node? Needed to compute the
index of the current data in the data record?

- Is there any other better way to do it? Any way that I can do the
same using xsl:element?

In general, is this the only/best way or is there any other better way to
achieve the same goal?


Thanks and Regards,

Vish.

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-2011 All Rights Reserved.