Re: [xsl] Sorting and Grouping revisited

Subject: Re: [xsl] Sorting and Grouping revisited
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 19 Apr 2001 09:05:54 +0100
> 'am I crazy for expecting XSLT to be able to handle sorting and
> grouping gracefully?'

You're not crazy to ask that (but that doesn't necessarily mean that
you are not crazy, and it certainly doesn't mean that XSLT handles
sorting and grouping gracefully).

> 'There is no simple way of doing grouping in XSLT. In some cases it can be
> done, painfully and generally rather slowly, by testing each item to see if
> it is the same as the previous item. 

That was probably written before Steve Meunch's insight into using keys
to make this more efficient (but in some ways even less graceful).
There's more on this sorting technique at Jeni's site.

> It seems to me the best bet for accomplishing what I need to do is to
> restructure my XML.  As another member of this group noted, this is changing
> the data subtly for a presentation reason.  This seems wrong.

It is wrong:-)

> In short, it seems as if the shortcoming is simply the lack of a way to
> access elements of a sorted node list (as opposed to in document-order).  I
> have read many archived messages from this group, but am I missing
> something?

Partly you are missing XSLT 2.0: the XSLT2 reequirements document
explicitly lists this as one of the main areas to be improved,
but there isn't even a draft spec for XSLT2, so you are not the only one
missing that.

However all is not lost. You can access the sorted list by doing

<xsl:variable name="x">

then use it as in

<xsl:apply-templates select="exslt:node-set($x)"/>

where exslt: is the prefix for your processors extension namespace.
All the major XSLT systems (I think) offer a node-set() function for
this use.


This message has been checked for all known viruses by Star Internet delivered
through the MessageLabs Virus Control Centre. For further information visit

 XSL-List info and archive:

Current Thread