Click here to Skip to main content
Click here to Skip to main content

Sorting dates in XSL

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

Introduction

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:

<Root>
  <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"/>
</Root>

(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:

<Root>
  <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"/>
</Root>

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" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <xsl:variable name="vMonthNames" 
    select="'|January|February|March|April|May|June|July|
    August|September|October|November|December'"/>
      <xsl:template match="/">
         <xsl:for-each select="Root/Element">
         <!-- year part -->
         <xsl:sort 
            select="substring-after(substring-after(@Startdate,' '),' ')" 
            data-type="number"/>
         <!-- month part -->
         <xsl:sort 
           select="string-length(substring-before($vMonthNames,
             substring-before(substring-after(@Startdate,' '),' ')))" 
           data-type="number"/>
         <!-- day part -->
         <xsl:sort select="substring-before(@Startdate,' ')" 
           data-type="number"/>
         <xsl:value-of select="@date"/><br/>
         </xsl:for-each>
     </xsl:template>
</xsl:stylesheet>

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.

License

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

Share

About the Author

Amol Chavan
Web Developer
India India
No Biography provided

Comments and Discussions

 
GeneralXML Schema valid dates Pinsussvilde9-Jun-03 20:32 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 3 Jun 2003
Article Copyright 2003 by Amol Chavan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid