xsl-list
[Top] [All Lists]

Re: Sorting by days of the week

2003-12-23 02:49:32

<allan(_dot_)mccluskey(_at_)centrelink(_dot_)gov(_dot_)au> wrote in message
news:OF81B59B27(_dot_)47321731-ONCA256E05(_dot_)000EB718-CA256E05(_dot_)000F8237(_at_)centrelink(_dot_)gov(_dot_)au(_dot_)(_dot_)(_dot_)
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[(_at_)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



<Prev in Thread] Current Thread [Next in Thread>