We help IT Professionals succeed at work.

xsl:sort - How to sort a date

mauler
mauler asked
on
I have been using xsl:sort, but, when using the following

<xsl:sort select="date" order="descending"/>, but it doesn't sort properly. The above results in a date listing of:

7/10/2001  
7/10/2001    
7/10/2001    
6/10/2001  
5/10/2001  
12/10/2001      
12/10/2001  
10/10/2001  
10/10/2001

Any thoughts on how to solve this?
Comment
Watch Question

Check this logic. may help you.
cheers
Umesh
-----------------------------------------------------------
datesort.xsl
------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
   <xsl:template match="/mypage/dates">
       <xsl:for-each select="date">
           <xsl:sort order="ascending" select="substring(text(), 7,4)"/>
        <xsl:sort order="ascending" select="substring(text(), 1,2)"/>
           <xsl:sort order="ascending" select="substring(text(), 4,2)"/>
           <xsl:value-of select="."/>
           <br/>
       </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>
/**************************************************/
datesort.xml
------------
<?xml version="1.0" encoding="UTF-8"?>
<?xml:stylesheet type="text/xsl" href="datesort.xsl" ?>
<mypage>
<dates>
  <date>12/12/2001</date>
  <date>04/02/1999</date>
  <date>28/06/2000</date>
  <date>19/07/2001</date>
</dates>
</mypage>
Sorting on dates in XSL can be rather tricky. There are basically three ways I can think of to approach this:

1) Ensure that all of your dates are in a normalized form in the XML document. To do this you must ensure that the dates always have 2-digit months and days and 4-digit years. If you can ensure this, then the simple sort in XSL will work just fine.
2) Write an external function for the XSL to normalize the dates. You don't specify what you're using to do your XSL processing (xalan, msxml, etc) so I can't tell whether this is something you can approach.
3) The most tricky way is to write a two step XSL. The first step would normalize the dates using XSL string functions, and create a temporary XML document which the second pass could sort. This would be a nightmare to do I think, but it should be possible.

If you have the ability to redefine the contents of the XML file to ensure that the dates are normalized before the XSL ever sees them, then that is definitely the way to go. If not, I'd go with the external function. I'd definitely leave the last solution out, until I'd exhausted all other options.

HTH

--Steve
BTW udnimbalkar's solution will work, if you're dates are normalized prior to the XSL being called.

--Steve
is your problem solved?
respond

Author

Commented:
Sorry to disappoint but I worked it out myself after not being able to get my head around the suggestions made,

it works using the following:

<xsl:sort select="translate(date,'/','')" order="ascending" data-type="number" />

Thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.