Solved

Display datetime in XSLT

Posted on 2008-10-20
9
1,179 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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22763968
0
 
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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to count occurrences of each item in an array.

758 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