[xsl] key() returning nodes in undesired order.

Subject: [xsl] key() returning nodes in undesired order.
From: Elliot Schlegelmilch <elliot@xxxxxxxxxxxxxxx>
Date: Sat, 12 Nov 2005 14:09:46 -0700
I'm attempting to simplify and reduce the size of my xml. Instead of repeating items many many times, I'm providing a reference. Being a good beginner xsl programmer, I made a key for each 'b' so i can refer to them simply by key('all-b', /set/x/a/reference) and have a very handy node set of b's.
<xsl:key name="all-b" match="/set/y/b" use="reference"/>


This part all works fine. However, the problem is when the references refer to them in an order which differs than how they are contained in y. So "key('all-b', /set/x/a/reference)" returns the B nodes I'm after, but not in the order which they are in A. Is there such a way to get them in the order which I desire?

Unfortunately, having the xml in the 'correct order' before I process isn't an option, and neither is adding sort criteria for each B so I can sort the node set before I use it.

The only way I can think to do it now is individually, like:
<xsl:for-each select="/set/x/a/reference">
<xsl:variable name="x" select="key('all-b', .)"/>
...
</xsl:for-each>




<set>


<x>
<a>
<reference>reference2</reference>
<reference>reference1</reference>
</a>
</x>

<y>
<b>
<reference>reference1</reference>
<c>other data</c>
</b>
<b>
<reference>reference2</reference>
<c>other data</c>
</b>
</y>

</set>

Current Thread