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

Re: Sorting by days of the week

Subject: Re: Sorting by days of the week
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Tue, 23 Dec 2003 10:49:32 +0100
xsl day of the week
<allan.mccluskey@xxxxxxxxxxxxxxxxx> wrote in message
news:OF81B59B27.47321731-ONCA256E05.000EB718-CA256E05.000F8237@xxxxxxxxxxxxxxxxxxxx
> G'day all,
>
> I need to sort my output by days of the week. i.e. I have a element called
> 'dayCode' which can contain one of the following values: MON1, MON2, TUE1,
> TUE2, WED1, WED2, THU1, THU2, FRI1, FRI2 etc etc
>
> I've tried using <xsl:sort select="dayCode" order="ascending"/> but as you
> would expect, the output order is FRI, MON, THU, TUE, WED.
>
> Is there a way to do this kind or sort??

A general type of solution is the following:

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:wkDays="my:week Days"
 exclude-result-prefixes="wkDays"
 >
  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <wkDays:wkDays>
    <wDay name="MON1" ordinal="1"/>
    <wDay name="MON2" ordinal="2"/>
    <wDay name="TUE1" ordinal="3"/>
    <wDay name="TUE2" ordinal="4"/>
    <wDay name="WED1" ordinal="5"/>
    <wDay name="WED2" ordinal="6"/>
    <wDay name="THU1" ordinal="7"/>
    <wDay name="THU2" ordinal="8"/>
    <wDay name="FRI1" ordinal="9"/>
    <wDay name="FRI2" ordinal="10"/>
    <wDay name="SAT1" ordinal="11"/>
    <wDay name="SAT2" ordinal="12"/>
    <wDay name="SUN1" ordinal="13"/>
    <wDay name="SUN2" ordinal="14"/>
  </wkDays:wkDays>

  <xsl:variable name="vwkDays" select="document('')/*/wkDays:*[1]"/>
  <xsl:template match="days">
    <days>
      <xsl:for-each select="day">
        <xsl:sort select="$vwkDays/wDay[@name=current()]/@ordinal"
         data-type="number"/>

         <xsl:copy-of select="."/>
      </xsl:for-each>
    </days>
  </xsl:template>
</xsl:stylesheet>

when applied on this source.xml:

<days>
  <day>FRI2</day>
  <day>TUE1</day>
  <day>MON1</day>
  <day>THU2</day>
  <day>WED1</day>
  <day>SUN2</day>
  <day>SAT1</day>
</days>

produces the wanted result:

<days>
   <day>MON1</day>
   <day>TUE1</day>
   <day>WED1</day>
   <day>THU2</day>
   <day>FRI2</day>
   <day>SAT1</day>
   <day>SUN2</day>
</days>



Dimitre Novatchev.
FXSL developer

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html




 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.