[xsl] Sorting problem

Subject: [xsl] Sorting problem
From: "Joe Fawcett" <joefawcett@xxxxxxxxxxx>
Date: Wed, 31 Aug 2005 11:16:26 +0100
Dear All

Supposing I have a document such as below =>
<root>
 <item type="A" subType="1"/>
 <item type="A" subType="2"/>
 <item type="A" subType="4"/>
 <item type="B" subType="2"/>
 <item type="B" subType="3"/>
 <item type="D" subType="1"/>
 <item type="D" subType="2"/>
 <item type="D" subType="3"/>
 <item type="D" subType="4"/>
 <item type="E" subType="2"/>
 <item type="E" subType="4"/>
</root>

This is sorted by @type and then @subType, the subType can be any positive number, not just an integer.

I need to select the first occurrence of each item, based on its @subType, then the second, then the third etc, keeping them in alphabetical order based on @type =>

<root>
 <item type="A" subType="1"/>
 <item type="B" subType="2"/>
 <item type="D" subType="1"/>
 <item type="E" subType="2"/>
 <item type="A" subType="2"/>
 <item type="B" subType="3"/>
 <item type="D" subType="2"/>
 <item type="E" subType="4"/>
 <item type="A" subType="4"/>
 <item type="D" subType="3"/>
 <item type="D" subType="4"/>
</root>

This is the result of converting a RTF via xx:node-set, I am stuck with version 1.0, so as far as I understand I cannot use keys in the solution.


I have produced a working solution but it seems inelegant. I first select a distinct list of @type using pre-Muenchian methods where the type doesn't have a preceding-sibling of the same value. I then call a template by name recursively whereby the first pass iterates through the distinct list using for-each and selects the the first <item> that matches the type. This template is then called again and does the same for the second matching item and continues in the same fashion. The recursion stops after being called sufficient times to ensure each item is matched once.


Can anyone suggest anything more appealing?

Joe

Current Thread