RE: [xsl] Sorting date format

Subject: RE: [xsl] Sorting date format
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 23 Dec 2008 10:26:06 -0000
It's best to avoid date formats such as d/m/y or m/d/y - better to use the
ISO date format yyyy-mm-dd. This is supported directly in XSLT 2.0 via the
data type xs:date. If you're stuck with XSLT 1.0 and/or d/m/y format dates,
you will have to rearrange them into y/m/d format using a combination of
concat() and substring() operations, you can then convert 21/02/05 to
20050221, which you can then sort directly either as a number or as a
string. So it's something like

<xsl:sort select="concat(substring(.,7,2), substring(.,4,2),
substring(.,1,2)"/>

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: V.Ramkumar [mailto:v.ramkumar@xxxxxxxxxxxxxxxxxxxxxx] 
> Sent: 23 December 2008 09:53
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Sorting date format
> 
> Hi List,
> 
> How to sort the date field. Please see below and provide solution.
> 
> XML:
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
>     <date>07/12/07</date>
>     <date>11/12/06</date>
>     <date>10/12/05</date>
>     <date>21/02/05</date>
> </root>
> 
> XSL:
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> version="2.0">
> <xsl:output omit-xml-declaration="yes"/>
> 
>     <xsl:template match="/">
>     <xsl:apply-templates/>
> </xsl:template>
> 
>     <xsl:template match="root">
>         <xsl:for-each select="date">
>             <xsl:sort select="." order="ascending" data-type="text"
> /><xsl:copy-of select="."/>
>         </xsl:for-each>
>     </xsl:template>
>     
> </xsl:stylesheet>
> 
> Required Output:
> 
>     <date>21/02/05</date>
>     <date>10/12/05</date>
>     <date>11/12/06</date>
>     <date>07/12/07</date>
> 
> Regards,
> Ramkumar

Current Thread