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

Re: Transforming tabular information to hierarchical

Subject: Re: Transforming tabular information to hierarchical
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Tue, 13 Feb 2007 16:59:15 +0000
Re:  Transforming tabular information to hierarchical
On 2/13/07, Simon Shutter <simon@xxxxxxxxxxx> 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>

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.