Click here to Skip to main content
13,289,242 members (56,188 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 2 Jun 2003

Sorting dates in XSL

, 2 Jun 2003
Rate this:
Please Sign up or sign in to vote.
Sorting dates in XSL


This article assumes that the reader has some prior knowledge of XML and XSL. XSL provides infrastructure for sorting on character and numerical values. But nothing is provided for sorting date. This article presents two ways for sorting XML data based on date.

First way by numerical method

Lets say you have an XML data as follows:

  <Element StartDate="26 January 2003"/>
  <Element StartDate="28 Feburary 1979"/>
  <Element StartDate="3 November 1989"/>
  <Element StartDate="1 July 2003"/>
  <Element StartDate="26 January 1977"/>

(Section a)

You can sort it by having one more attribute for each Element node which will have the following format "yyyymmdd". For example: if the date is 26 January 2003, then the format will be "20030126". 2003 is the year, 01 is the month and finally 26 is the date. So lets have one more attribute called SortDate, so our above XML will look like:

  <Element StartDate="26 January 2003" SortDate="20030126"/>
  <Element StartDate="28 Feburary 1979" SortDate="19790228"/>
  <Element StartDate="3 November 1989" SortDate="19891103"/>
  <Element StartDate="1 July 2003" SortDate="20030701"/>
  <Element StartDate="26 January 1977" SortDate="19770126"/>

Note: Month name and day which are less than 10 should be denoted by two numeric values, otherwise it will affect the sort order, resulting in wrong results. For example: January should be denoted by "01".

Now sort the XML data with respect to SortDate attribute and sorting using numerical data.

<xsl:sort select="@SortDate" data-type="numeric" order="descending"/>

Second way by string method

To explain how this method works, let me first give you code which will be used to sort the above XML data (Section a) by this method.

<xsl:stylesheet version="1.0" 


   <xsl:variable name="vMonthNames" 

      <xsl:template match="/">
         <xsl:for-each select="Root/Element">
         <!-- year part -->

            select="substring-after(substring-after(@Startdate,' '),' ')" 

         <!-- month part -->

             substring-before(substring-after(@Startdate,' '),' ')))" 

         <!-- day part -->
         <xsl:sort select="substring-before(@Startdate,' ')" 

         <xsl:value-of select="@date"/><br/>

The idea is extract the year part of the date and sort the date values using year, then extract the month part of the date and find the month string in the variable vMonthNames, you will find the index and that index is used to sort the month. For example if you find "January" you will get the index as '1' and if you find "March" you will get an index 18. This is how "January" is less than "March". And finally extract the day part from the date and sort on it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Amol Chavan
Web Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralXML Schema valid dates Pin
vilde9-Jun-03 21:32
sussvilde9-Jun-03 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 3 Jun 2003
Article Copyright 2003 by Amol Chavan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid