xsl-list
[Top] [All Lists]

[xsl] Modification to The CSV to XML transform XSLT v2 from Andrew Welch

2008-03-26 20:54:11
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(_at_)crackerbrandware(_dot_)com
www.crackerbrandware.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>
--~--