Solved

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

Posted on 2011-03-15
9
2,922 Views
Last Modified: 2012-05-11
Hi,
    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?
0
Comment
Question by:SANbuddies
[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
  • 6
  • 3
9 Comments
 
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 http://www.exslt.org/date/functions/date-time/index.html
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
0
 
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
0
 

Author Comment

by:SANbuddies
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.
thanks
0
Technology Partners: 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: 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
0
 

Author Comment

by:SANbuddies
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?
0
 
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)
0
 
LVL 60

Accepted Solution

by:
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"
                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:SANbuddies
ID: 35178688
Thanks
I am already using a VB function like this
<msxsl:script language="VB" implements-prefix="vb">
 <![CDATA[
  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?

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

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

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…
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
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 receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

717 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