xsl-list
[Top] [All Lists]

Re: [xsl] Creating Hierarchy

2008-10-12 14:46:12
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(_at_)CraneSoftwrights(_dot_)com
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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>