Solved

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

Posted on 2004-09-29
17
552 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
[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
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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
 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 …
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

735 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