xsl-list
[Top] [All Lists]

Sorting subtrees based on their maximum depth.

2004-06-26 09:14:23
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(_dot_)trotman(_at_)datalucid(_dot_)com
/
UK Co. Reg:     4383635
VAT Reg.:       798 7531 60




<Prev in Thread] Current Thread [Next in Thread>
  • Sorting subtrees based on their maximum depth., Mike Trotman <=