Solved

Format Date Using XSL

Posted on 2003-10-24
6
9,977 Views
Last Modified: 2010-08-05
I need to format the date in an XML Response. It is returned in the following format YYYYMMDD, and I'd like to have it display as MM/DD/YYYY.

I have the following code in place in the XSL file, and if I replace

value-of select="ms:format-date(Date, 'MMM dd, yyyy')"/

with just Date, I can get the results unformatted, but I'm apparently a bit off on my attempt to format.


<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                       xmlns:ms="urn:schemas-microsoft-com:xslt"
                             xmlns:dt="urn:schemas-microsoft-com:datatypes">
<xsl:template match="/">



  <html>
  <body>
    <h2>Tracking Results</h2>
          <table width="70%" border="1" cellpadding="1" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#CCCCCC">
      <tr>
        <th>Date</th>
        <th>Time</th>
        <th>Location</th>
        <th>Activity</th>
      </tr>
      <xsl:for-each select="TrackResponse/Shipment/Package/Activity">
        
      <tr>
               
                        <td width="10%"><xsl:value-of select="ms:format-date(Date, 'MMM dd, yyyy')"/></td>
            <td width="10%"><xsl:value-of select="Time"/></td>
            <td width="40%"><xsl:value-of select="ActivityLocation/Address/City"/>,
            <xsl:value-of select="ActivityLocation/Address/StateProvinceCode"/>,
            <xsl:value-of select="ActivityLocation/Address/CountryCode"/></td>
            <td width="40%"><xsl:value-of select="Status/StatusType/Description"/></td>
                                ...etc

Hopefully my explanation is clear.
Thanks in advance.
0
Comment
Question by:DCfromDisplay
  • 3
  • 3
6 Comments
 
LVL 26

Expert Comment

by:rdcpro
ID: 9617131
I think you have to use MSXML 4 for this.  Make sure your progID is for the version 4 parser ("Msxml2.DomDocument.4.0").

If you can't use MSXML 4 to do this, you'll have to resort to MSXML 3, and in that case I usually just write an extension function that does what I need.  Here's an example that plays around with dates:

http://dev.rdcpro.com/Members/rdcpro/snippets/filterandsort/

Chris Bayes has written an MSXSL extension that supports EXSLT:

http://www.exslt.org/date/functions/format-date/date.msxsl.xsl

Regards,
Mike Sharp

http://dev.rdcpro.com


0
 

Author Comment

by:DCfromDisplay
ID: 9617303
Yeah, it looks like I should get away from using MSXML 4. While I'm not crystal clear on the solution after looking at your example, I hope to figure it out over the weekend.

Thanks again,

Darryl
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 9617355
oh, I meant you need to use MSXML 4, if you want the built-in IXTLRuntime format-date() function.  I think it was left out of MSXML 3, but I can't say for sure.

Regards,
Mike Sharp
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:DCfromDisplay
ID: 9626742
I'm afraid that I've not been able to make it work using your example. Any other suggestions would be greatly appreciated.

The problem with the MSXML 4 that I see is that I can't be sure that every client that tries to access this page will have these capabilities.

Also, here is a sample of the XML Response that I receive.


<?xml version="1.0" encoding="UTF-8"?>

<TrackResponse>
    <Response>
        <TransactionReference>
            <XpciVersion>1.0001</XpciVersion>
        </TransactionReference>
        <ResponseStatusCode>1</ResponseStatusCode>
        <ResponseStatusDescription>Success</ResponseStatusDescription>
    </Response>
    <Shipment>
        <Shipper>
            <ShipperNumber>12345E</ShipperNumber>
        </Shipper>
        <Service>
            <Code>15</Code>
            <Description>NDA EAM/EXP EAM</Description>
        </Service>
        <ShipmentIdentificationNumber>1Z123456789987654</ShipmentIdentificationNumber>
        <Package>
            <TrackingNumber>1Z123456789987654</TrackingNumber>
            <Activity>
                <ActivityLocation>
                    <Address>
                        <City>CLARKVILLE</City>
                        <StateProvinceCode>AK</StateProvinceCode>
                        <PostalCode>99901</PostalCode>
                        <CountryCode>US</CountryCode>
                    </Address>
                    <Code>ML</Code>
                    <Description>FRONT DOOR</Description>
                </ActivityLocation>
                <Status>
                    <StatusType>
                        <Code>D</Code>
                        <Description>DELIVERED</Description>
                    </StatusType>
                    <StatusCode>
                        <Code>FS</Code>
                    </StatusCode>
                </Status>
                <Date>20031002</Date>
                <Time>135500</Time>
            </Activity>
0
 
LVL 26

Accepted Solution

by:
rdcpro earned 250 total points
ID: 9628544
There was a recent thread that discussed several approaches.  One was a pure XSLT approach, and the other was one that used extension functions.  I rather prefer the extension function approach as it seems easier to me, but it assumes that your stylesheet isn't going to need to be portable (that is, it will only have to work on MSXML).   Here's the extension approach:

<xsl:stylesheet version="1.0"
               xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
               xmlns:msxsl="urn:schemas-microsoft-com:xslt"
               xmlns:dates="urn:rdcpro-com:dates"
               >
   <xsl:output method="html" encoding="UTF-8"/>
   <msxsl:script language="JScript" implements-prefix="dates">
       <![CDATA[
           function parseDate(oNodeList) {
           // construct a dateString from your node
           var dateString = oNodeList[0].text.substr(0, 4) + "-" + oNodeList[1].text.substr(4, 2) + "-" + oNodeList[2].text.substr(6, 2);
           var t = Date.parse(dateString);
           return t;
           }
           function formatDate(t) {
           arrMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
           var oDate = new Date(t);
           return arrMonth[oDate.getMonth()] + " " + oDate.getDate() + "," + oDate.getFullYear();
           }
       ]]>
   </msxsl:script>


Call the extension function with:

<xsl:value-of select="dates:formatDate(dates:parseDate(Date))"/>

Basically the extension function takes a nodelist with your Date node in it.  A date string is constructed, and a static method of the Date Object is used to get the time (number of milliseconds since somewhere around January 1970).  This is used to instantiate a new date object.

The formatDate constructs an array of the months, and uses the getMonth() method of the new Date object to return the Month string.

This could be done in a single function, but I like to split them up like this.

Take a look at this thread for the pure XSLT approach:

http://oldlook.experts-exchange.com/Web/Web_Languages/XML/Q_20762208.html

Regards,
Mike Sharp
0
 

Author Comment

by:DCfromDisplay
ID: 9628873
Thanks again...I think I'm well on my way now.

Darryl
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to read XML file attributes... 17 63
XSL - change date format 3 45
XML XSL Message 3 22
Xsl-fo show PDF when no results are returned 2 18
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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