Sorting subtrees based on their maximum depth.

Subject: Sorting subtrees based on their maximum depth.
From: Mike Trotman <mike.trotman@xxxxxxxxxxxxx>
Date: Sat, 26 Jun 2004 17:14:23 +0100
Hi.

I have XML documents containing hierarchical ly recursive SUBTREES.
(eg:
<XML>
....
<SUBTREE>
...<SUBTREE>
......<SUBTREE>
.....etc....
......</SUBTREE>
......<SUBTREE>
.....etc....
......</SUBTREE>
....</SUBTREE>
...<SUBTREE>
......<SUBTREE>
.....etc....
......</SUBTREE>
....</SUBTREE>
</SUBTREE>
...
</XML>

I need to recursively sort these SUBTREES based on the maximum depth of descendant elements that they contain.

I am currently doing this in two passes.
In the 1st pass I compute and assign a 'DEPTH' attribute to each SUBTREE using:


<xsl:variable name='curr_depth' select='count(ancestor::*'/>
<xsl:variable name='DEPTH'>
<xsl:for-each select='.//*[not(child::*)]'>
<xsl:sort select='count(ancestor::*) - $curr_depth' data-type='number' order='descending'/>
<xsl:if test='position() = 1'><xsl:value-of select='count(ancestor::*) - $curr_depth'/></xsl:if>
</xsl:for-each>
</xsl:variable>


and then I reprocess the new document using normal sorting on the '@DEPTH' attribute.

I wondered if there is a way to do this in one-pass - and to avoid doing those ancestor counts twice.

Any help greatly appreciated.

--
Datalucid Limited
8 Eileen Road
South Norwood
London SE25 5EJ
/ tel :0208-239-6810
mob: 0794-725-9760
email: mike.trotman@xxxxxxxxxxxxx
/
UK Co. Reg:   	4383635
VAT Reg.:   	798 7531 60



Current Thread