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

Subject: RE: [xsl] key() returning nodes in undesired order.
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sat, 12 Nov 2005 21:43:41 -0000
key() is defined to return its results in document order, with duplicates
eliminated. If you want a different order, you have to call it multiple
times, once for each key value, as you suggest.

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: Elliot Schlegelmilch [mailto:elliot@xxxxxxxxxxxxxxx] 
> Sent: 12 November 2005 21:10
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] key() returning nodes in undesired order.
> 
> 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