Solved

Display datetime in XSLT

Posted on 2008-10-20
9
1,192 Views
Last Modified: 2013-11-18
how get a current date time value in XSLT. the date value should be in the format MM/DD/YYYY
0
Comment
Question by:intikhabk
[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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22765155
@chaituu,

I am very pleased that you reference my answers to a similar question, but...
- you could have removed the QueryTermInfo from the links you posted. Now it is obvious that as an answer to this question you simply queried the EE database. I can tell you that just googling for an answer without critically looking at the results is NOT an EE experts correct attitude.
- If you would have understood the links you are referring to, you would have realised that this is only a partial answer to the question asked, working in a very limited scope. The first link gives the current-date, only if EXSLT is supported by the processor. Not all processors do support EXSLT date functions to the same extend. You could have warned for that. The second link you post is a very limited approach from one date format to another and is not likely to work in this case

@intikhabk,

In order to answer this question, it is important to know which processor you are using
- If you are using XSLT2, there is a current-date() function that gives you all you need, you can reformat with a simple regular expression then
- If you are using msxml, saxon, xalan or some other, you have access to exslt (verify in the date functions section of www.exslt.org). You can then use a solution similar to what you find in chaituus first reference. This solution uses exernal functions, so they are pretty bound to a specific processor. Make sure you get the namespace right
- If your processor is XSLT1 and is not EXSLT aware, you will not be able to access the current date from whitin the Stylesheet and you will have to pass the current date from your application as a parameter to the XSLT. It would be interesting to know how your architecture looks like
As soon as you figured out what the best approach be for you, I will be happy to help you with the code

cheers

Geert
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22765215
I thought the links posted above may help him in solving the problem.what is QueryTermInfo you are reffering to?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22765230
just look at the URL
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XSLT/Q_22837627.html?sfQueryTermInfo=1+current+date+xslt
is what you posted
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XSLT/Q_22837627.html
is the link to the question without showing that you just used the EE search engine
Posting links without annotation is dangerous and should not be done.
Only post links if you know what the link ends limitations are.
People asking questions on EE can use the search engine to, always assume that they have done that and were not helped.
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22765244
from next time onwards i will keep in mind while posting these links.
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 22765679
If your using .Net without EXSLT you can create an Extension class that can provide functions to your xslt. Functions such as CurrentDate and FormatDate can be easily created in .Net

If so I can sort out an example for you.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22766371
mmh, for .Net, I tend to use the JScript solution, based on the EXSLT msxml scripts.
These have the advantage that they work for all usage of msxml, both in .Net or outside this.
I added the external function (and a usage example) that gives you what you need for msxml in and outside .Net
It will not work for other processors, that is why it is important that we know which processor you are using.
If you need your XSLT to be processor independent, only passing the parameter is an option
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:date="http://exslt.org/dates-and-times"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                extension-element-prefixes="date msxsl">
 
<msxsl:script language="JScript" implements-prefix="date">
	<![CDATA[
	function date(){
		var oDate = new Date();
		var ret = "";
		var m = oDate.getMonth() + 1;
	    var mm = m < 10 ? "0" + m : m;
		ret = ret + mm + "/";
		var d = oDate.getDate();
		var dd = d < 10 ? "0" + d : d;
		ret = ret + dd + "/";
		ret = ret + oDate.getFullYear();
		return ret;
		}
	]]>
</msxsl:script>
<xsl:template match="/">
	<xsl:value-of select="date:date()"/>
</xsl:template>
 
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:intikhabk
ID: 22766993
I will try and check if it works...thanks for the answer.......
0
 
LVL 23

Accepted Solution

by:
Tony McCreath earned 500 total points
ID: 22767129
Probably getting too deep for the question but always good to get ideas out there...

I have xsl files that can be used by several types of processors. Commonly asp (msxml) and .Net 1.1/2.0, and soon php.  

With my xslt files I tend to include a shared xsl file that contains common JScript functions and special template elements to access external functions. This is so I can have the xsl gracefully adapt to the different processors.

Heres a dateformat example based on the fact I may use a .Net Extension with a FormatDate method.

It could be extended to support more namespaced libraries as required.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mydotnetxsl="urn:schemas-mydotnet-com:xslt" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
		<xsl:template name="formatdate">
		<xsl:param name="date" />
		<xsl:param name="format" select="'dd/MM/yyyy'"/>
 
		<xsl:choose>
			<xsl:when test="function-available('mydotnetxsl:FormatDate')">
				<xsl:value-of disable-output-escaping="yes" select="mydotnetxsl:FormatDate(string($date),string($format))"/>
			</xsl:when>
			<xsl:when test="function-available('msxsl:format-date')">
				<xsl:value-of disable-output-escaping="yes" select="msxsl:format-date(string($date),string($format))"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of disable-output-escaping="yes" select="substring($date,1,10)"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

Open in new window

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

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

724 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