?
Solved

Sort date in XSL with DD-MMM-YY format

Posted on 2004-10-05
3
Medium Priority
?
1,445 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 500 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Introduction to Processes
Starting up a Project
Suggested Courses

741 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