xsl-list
[Top] [All Lists]

Re: [xsl] Sorting with Different Sort Keys

2007-10-19 02:10:46
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.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--