[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Ultimate arbitrary sort algorithm :-)

Subject: Ultimate arbitrary sort algorithm :-)
From: Oliver Becker <obecker@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 8 Aug 2000 11:16:35 +0200 (MET DST)
sort algorithm
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.