[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3632
  • Last Modified:

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

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
SANbuddies
Asked:
SANbuddies
  • 6
  • 3
1 Solution
 
Geert BormansCommented:
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
 
Geert BormansCommented:
Tell me which method you prefer (and most importantly which processor you use)
and I will give you an example
0
 
SANbuddiesAuthor Commented:
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
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!

 
Geert BormansCommented:
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
 
SANbuddiesAuthor Commented:
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
 
Geert BormansCommented:
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
 
Geert BormansCommented:
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
 
SANbuddiesAuthor Commented:
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
 
Geert BormansCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now