Solved

Sort date in XSL with DD-MMM-YY format

Posted on 2004-10-05
3
1,443 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
detecting  the added row index in a datagridview 3 86
Plain Text Editor for iPad 6 105
backup program with robocopy 6 74
VB.NET String Settings and Temp Folder Question 3 71
This is an explanation of a simple data model to help parse a JSON feed
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?

733 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