RE: [xsl] Sorting Using A Predefined Order

Subject: RE: [xsl] Sorting Using A Predefined Order
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 21 Jun 2007 09:05:09 +0100
In 2.0, you just need to write a user-defined function that translates your
keys into numbers, for example January->1, February->2, and so on. The use
xsl:sort select="my:function(head)".

In 1.0 it's a bit more difficult because you can't call user-defined
functions within the select expression of xsl:sort. But you can usually
achieve the same thing by means of table-defined mapping. Given a document
months.xml such as

<doc>
  <month name="January" value="1"/>
  <month name="February" value="2"/> 

you can write 

<xsl:sort
select="document('months.xml')/doc/month[@name=current()/head]/@value"
data-type="number"/>

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: Jeff Sese [mailto:jsese@xxxxxxxxxxxx] 
> Sent: 21 June 2007 05:16
> To: Xsl-List
> Subject: [xsl] Sorting Using A Predefined Order
> 
> Hi,
> 
> Is it possible to sort some data according to a predefined ordering?
> 
> 
> I have a file like:
> 
> <root>
> <div>
>     <head>Some text 1</head>
> </div>
> <div>
>     <head>Some text 2</head>
> </div>
> <div>
>     <head>Some text 3</head>
> </div>
> <div>
>     <head>Some text <4/head>
> </div>
> <div>
>     <head>Some text 5</head>
> </div>
> </root>
> 
> and i want it sorted using this ordering:
> 
> Some text 2
> Some text 3
> Some text 5
> Some text 1
> Some text 4
> 
> Is the only solution to this is creating my own collation?
> 
> Thanks!
> -- Jeff

Current Thread