xsl-list
[Top] [All Lists]

RE: [xsl] Transforming tabular information to hierarchical

2007-02-13 10:56:41
Thanks, Andrew

I'm relatively new to XML.  If I want to apply your converter, what input
file do I give the processor?

Simon

-----Original Message-----
From: Andrew Welch [mailto:andrew(_dot_)j(_dot_)welch(_at_)gmail(_dot_)com] 
Sent: February 13, 2007 8:59 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Transforming tabular information to hierarchical

On 2/13/07, Simon Shutter <simon(_at_)schemax(_dot_)com> wrote:
If I have a tabular data set that defines parent-child relationships, 
is it possible to transform this into a hierarchical tree format using
XSLT?

Here's a csv to XML converter I wrote the other day, feel free to convert it
to use tabs, or adjust your input to be comma delimited.


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

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

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

<xsl:template 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,
'&#xa;')" as="xs:string+"/>
                        <xsl:variable name="elemNames"
select="tokenize($lines[1], ',')"
as="xs:string+"/>
                        <root>
                                <xsl:for-each select="$lines[position() >
1]">
                                        <row>
                                                <xsl:variable
name="lineItems"
select="tokenize(normalize-space(.), ',')" as="xs:string+"/>
                                                <xsl:for-each
select="$elemNames">
                                                        <xsl:variable
name="pos" select="position()"/>
                                                        <xsl:element
name="{normalize-space(.)}">
        
<xsl:value-of select="$lineItems[$pos]"/>
                                                        </xsl:element>
                                                </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>

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

<Prev in Thread] Current Thread [Next in Thread>