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 10:57:46 -0800
Hi Michael,

I'm note sure how to proceed with your suggestion.  My XML/XSLT experience
is too limited.  Do I have to use an input file like Andrew suggested or are
you assuming that I will have the tablular data in xml format as below?

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<itm id="1" parentID="null" name="One" col1="bla1" col2="bla1" col3="bla1"
/>
<itm id="2" parentID="1" name="Two" col1="bla2" col2="bla2" col3="bla2" />
<itm id="3" parentID="2" name="Three" col1="bla3" col2="bla3" col3="bla3" />
<itm id="4" parentID="3" name="Four" col1="bla4" col2="bla4" col3="bla4" />
<itm id="5" parentID="1" name="Five" col1="bla5" col2="bla5 col3="bla5" />
<itm id="6" parentID="4" name="Six" col1="bla6" col2="bla6" col3="bla6" />
<itm id="7" parentID="4" name="Seven" col1="bla7" col2="bla7" col3="bla7" />
<itm id="8" parentID="7" name="Eight" col1="bla8" col2="bla8" col3="bla8" />
<itm id="9" parentID="3" name="Nine" col1="bla9" col2="bla9" col3="bla9" />
<itm id="10" parentID="9" name="Ten" col1="bla10" col2="bla10" col3="bla10"
/>
</root>

How will the root recursively find all its children?  I tried to create the
template but I'm largely guessing at this point.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:key name="parentKey" match="itm" use="id" />
 <xsl:template match="/">
  <xsl:apply-templates select="key('parentKey',@id)"/>
 </xsl:template>
</xsl:stylesheet>

Simon

-----Original Message-----
From: Michael Kay [mailto:mike@xxxxxxxxxxxx] 
Sent: February 13, 2007 9:13 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] Transforming tabular information to hierarchical


Write a stylesheet that has the same structure as a normal one: start with a
template that processes the root node, and call apply-templates when you
want to process its children. The only difference is that the children are
not physical XML children, but logical children found by using a key. Define
a key for nodes based on the ParentID property, and to find the logical
children of a node, use key('parentKey', @ID).

Michael Kay
http://www.saxonica.com/
 

> -----Original Message-----
> From: Simon Shutter [mailto:simon@xxxxxxxxxxx]
> Sent: 13 February 2007 16:32
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Transforming tabular information to hierarchical
> 
> 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?
> 
> eg source:
> 
> ID	ParentID	Name	col1	col2	col3
> 1	null		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
> 
> desired output:
> 
> <?xml version="1.0" encoding="utf-8"?> <ul id='root' 
> xmlns:bla="http://www.blablabla.com/bla";>
>   <li id='1' bla:col1='bla1' bla:col2='bla1' bla:col3='bla1'>
>     One
>     <ul>
>       <li bla:col1='bla2' bla:col2='bla2' bla:col3='bla2'>
>         Two
>         <ul>
>         <li bla:col1='bla3' bla:col2='bla3' bla:col3='bla3' />Three
>         <ul>
>           <li id='4' bla:col1='bla4' bla:col2='bla4' 
> bla:col3='bla4' />Four
>           <ul>
>             <li id='6' bla:col1='bla6' bla:col2='bla6'
> bla:col3='bla6'>Six</li>
>             <li id='7' bla:col1='bla7' bla:col2='bla7' 
> bla:col3='bla7'>
>               Seven
>               <ul>
>                 <li id='8' bla:col1='bla8' bla:col2='bla8'
> bla:col3='bla8'>Eight</li>
>               </ul>
>             </li>
>           </ul>
>           <li id='9' bla:col1='bla9' bla:col2='bla9' bla:col3='bla9'>
>             Nine
>             <ul>
>               <li id='10' bla:col1='bla10' bla:col2='bla10'
> bla:col3='bla10'>Ten</li>
>             </ul>
>           </li>
>         </ul>
>       </ul>
>       </li>
>       <li id='5' bla:col1='bla5' bla:col2='bla5' 
> bla:col3='bla5'>Five</li>
>     </ul>
>   </li>
> </ul>
> 
> 
> Thanks, Simon

Current Thread