xsl place current date into output

The context here is formatting a XML file with an XSL:stylesheet producing a html report.
Most things are working but i am having trouble getting the following to work :

- I want to printout the current date the report was generated on

I have tried many different snippets in experts-exchange on the net, with no result.

I always end up with the system saying it has no context of the function i am referencing.
Although the sample below shows a vanilla xsl:stylesheet header, i have tried many with other w3.org, microsoft , xmlns:datetime="http://exslt.org/dates-and-times"> and others

No matter what i do, i can't find the function referenced, fn:xxx , date:xxx etc.

>> So i need to know a way that works to get the date ?

Some URLs http://www.zvon.org/xxl/XSL-Ref/Tutorials/Date-Time/dt1.html 
see bottom of http://www.w3schools.com/Xpath/xpath_functions.asp Context Functions


nb: Points only awarded for a working example, i have burned so much time i don't want to still not be across the line after assigning points.

<xsl:stylesheet version="1.0"
  <xsl:output method="html"/>
  <xsl:template match="/">
      <!-- Like date in '12 November 2008' format -->
         <xsl:value-of select="current-date()"/>
      <xsl:for-each select="DocumentElement/Results[section != 'Help']">
<!-- Other stuff in here -->

Open in new window

Who is Participating?
Geert BormansConnect With a Mentor Information ArchitectCommented:
If your context is both IE and FireFox,
you are down to option four.
since browsers don't support XSLT2 and extension functions don't work across browsers.

Your only option is
- running the XSLT from a JavaScript in an HTML page (preferably using sarissa library for cross browser XSLT)
- create the current dat inside the javascript
- pass the current date to the XSLT from within javascript

complex no? Well it is the only way it can be done, I am afraid.

If you drop FireFox and can hapily live with IE alone, the above example will work for you

There is no XSLT2 in browsers.
There is no solution for doing this in standard XSLT1.
Geert BormansInformation ArchitectCommented:
In order to answer this question, it is important to know which processor you are using
There are four possible options

- If you are using XSLT2, there is a current-date() function that gives you all you need
Your stylesheet has a version 1.0, so you don't have current-date() available. That is why you get the erro message.
You can only set this to 2.0 if you have an XSLT2 processor available of course

- If you are using saxon, xalan or some other, you have access to exslt (verify in the date functions section of www.exslt.org). This solution uses exernal functions, so they are pretty bound to a specific processor. Make sure you get the namespace right
So th ereason why the exslt extension doesn't work could be because your processor does not support it, or that you set the namespaces the wrong way

- you could also write an extension function yourself. In msxml that would be a simple JavaScript function,; in .NET that could be C#

- 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
Geert BormansInformation ArchitectCommented:
This will work for msxml, both common and .NET
This will also work in Internet Explorer

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

greg_robertsAuthor Commented:

My context is IE 7.0 or firefox, i.e. opening the xml in these then saving the html result.

I will try this. I have seen this code either on the .net or experts-exchange and did not try it because i was focusing on using existing standard functions (per some posts).

I will test and award points and will give bonus points for a solution using existing standard functions. i.e. a function with some formatting options so no extra code is needed.

greg_robertsAuthor Commented:
p.s. i tried xsl version=2.0 but may have noy done this correctly.
greg_robertsAuthor Commented:

OK tried in IE , works and as you said does not work for Firefox. Unfortunately (and i did not say this) IE works fine for viewing the data but i need Firefox to save the data as html.
I can't find an option in IE7 to save a xml rendered page as html. The "save as" only seems to want to save the original xml file.

Many thanks
greg_robertsAuthor Commented:
Nice to have some ref. to the sarissa library and the Date() options so i can modify the sample for the format i am after.

Geert BormansInformation ArchitectCommented:
Why don't you make this two seperate processes?
Use the XSLT for viewing in IE
and use msxsl command line tool to effectively transform the XML to the same HTML using the same XSLT
Geert BormansInformation ArchitectCommented:
here is where to get msxsl (note that you will need to install xsxml4 as well)

here you can find sarissa
but I would definitely go for the msxsl solution... a lot less complexity
let me know if you still want sarissa and need help with it

and about the date function, that is simply JavaScript date object

greg_robertsAuthor Commented:
Thanks for the extra info !
Geert BormansInformation ArchitectCommented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.