Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xsl:sort on date

Status
Not open for further replies.

csbdeady

Programmer
May 18, 2002
119
GB
Hi

If I have in my xml file two records with:

<date>03/03/04</date>

in one, and:

<date>04/02/04</date>

in the other (format is: dd/mm/yy)

then

<xsl:sort select="date" order="ascending" />

will sort them so that the 03/03/04 record comes before the 04/02/04 record.

However obviously (to a human) this should be the other way around, based on the dd/mm/yy format.

How do I make this obvious in the XSLT xsl:sort transformation I am using?

Many thanks
-Colin
 
Ahah sorted it - I do like when I post a question and then an hour later solve my own problem;)

Still worth posting though so others don't have to waste that hour.

The solution is to use the following xml:sort :

<xsl:sort order="ascending" select="concat(substring(date, 7,2),substring(date, 4,2),substring(date, 1,2))" data-type="numeric" />

This creates a numeric representation of the date:

040303

and

040204

Both as yymmdd, which can then be sorted numerically as per the xsl:sort above.
 
Or, use the correct date format for XML: ISO-8601
[tab]yyyy-mm-ddThh:mm:ss

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top