Re: [xsl] xsl sorting (Follow-up question)

Subject: Re: [xsl] xsl sorting (Follow-up question)
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 17 Apr 2002 17:40:40 +0100
Hi Greg,

> In the case where a sorting key (e.g. the attributes in the above
> example) is not present, is it possible to specify that absentees
> should be at the front or at the back of the list? For example, I
> want to sort a group of items in ascending alphabetical order based
> on child element "name". Since the name is optional (there may be a
> series/name and series/number instead of ./name), can I request that
> any items without a ./name element be at the top of the list,
> followed by the alphabetical sorting of all items with a ./name
> element? Conversely, how do I request that they be at the end of the
> list? (I forget which is the default at the moment.)

Add a level of sorting to your sorts, so that you first sort by the
presence/absence of the element, and then by that element:

  <xsl:sort select="boolean(name)" />
  <xsl:sort select="name" />

This will place all the elements without names (such that
boolean(name) equals 'false') before all the elements that do have
names (such that boolean(name) equals 'true') and sort those that do
have names in alphabetical order.

If you want the elements without names to come after those with names,
then just change the ordering of the first sort:

  <xsl:sort select="boolean(name)" order="descending" />
  <xsl:sort select="name" />

Note that this works because 'false' is alphabetically before 'true',
not because the XSLT processor knows anything about how to sort
boolean values.
  
Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread