Solved

Sort date in XSL with DD-MMM-YY format

Posted on 2004-10-05
3
1,440 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

777 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