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

Modification to The CSV to XML transform XSLT v2 from

Subject: Modification to The CSV to XML transform XSLT v2 from Andrew Welch
From: Marney Cotterill <marney@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 27 Mar 2008 14:52:15 +1100
 Modification to The CSV to XML transform XSLT v2 from
Hi 

Using XML version 1.0, processing with Kernow.

The stylesheet works great! I am completely new to XML and XSLT and was
wondering if it was possible to change the output of the XML from:

<elem name="ColumnName">Value</elem>
to
<ColumnName>Value</ColumnName>

Here is the complete Stylesheet from Andrew Welch:

<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fn="fn" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  version="2.0" exclude-result-prefixes="xs fn">

<xsl:output indent="yes" encoding="US-ASCII" />

<xsl:param name="pathToCSV" select="'file:///c:/csv.csv'" />

<xsl:function name="fn:getTokens" as="xs:string+">
    <xsl:param name="str" as="xs:string" />
    <xsl:analyze-string select="concat($str, ',')"
regex='(("[^"]*")+|[^,]*),'>
        <xsl:matching-substring>
        <xsl:sequence select='replace(regex-group(1), "^""|""$|("")""",
"$1")' />
        </xsl:matching-substring>
    </xsl:analyze-string>
</xsl:function>

<xsl:template match="/" name="main">
    <xsl:choose>
    <xsl:when test="unparsed-text-available($pathToCSV)">
        <xsl:variable name="csv" select="unparsed-text($pathToCSV)" />
        <xsl:variable name="lines" select="tokenize($csv, ' ')"
as="xs:string+" />
        <xsl:variable name="elemNames" select="fn:getTokens($lines[1])"
as="xs:string+" />
        <root>
        <xsl:for-each select="$lines[position() &gt; 1]">
            <row>
            <xsl:variable name="lineItems" select="fn:getTokens(.)"
as="xs:string+" />

            <xsl:for-each select="$elemNames">
                <xsl:variable name="pos" select="position()" />
                <elem name="{.}">
                <xsl:value-of select="$lineItems[$pos]" />
                </elem>
            </xsl:for-each>
            </row>
        </xsl:for-each>
        </root>
    </xsl:when>
    <xsl:otherwise>
        <xsl:text>Cannot locate : </xsl:text><xsl:value-of
select="$pathToCSV" />
    </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

And the output XML I get from my test CSV using the above stylesheet is:

<?xml version="1.0" encoding="US-ASCII"?>
<root>
    <row>
        <elem name="classDay">Monday</elem>
        <elem naem="classTime">11am</elem>
    </row>
    <row>
        <elem name="classDay">Tuesday</elem>
        <elem naem="classTime">12pm</elem>
    </row>
</root>

So hence, what I would like is for the output to be:
....
    <row>
        <classDay>Monday</classDay>
        <classTime>11am</classTime>.......etc

Can we just modify this portion of the stylesheet:

                <elem name="{.}">
                <xsl:value-of select="$lineItems[$pos]" />
                </elem>

Or do I need to run two style sheets using transform () in the second one?

Any direction would be appricated!
Thanks


Marney Cotterill
                   
cracker//brandware

6 Bourke Street
Queens Park 
NSW 2022
Telephone 02 9387 2001
Facsimile 02 9387 2006
marney@xxxxxxxxxxxxxxxxxxxx
www.crackerbrandware.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.