It is necessary to order the elements from the mixed order. Ordering is done using the recursion method. Two conditions must be met that while are not implemented in the code:
This is a change to your root template. Your other templates stay the same. Hopefully, this will help.
<xsl:template match="/">
<xsl:variable name="chains">
<xsl:apply-templates select="root/object[@STATUS='1']" mode="ancestors"/>
</xsl:variable>
<xsl:variable name="chainList" select="exslt:node-set($chains)"/>
<xsl:variable name="objects">
<xsl:for-each select="$chainList/object">
<element>
<position>
<xsl:value-of select="position()"/>
</position>
<numberOfObjects>
<xsl:value-of select="count(.//object)+1"/>
</numberOfObjects>
</element>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="objectList" select="exslt:node-set($objects)"/>
<xsl:variable name="sortedObjects">
<xsl:for-each select="$objectList/element">
<xsl:sort select="numberOfObjects" order="descending" data-type="number"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="sortedObjectList" select="exslt:node-set($sortedObjects)"/>
<xsl:variable name="maxObjects" select="$sortedObjectList/element[1]/numberOfObjects"/>
<root>
<xsl:for-each select="$objectList/element[numberOfObjects = $maxObjects]">
<xsl:variable name="position" select="position"/>
<xsl:copy-of select="$chainList/object[number($position)]"/>
</xsl:for-each>
</root>
</xsl:template>
Only how to choose the longest one chain?
Consider the following much simplified example.
IMPORTANT
In this example it is assumed that a parent object
can have at most one child object
. This allows us to begin the recursion with ancestor objects (objects that do not have a parent) and work downwards. Otherwise we would have to create a separate chain for every leaf object (an object that does not have any child objects) and recurse upwards from there.
XML
<root>
<object id="a"/>
<object id="b"/>
<object id="c"/>
<object id="aa" parent-id="a"/>
<object id="bb" parent-id="b"/>
<object id="cc" parent-id="c"/>
<object id="aaa" parent-id="aa"/>
<object id="bbb" parent-id="bb"/>
<object id="ccc" parent-id="cc"/>
<object id="bbbb" parent-id="bbb"/>
<object id="cccc" parent-id="ccc"/>
<object id="bbbbb" parent-id="bbbb"/>
</root>
XSLT 1.0 (+ node-set function)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="child" match="object" use="@parent-id" />
<xsl:template match="/root">
<!-- generate chains -->
<xsl:variable name="chains">
<xsl:apply-templates select="object[not(@parent-id)]"/>
</xsl:variable>
<!-- find the longest chain -->
<xsl:for-each select="exsl:node-set($chains)/object">
<xsl:sort select="count(descendant::object)" data-type="number" order="descending"/>
<xsl:if test="position() =1 ">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="object">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="key('child', @id)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
After the first pass, the $chains
variable will contain:
<object id="a">
<object id="aa" parent-id="a">
<object id="aaa" parent-id="aa"/>
</object>
</object>
<object id="b">
<object id="bb" parent-id="b">
<object id="bbb" parent-id="bb">
<object id="bbbb" parent-id="bbb">
<object id="bbbbb" parent-id="bbbb"/>
</object>
</object>
</object>
</object>
<object id="c">
<object id="cc" parent-id="c">
<object id="ccc" parent-id="cc">
<object id="cccc" parent-id="ccc"/>
</object>
</object>
</object>
After sorting the chains by their length (i.e. the count of descendant objects) and selecting the longest one we will get:
Result
<?xml version="1.0" encoding="UTF-8"?>
<object id="b">
<object id="bb" parent-id="b">
<object id="bbb" parent-id="bb">
<object id="bbbb" parent-id="bbb">
<object id="bbbbb" parent-id="bbbb"/>
</object>
</object>
</object>
</object>
Hint: with recursion working downwards, it is very easy to use a template parameter to pass a common value from the ancestor to all its descendants.