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

Re: Sorting with Different Sort Keys

Subject: Re: Sorting with Different Sort Keys
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Fri, 19 Oct 2007 11:08:58 +0200
Re:  Sorting with Different Sort Keys
Jeff Sese wrote:

I would want this kind of ordering: seite 0012 (sort key value should be 12) Siete 99 (sort key value should be 99) Seite 120 (sort key value should be 120) seite 120 (sort key value should be 120) SEITE 120 (sort key value should be 120) Seite 0120 (sort key value should be 120) seite120 (sort key value should be 120) Seite 12000 (sort key value should be 12000) 99 (sort key value should be 99) Page 12 (sort key value should be '') pink rabbit (sort key value should be '')

I hope this make things a little bit clear...

It does, by a landslide! It is not handy that "Seite" comes before "seite" comes before "SEITE", but I think your point is that the word doesn't matter, the sort key does. Here's how I would do it:


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="input">
<xsl:apply-templates select="data">
<!-- sort the 'seite' first -->
<xsl:sort select="matches(., '\s*seite', 'i')" order="descending" />
<!-- sort the non-correct ones last -->
<xsl:sort select="not(matches(., '^\d+$'))" order="ascending" />
<!-- sort everything by its numeric value -->
<xsl:sort select="replace(., '\D', '')" order="ascending" data-type="number" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="data">
<xsl:sequence select="'&#10;', text()"/>
</xsl:template>
</xsl:stylesheet>



I used this as input example:


<input>
<data>99</data>
<data>seite 120</data>
<data>seite120</data>
<data>Seite 99</data>
<data>Seite 120</data>
<data>Seite 0120</data>
<data>Page 12</data>
<data>seite 0012</data>
<data>seite120</data>
<data>pink rabbit</data>
<data>SEITE 120</data>
<data>Seite 99</data>
<data>0001</data> <data>Seite 12000</data>
<data>1200</data>
</input>


And I got this for output:

seite 0012
Seite 99
Seite 99
seite 120
seite120
Seite 120
Seite 0120
seite120
SEITE 120
Seite 12000
0001
99
1200
pink rabbit
Page 12


Which is, I believe, about according your rules. Note that the simplest way to put something on top in a sorting order is by extracting its boolean value. Another method is by using .[predicate] where the result is the empty sequence, but that may have some nasty side effects (i.e., when the result is not the empty sequence it will be sorted by its value and in your case, you do not want that).


HTH,

Cheers,
-- Abel Braaksma

PS: I chose for replace() in favor of starts-with(lower-case(.), 'seite') because I found it marginally clearer, but that's a matter of taste of course.

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.