[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: "Simon Shutter" <simon@xxxxxxxxxxx>
Date: Tue, 13 Feb 2007 23:32:27 -0800
RE:  Transforming tabular information to hierarchical
I'm struggling with this problem and if any kind soul could provide some
guidance I'd be very grateful.  I began with a table (adjacency list) of csv
values that I transformed using Andrew Welch's stylesheet suggestion.  This
provided me with an XML representation of the data but I have been unable to
use Michael's suggestion for xsl:key to produce the desired output.  I have
managed to associate each child node with its parent but that's as far as
I've been able to get.

Simon

The latest I have is...

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="parentKey" match="row" use="ChildID"/>

  <xsl:template match="root">
    <html>
      <head></head>
        <body>
          <ul>
            <xsl:apply-templates select="row" />
          </ul>
        </body>
    </html>
  </xsl:template>
  
  <xsl:template match="ChildID">
    <xsl:value-of select="key('parentKey',@pid)/ChildID"/>
  </xsl:template>
</xsl:stylesheet> 

Which transforms 

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


into

<html>
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <ul>
    
    One
    bla1
    bla1
    bla1
  
    
    1
    Two
    bla2
    bla2
    bla2
  
    
    2
    Three
    bla3
    bla3
    bla3
  
    
    3
    Four
    bla4
    bla4
    bla4
  
    
    1
    Five
    bla5
    bla5
    bla5
  
    
    4
    Six
    bla6
    bla6
    bla6
  
    
    4
    Seven
    bla7
    bla7
    bla7
  
    
    7
    Eight
    bla8
    bla8
    bla8
  
    
    3
    Nine
    bla9
    bla9
    bla9
  
    
    9
    Ten
    bla10
    bla10
    bla10
  </ul>
  </body>
</html>

-----Original Message-----
From: Michael Kay [mailto:mike@xxxxxxxxxxxx] 
Sent: February 13, 2007 9:13 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE:  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:  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

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.