Solved

Sort date in XSL with DD-MMM-YY format

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

912 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

20 Experts available now in Live!

Get 1:1 Help Now