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:>
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
XSLT 1.0 (+ node-set function)
After the first pass, the $chains variable will contain:
After sorting the chains by their length (i.e. the count of descendant objects) and selecting the longest one we will get:
Result
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.