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

Re: Creating Hierarchy

Subject: Re: Creating Hierarchy
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 12 Oct 2008 14:45:42 -0400
Re:  Creating Hierarchy
At 2008-10-12 18:44 +0100, Rowan Sylvester-Bradley wrote:
I have an XML file that represents a tree of data elements, similar to a directory tree. The structure of the file has every node in the tree represented by a <node> element, all children of the root, with the hierarchy defined by a <level> element within each node. So the <node> with <level> = 0 is the root of the tree, its children have <level> = 1, their children have <level> = 2 etc.

In my proposed solution I'm assuming this is monotonically increasing.


Here's an example:
...
I want to transform this into a hierarchical file like this:
...
How do I write a stylesheet to do this?

Not sure aspect of writing the stylesheet you are asking about. Which version of XSLT to use (2.0 is easiest for this problem)? Which construct to use (xsl:for-each-group)? How to handle the arbitrary number of levels (recursion)?


A very similar question was already asked this week needing many of the same principles in the solution.

An answer is below, but I'm not sure which of your questions it is answering.

I hope this helps.

. . . . . . . . . . Ken

T:\ftemp>type rowan.xml
<mytree>
 <node>
   <name>Root of my tree</name>
   <level>0</level>
 </node>
 <node>
   <name>Child of root</name>
   <level>1</level>
 </node>
 <node>
   <name>Another child of root</name>
   <level>1</level>
 </node>
 <node>
   <name>Grandchild of root</name>
   <level>2</level>
 </node>
 <node>
   <name>Yet another child of root</name>
   <level>1</level>
 </node>
</mytree>

T:\ftemp>xslt2 rowan.xml rowan.xsl con
<?xml version="1.0" encoding="UTF-8"?>
<newnode>
   <name>Root of my tree</name>
   <newnode>
      <name>Child of root</name>
   </newnode>
   <newnode>
      <name>Another child of root</name>
      <newnode>
         <name>Grandchild of root</name>
      </newnode>
   </newnode>
   <newnode>
      <name>Yet another child of root</name>
   </newnode>
</newnode>
T:\ftemp>type rowan.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

<xsl:output indent="yes"/>

<!--first time around start at level 0 and use all nodes-->
<xsl:template match="mytree" name="next-level">
  <xsl:param name="level" select="0"/>
  <xsl:param name="nodes" select="node"/>

<xsl:if test="$nodes/level[.=$level]">
<!--then group the nodes at this level-->
<xsl:for-each-group select="$nodes"
group-starting-with="node[level[.=$level]]">
<!--act on the first of each group only, passing others to next level-->
<newnode>
<xsl:copy-of select="name"/>
<!--group at the next level assuming monotonically increasing level-->
<xsl:call-template name="next-level">
<xsl:with-param name="level" select="$level + 1"/>
<xsl:with-param name="nodes" select="current-group()[position()>1]"/>
</xsl:call-template>
</newnode>
</xsl:for-each-group>
</xsl:if>
</xsl:template>


</xsl:stylesheet>

T:\ftemp>

--
Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video sample lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg
Video course overview:  http://www.youtube.com/watch?v=VTiodiij6gE
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

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.