How to get current date and time of system using XSLT 1.0

Posted on 2011-03-15
Last Modified: 2012-05-11
    I want to add the current date and time.
What I conclude that its not possible using xslt 1.0 but you can retrieve the system date time.
how that can be done? does it depend on hardware which allow to send date and time information?
Question by:SANbuddies
  • 6
  • 3
LVL 60

Expert Comment

by:Geert Bormans
ID: 35140012
No, that is something XSLT1 simply can't do

here are your options

- pass a parameter in from outside the XSLT. If you run the XSLT from a programming langiuage, fetch the current date from the programming language and pass it on

- use EXSLT extensions
supported for Xalan, Saxon and some others
This restricts the number of XSLT processors you can use

- use an extension language for building an extension function yourself
Depends even more on the browser
would be Javascript or C# for msxml, would be java for xalan and saxon (but for those you could usethe previous method anyway)

- if you need to run the XSLT in a browser, call the XSLT from javascript and use the first method

re-check your requirements for XSLT1.
I have found many projects that claimed to requirer XSLT1, but that were easy to turn into XSLT2 projects
XSLT2 has the functionality in house
LVL 60

Expert Comment

by:Geert Bormans
ID: 35140018
Tell me which method you prefer (and most importantly which processor you use)
and I will give you an example

Author Comment

ID: 35145188
well I am not using XSLT in a browser, I am using the XSLT against xml file to fetch some data according to the requirement.
Is possible that I use the VB code in same XSLT file.
Public Function GetDate() As String
  {    body of the function get date


XSLT function
<xsl:variable name="MasterDate" select="vb:GetDate()" />

What is your idea about above?
If you have a better solution, you are welcome to share.
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

LVL 60

Expert Comment

by:Geert Bormans
ID: 35145194
well, that would be an extension function,
I am not sure about VB, there is a possibility that you can get this to work (but more code is needed then) using VBScript and msxml

You haven't answered my questions
- how do you execute the XSLT (I know it is not the browser, but what is it then), do you execute the XSLT from VB?
- which processor are you using

Author Comment

ID: 35145791
I am actually not quite sure which processor we are using.
we are calling the xslt in a program but I am not sure which processor its using the program is either build in VB or any .net technology I am sure.
Is it very important to know the processor?
LVL 60

Expert Comment

by:Geert Bormans
ID: 35145817
Yes, it is very important to know the processor if you need to build extension functions.

If you are calling the XSLT from VB or .Net then you are using msxml

I will look for an example using extension functions,
that will work for msxml only

but I would simply pass the date as a parameter

have a global parameter in the XSLT
<xsl:param name="cur-time"/>
as a direct child of the xsl:stylesheet element

and from the VB code pass the parameter with the transform
(using something like "addParameter", but that depends on the XSLT calling code you are using)
LVL 60

Accepted Solution

Geert Bormans earned 125 total points
ID: 35148488
Here is what you can likely do

you will need
- msxsl namespace declared
- a random namespace declared for your function (I used exslt date here)
- an msxsl:script block in your stylesheet that implements the javascript function. Note that you need to indicate the namespace prefix here and that you need to use it in the function call
and there is a language specification. I am quite certain that it works for VBScript too. SAme approach

But still... I would go the passing parameter way

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                extension-element-prefixes="date msxsl">

<msxsl:script language="JScript" implements-prefix="date">
	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;
<xsl:template match="/">
	<xsl:value-of select="date:date()"/>


Open in new window


Author Comment

ID: 35178688
I am already using a VB function like this
<msxsl:script language="VB" implements-prefix="vb">
  Public Function GetInterfaceDef(byVal name as string) As String

What you think, using existing VB would be better to generate Date? as compare to have an additional JScript?

LVL 60

Expert Comment

by:Geert Bormans
ID: 35178934
Well, my argument for getting the date outside the XSLT and then pass it in, is mainly based on keeping the XSLT processor independent.
If you already have a VB function implemented in your XSLT,
you are bound to msxml already,
so I would suggest that you'd better create another VB function for the current date.
You can do that inside the same msxsl:script block
There is no need to merge vB and JScript blocks, if you are comfortable with VB, continue using it (don't merge three languages if two will do :-)

I by the way did not know that VB was an option for msxsl:script

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System ( I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

773 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