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

RE: How to convert flat data to hierarchical data

Subject: RE: How to convert flat data to hierarchical data
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Mon, 26 Apr 2004 19:40:05 +0100
element sk
The trick to this is to write a stylesheet with template rules that use
<xsl:apply-templates> to process the children of the current element in the
usual way, but instead of selecting the physical children of an element, you
select its logical children, based on following the relationships in your
data. The only other thing needed is to start processing at the logical root
of the tree, which in your example appears to be (always?) the first
EmployeeGroup.

Michael Kay 

> -----Original Message-----
> From: Hansen, John [mailto:John.Hansen@xxxxxxxxxx] 
> Sent: 26 April 2004 17:00
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  How to convert flat data to hierarchical data
> 
> Is there a simple stylesheet that will transform the flat data shown
> below into the hierarchical form that is at the end of this email?  I
> hate to ask for your help but I'm stumped at the moment.  
> EmployeeGroup
> elements, while presented in a flat form in the initial XML 
> output need
> to be nested based on their EmployeeGroupTree child element's SK
> attribute and their Parent child element's SK attribute (or xsi:nil
> attribute).  Each level in the hierarchy can have zero or more child
> EmployeeGroup elements.  The root of each tree is the 
> EmployeeGroup with
> a <Parent xsi:nil="true"/> child.
> 
> <!-- Original XML with flat structure -->
> 
> <EmployeeGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
> 
> <EmployeeGroup SK="1">
> <EmployeeGroupTree SK="1"/>
> <Code>AB_GP1</Code>
> <Parent xsi:nil="true"/>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="2">
> <EmployeeGroupTree SK="2"/>
> <Code>AB_GP2</Code>
> <Parent xsi:nil="true"/>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="3">
> <EmployeeGroupTree SK="1"/>
> <Code>AB1_G1</Code>
> <Parent SK="1"/>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="4">
> <EmployeeGroupTree SK="2"/>
> <Code>AB2_G1</Code>
> <Parent SK="2"/>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="5">
> <EmployeeGroupTree SK="3"/>
> <Code>CATGRY</Code>
> <Parent xsi:nil="true"/>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="6">
> <EmployeeGroupTree SK="4"/>
> <Code>CSV   </Code>
> <Parent xsi:nil="true"/>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="7">
> <EmployeeGroupTree SK="5"/>
> <Code>HEAP  </Code>
> <Parent xsi:nil="true"/>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="8">
> <EmployeeGroupTree SK="5"/>
> <Code>H1    </Code>
> <Parent SK="7"/>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="9">
> <EmployeeGroupTree SK="5"/>
> <Code>H10   </Code>
> <Parent SK="7"/>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="10">
> <EmployeeGroupTree SK="5"/>
> <Code>H11   </Code>
> <Parent SK="7"/>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="11">
> <EmployeeGroupTree SK="5"/>
> <Code>H12   </Code>
> <Parent SK="7"/>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="12">
> <EmployeeGroupTree SK="5"/>
> <Code>H13   </Code>
> <Parent SK="7"/>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="13">
> <EmployeeGroupTree SK="1"/>
> <Code>TEST  </Code>
> <Parent SK="3"/>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="14">
> <EmployeeGroupTree SK="1"/>
> <Code>TEST2 </Code>
> <Parent SK="13"/>
> </EmployeeGroup>
> 
> </EmployeeGroups>
> 
> <!-- desired hierarchical output of XSL transformation -->
> 
> <EmployeeGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
> 
> <EmployeeGroup SK="1">
>   <EmployeeGroupTree SK="1"/>
>   <Code>AB_GP1</Code>
>   <EmployeeGroup SK="3">
>     <EmployeeGroupTree SK="1"/>
>     <Code>AB1_G1</Code>
>     <EmployeeGroup SK="13">
>       <EmployeeGroupTree SK="1"/>
>       <Code>TEST </Code>
>       <EmployeeGroup SK="14">
>         <EmployeeGroupTree SK="1"/>
>         <Code>TEST2 </Code>
>       </EmployeeGroup>
>     </EmployeeGroup>
>   </EmployeeGroup>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="2">
>   <EmployeeGroupTree SK="2"/>
>   <Code>AB_GP2</Code>
>   <EmployeeGroup SK="4">
>     <EmployeeGroupTree SK="2"/>
>     <Code>AB2_G1</Code>
>   </EmployeeGroup>
> </EmployeeGroup>
> 
> 
> <EmployeeGroup SK="5">
>   <EmployeeGroupTree SK="3"/>
>   <Code>CATGRY</Code>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="6">
>   <EmployeeGroupTree SK="4"/>
>   <Code>CSV   </Code>
> </EmployeeGroup>
> 
> <EmployeeGroup SK="7">
>   <EmployeeGroupTree SK="5"/>
>   <Code>HEAP  </Code>
>   <EmployeeGroup SK="8">
>     <EmployeeGroupTree SK="5"/>
>     <Code>H1    </Code>
>   </EmployeeGroup>
>   <EmployeeGroup SK="9">
>     <EmployeeGroupTree SK="5"/>
>     <Code>H10   </Code>
>   </EmployeeGroup>
>   <EmployeeGroup SK="10">
>     <EmployeeGroupTree SK="5"/>
>     <Code>H11   </Code>
>   </EmployeeGroup>
>   <EmployeeGroup SK="11">
>     <EmployeeGroupTree SK="5"/>
>     <Code>H12   </Code>
>   </EmployeeGroup>
>   <EmployeeGroup SK="12">
>     <EmployeeGroupTree SK="5"/>
>     <Code>H13   </Code>
>   </EmployeeGroup>
> </EmployeeGroup>
> 
> </EmployeeGroups>

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.