Solved

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

Posted on 2011-03-15
9
2,267 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
  • 6
  • 3
9 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:SANbuddies
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

762 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

11 Experts available now in Live!

Get 1:1 Help Now