RE: [xsl] Transforming tabular information to hierarchical

Subject: RE: [xsl] Transforming tabular information to hierarchical
From: "Simon Shutter" <simon@xxxxxxxxxxx>
Date: Tue, 13 Feb 2007 14:05:27 -0800
I just installed Saxon.NET (Thank you Michael Kay, Pieter Siegers Kort, M.
David Peterson, Jeroen Frijters et al.) and ran Andrew Welch's stylesheet
with a csv-formatted input file using the command-line instruction (Thank
you Martin and Abel):

Transform -it main aw.xsl

ID,ParentID,Name,col1,col2,col3
1,99,One,bla1,bla1,bla1
2,1,Two,bla2,bla2,bla2
3,2,Three,bla3,bla3,bla3
4,3,Four,bla4,bla4,bla4
5,1,Five,bla5,bla5,bla5
6,4,Six,bla6,bla6,bla6
7,4,Seven,bla7,bla7,bla7
8,7,Eight,bla8,bla8,bla8
9,3,Nine,bla9,bla9,bla9
10,9,Ten,bla10,bla10,bla10

I got the output below, which is great but what I was hoping for was some
help with the transformation to the output I had in my original post ie
reflecting the recursive parent-child relationships.  Michael Kay provided
some pointers but I don't know (yet) enough about XSLT to be able to create
the stylesheet he was conceptualizing.  Any further assistance on this would
be hugely appreciated.

Regards,

Simon


<?xml version="1.0" encoding="US-ASCII"?>
<root>
   <row>
      <ID>1</ID>
      <ParentID>99</ParentID>
      <Name>One</Name>
      <col1>bla1</col1>
      <col2>bla1</col2>
      <col3>bla1</col3>
   </row>
   <row>
      <ID>2</ID>
      <ParentID>1</ParentID>
      <Name>Two</Name>
      <col1>bla2</col1>
      <col2>bla2</col2>
      <col3>bla2</col3>
   </row>
   <row>
      <ID>3</ID>
      <ParentID>2</ParentID>
      <Name>Three</Name>
      <col1>bla3</col1>
      <col2>bla3</col2>
      <col3>bla3</col3>
   </row>
   <row>
      <ID>4</ID>
      <ParentID>3</ParentID>
      <Name>Four</Name>
      <col1>bla4</col1>
      <col2>bla4</col2>
      <col3>bla4</col3>
   </row>
   <row>
      <ID>5</ID>
      <ParentID>1</ParentID>
      <Name>Five</Name>
      <col1>bla5</col1>
      <col2>bla5</col2>
      <col3>bla5</col3>
   </row>
   <row>
      <ID>6</ID>
      <ParentID>4</ParentID>
      <Name>Six</Name>
      <col1>bla6</col1>
      <col2>bla6</col2>
      <col3>bla6</col3>
   </row>
   <row>
      <ID>7</ID>
      <ParentID>4</ParentID>
      <Name>Seven</Name>
      <col1>bla7</col1>
      <col2>bla7</col2>
      <col3>bla7</col3>
   </row>
   <row>
      <ID>8</ID>
      <ParentID>7</ParentID>
      <Name>Eight</Name>
      <col1>bla8</col1>
      <col2>bla8</col2>
      <col3>bla8</col3>
   </row>
   <row>
      <ID>9</ID>
      <ParentID>3</ParentID>
      <Name>Nine</Name>
      <col1>bla9</col1>
      <col2>bla9</col2>
      <col3>bla9</col3>
   </row>
   <row>
      <ID>10</ID>
      <ParentID>9</ParentID>
      <Name>Ten</Name>
      <col1>bla10</col1>
      <col2>bla10</col2>
      <col3>bla10</col3>
   </row>
</root>

-----Original Message-----
From: Andrew Welch [mailto:andrew.j.welch@xxxxxxxxx] 
Sent: February 13, 2007 8:59 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] 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