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

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

Thankyou for all the help
Mas
royalcyberAsked:
Who is Participating?
 
rdcproConnect With a Mentor Commented:
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
 
rdcproCommented:
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
 
royalcyberAuthor Commented:
thankyou mike

can upls send me the sample for pure xslt

Thankyou so much fo rur help
Mas
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
rdcproCommented:
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
 
MogalManicConnect With a Mentor Commented:
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
 
rdcproCommented:
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
 
dualsoulCommented:
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
 
rdcproCommented:
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
 
rdcproCommented:
Say, why don't *you* write one?!

Mike
0
 
dualsoulCommented:
> Say, why don't *you* write one?!

hmm..? not sure i undestood, write what?
0
 
rdcproCommented:
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
 
dualsoulCommented:
publish an article about XSLT 2.0 at your site? :) you mean this?
0
 
rdcproCommented:
Sure, why not?  I'd read it!  Not that I could pay, but it's a thought.

Regards,
Mike Sharp
0
 
dualsoulCommented:
hm...it's an idea, why not, can you contact me by email?
0
 
rdcproCommented:
Sure, though I don't know what it is. Mine is rdcpro@hotmail.com

Regards,
Mike Sharp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.