Solved

any xsl code  to easily transform a mm-dd-yyy to a yyyy-mm-dd format?

Posted on 2004-09-29
17
541 Views
Last Modified: 2011-10-03
any xsl code  to easily transform a mm-dd-yyy to a yyyy-mm-dd format?

Thankyou for all the help
Mas
0
Comment
Question by:royalcyber
17 Comments
 
LVL 26

Expert Comment

by:rdcpro
ID: 12184008
What XSLT processor are you using?

There are several approaches.  Some use "pure" XSLT to do it, but are a bit hard to do, and a bit hard to write.  You can also use Extension functions to do this.  This is processor dependant.  So if you use MSXML or .NET, you use one approach, if you use Saxon, you use a different one...EXSLT has a large library of extensions...many processor support it.  You can even use EXSLT in MSXML or .NET, but it's a bit hard to do.  MSXML 4, I believe, has an IXTLRuntime function that supports this. It's called format-date().  It's used like:

XML Document: (Sample.xml)

<?xml version='1.0'?>
<XMLSamples xml:space="preserve"
         xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <FileDetails>
   <Author>Robert Brown</Author>
   <Date dt:dt="datetime">2000-02-16T15:56:00</Date>
  </FileDetails>
</XMLSamples>
XSLT Style sheet (Sample.xsl)

<?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>
  <HEAD>
  </HEAD>
  <BODY>
   <xsl:for-each select="XMLSamples/FileDetails/Date">
      <DIV>
          Date Unedited:
          <xsl:value-of select="."/>
      </DIV>
      <DIV>
          Date Edited:
          <xsl:value-of select="ms:format-date(., 'MMM dd, yyyy')"/>
      </DIV>
   </xsl:for-each>
  </BODY>
  </HTML>
</xsl:template>
</xsl:stylesheet>
Output

The output HTML page shows the following entries:

Date Unedited: 2000-02-16T15:56:00
Date Edited: Feb 16, 2000


Here's one example of doing it with extension functions that I've used:

<?xml version="1.0" encoding="UTF-8"?>
<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) {
                  var dateString = oNodeList[0].text;
                  var t = Date.parse(dateString);
                  return t;
                  }
                  function formatDate(t) {
                  var oDate = new Date(t);
                  return oDate.getFullYear() + "-" + (oDate.getMonth() + 1) + "-" + oDate.getDate();
                  }
            ]]>
      </msxsl:script>
      

You can use these extension functions like:

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

The "pure xslt" way uses substring-before and substring-after to do this.  It gets ugly...but if you need to do it this way, let me know and I'll pass along an example.

Regards,
Mike Sharp
0
 

Author Comment

by:royalcyber
ID: 12184135
thankyou mike

can upls send me the sample for pure xslt

Thankyou so much fo rur help
Mas
0
 
LVL 26

Accepted Solution

by:
rdcpro earned 250 total points
ID: 12184169
Here's a "pure" XSLT example:

This XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="formatdate1.xslt"?>
<Date>2/27/2004</Date>

and this XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:template match="/">
            Formatting the Date <xsl:value-of select="Date"/> as YYYY-MM-DD:
            <!-- Year -->
            <xsl:value-of select="substring-after(substring-after(Date, '/'), '/')"/>
            <xsl:text>-</xsl:text>
            <!-- Month -->
            <xsl:value-of select="format-number(number(substring-before(Date, '/')), '00')"/>
            <xsl:text>-</xsl:text>
            <!-- Day -->
            <xsl:value-of select="format-number(number(substring-before(substring-after(Date, '/'), '/')), '00')"/>
      </xsl:template>
</xsl:stylesheet>


produces this result:

Formatting the Date 2/27/2004 as YYYY-MM-DD: 2004-02-27

Regards,
Mike Sharp
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 12184202
Of course, if you're using "-" instead of "/", you'll have to substitute the delimiter.  You can also use the translate function to convert a variety of delimiters to a common one, like:

translate(Date, "/ ", "--")

which converts forward slash and space to a hyphen.

There is also a way to map Month names using XSLT, in case your dates are like:  Jan 07, 1967

Regards,
Mike Sharp
0
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 250 total points
ID: 12188421
Mike's code should be written as a template, that way it can be called multiple times and in multiple contexts:
...
<xsl:template name='format-date'>
    <xsl:param name="inDate'>mm-dd-yyyy</xsl:param>

         <!-- Year -->
          <xsl:value-of select="substring-after(substring-after($inDate, '-'), '-')"/>
          <xsl:text>-</xsl:text>
          <!-- Month -->
          <xsl:value-of select="format-number(number(substring-before(Date, '-')), '00')"/>
          <xsl:text>-</xsl:text>
          <!-- Day -->
          <xsl:value-of select="format-number(number(substring-before(substring-after(Date, '-'), '-')), '00')"/>
     </xsl:template>
...

You can then call the function like this:
    <xsl:call-template name="format-date>
        <xsl:with-param name='inDate' select="//nodeData/Date"/>
    </xsl:template>

0
 
LVL 26

Expert Comment

by:rdcpro
ID: 12191849
I've added all the various techniques for this in a single article, and posted it on my web site.  I include a clever mechanism that dualsoul posted recently on this forum (in response to a different problem), that's definitely worth looking at.  

http://rdcpro.com/zones/xml/xslt/faqroot/formattingdates/view

As for placing the code in a template, well, yes you would probably do that in a real application.  My point was to show the technique; whether to place it in a template or not depends on the XSLT.  But if you do, you don't need to pass a parameter with the date, as the current node is still in context.  So whereever you need a formatted date, assuming the date node is in context, you simply use <xsl:call-template name="format-date"/>

Regards,
Mike Sharp
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 12198677
Mike, i'm interesting why don't you have any XSLT 2.0 articles at your site?

I'm playing with it last 2 weeks, and can say it's cool enough, really great things are possible with it now.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 26

Expert Comment

by:rdcpro
ID: 12202240
I've been awfully busy lately, trying to get a project out the door.  It's a good point though.  I'll get right on it!  ;^)

Mike
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 12202245
Say, why don't *you* write one?!

Mike
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 12203440
> Say, why don't *you* write one?!

hmm..? not sure i undestood, write what?
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 12204266
I was referring to your comment:

"Mike, i'm interesting why don't you have any XSLT 2.0 articles at your site?"

Regards,
Mike Sharp

0
 
LVL 15

Expert Comment

by:dualsoul
ID: 12204543
publish an article about XSLT 2.0 at your site? :) you mean this?
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 12209717
Sure, why not?  I'd read it!  Not that I could pay, but it's a thought.

Regards,
Mike Sharp
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 12210624
hm...it's an idea, why not, can you contact me by email?
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 12217660
Sure, though I don't know what it is. Mine is rdcpro@hotmail.com

Regards,
Mike Sharp
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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