Solved

Sort date in XSL with DD-MMM-YY format

Posted on 2004-10-05
3
1,431 Views
Last Modified: 2008-01-09
I'm a newbie at XML/XSL and I want to be able sort dates with the format, DD-MMM-YY.  Please advise.  Thanks in advance.

DNice30
0
Comment
Question by:DNice30
  • 2
3 Comments
 
LVL 5

Expert Comment

by:WesleySaysHi
ID: 12229326
DD-MMM-YY ?
It's DD-MM-YYYY or MM-DD-YYYY or DD-MM-YY or MM-DD-YY
Anyway, I will explain you in detail for the format: month/day/year (MM-DD-YY).

Lets say that the XML file has the following content:

<dates>

  <DNice30 interestDate="10/20/2004">
  </DNice30>

  <DNice30 interestDate="09/18/2004">
  </DNice30>

  <DNice30 interestDate="12/11/2004">
  </DNice30>

</dates>

The instruction 'xsl:sort' lets you sort a group of similar elements. Attributes for this element let you add details about how you want to sort them.
When you sort an element's children, the 'xsl:sort' element goes between the start- and end-tags of the 'xsl:apply-templates' element or goes inside of the 'xsl:for-each' instruction used to iterate across a node set.
The attribute 'select' of the instruction 'xsl:sort' can take any XPath expression as a sort key.

For example, you can sort the dates by your 'interestDate' attribute values:

<xsl:template match="dates">
  <xsl:apply-templates>
    <xsl:sort select="@interestDate"/>
  </xsl:apply-templates>
</xsl:template>

or you can have have three sort keys based on the year, month, and day substrings of the date string (you have to modify the lines marked 'year', 'month' and 'day' if you have another date format in your mind):

<xsl:template match="dates">
  <xsl:apply-templates>
    <xsl:sort select="substring(@interestDate,7,4)"/> <!-- year  -->
    <xsl:sort select="substring(@interestDate,1,2)"/> <!-- month -->
    <xsl:sort select="substring(@interestDate,3,2)"/> <!-- day   -->
  </xsl:apply-templates>
</xsl:template>

If, for example, your dates look like "MM/DD/YY" then in the line marked 'year' you don't have to change the '7' because the year still starts at position 7 in the date string but you have to replace the '4' with '2', because the year, in this case, has 2 characters instead of 4.

Of course you must put the code ------->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0">

-----> here !

</xsl:stylesheet>


Regards,
Wesley
0
 

Author Comment

by:DNice30
ID: 12230033
Thanks, Wesley.  The issue I'm having is that the date is formatted as DD-MMM-YY (ex. 05-OCT-04) .  I guess I could write a function to change the month text to the month number and add a new attribute.  Or is there anyway to convert the date to the serial equivalent much like the DATEVALUE function in Excel?

<DATES>
     <DATE>05-OCT-04</DATE>
     <DATE>05-SEP-04</DATE>
</DATES>
0
 
LVL 5

Accepted Solution

by:
WesleySaysHi earned 125 total points
ID: 12232416
I didn't think at something like 05-OCT-04 and thought that DD-MMM-YY was a mistake. Sorry.
As far as I know there is no equivalent of Excel's DATEVALUE function, but you could test the month text in the code I posted previously for each month (considering the XML file with your date format), using something like this:

<xsl:choose>
   .....
   <xsl:when test="OCT">
      .....
   </xsl:when>
   .....
</xsl:choose>  

Try and tell me how it worked. If you don't figure it out I will rewrite the code sometime after I wake up and finish my work. It's already 2 am here.

Good luck,
Wesley
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now