|
Subject: Ultimate arbitrary sort algorithm :-) From: Oliver Becker <obecker@xxxxxxxxxxxxxxxxxxxxxxx> Date: Tue, 8 Aug 2000 11:16:35 +0200 (MET DST) |
Hello world, dear order fanciers!
Unfortunately I deleted a mail asking for sorting a list of names
by ignoring leading "Re: " and treating "Mc" and "Mac" the same.
Mike Kay answered something like "it's not possible to construct
a key expression that requires conditional processing".
As we've learned from the intersection expression: never say never! :-)
In the following I try to explain a method to construct such an
expression - only by XPath means. Decide yourself wether the result
is rather pretty or rather ugly ...
xsl:sort requires an expression used as the sort key.
What we want is the following:
if condition1 then use string1 as sort key
if condition2 then use string2 as sort key
etc.
How to achieve that?
The following expression gives $string if $condition is true,
otherwise it results in an empty string:
substring($string, 1 div number($condition))
Regarding to Mike's book this is perfectly valid.
(Note: works with Saxon and XT, but not with my versions of Xalan and
Oracle XSL - but I've not installed the latest versions ...)
If you don't like "infinity" - here's another solution:
substring($string, 1, number($condition)*string-length($string))
but then you need $string twice ...
The concatenation of all these substring expressions forms the sort key.
Requirement: all conditions must be mutually exclusive.
That's all! :-)
Here's my example which demonstrates the handling of leading "Re: "s.
If the string starts with "Re: ", an equivalent string without this
prefix but with an appended ", Re" forms the key, otherwise the
original string is used:
<xsl:sort select="concat(
substring(concat(substring-after(.,'Re: '), ', Re'),
1 div number(starts-with(.,'Re: '))),
substring(., 1 div number(not(starts-with(.,'Re: ')))))" />
As you may imagine these expressions could become very complex the more
arbitrary you want to sort. (I've not tried to work out the full problem
regarding "Mc" and "Mac" - however, the method should be clear.)
Pretty or ugly?
But it's XSLT 1.0!
Cheers,
Oliver
/-------------------------------------------------------------------\
| ob|do Dipl.Inf. Oliver Becker |
| --+-- E-Mail: obecker@xxxxxxxxxxxxxxxxxxxxxxx |
| op|qo WWW: http://www.informatik.hu-berlin.de/~obecker |
\-------------------------------------------------------------------/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| RE: "Skip until"??, Kay Michael | Thread | Re: Ultimate arbitrary sort algorit, Jeni Tennison |
| RE: "Skip until"??, Kay Michael | Date | Re: SAX is not XML (Key() Re: Saxon, Sebastian Rahtz |
| Month |