• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 511
  • Last Modified:

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

Thanks

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"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
 
  <xsl:template match="/">
      <html>
      <h4>
      <!-- Like date in '12 November 2008' format -->
         <xsl:value-of select="current-date()"/>
      </h4>
      <xsl:for-each select="DocumentElement/Results[section != 'Help']">
 
<!-- Other stuff in here -->
 
        </xsl:for-each>
     </html>
  </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
greg_roberts
Asked:
greg_roberts
  • 6
  • 5
1 Solution
 
Geert BormansCommented:
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
0
 
Geert BormansCommented:
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"
                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
 
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.

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!

 
greg_robertsAuthor Commented:
p.s. i tried xsl version=2.0 but may have noy done this correctly.
0
 
Geert BormansCommented:
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.
0
 
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
0
 
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.

Thanks
0
 
Geert BormansCommented:
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
0
 
Geert BormansCommented:
here is where to get msxsl (note that you will need to install xsxml4 as well)
http://www.microsoft.com/downloads/details.aspx?FamilyID=2fb55371-c94e-4373-b0e9-db4816552e41&displaylang=en

here you can find sarissa
http://sourceforge.net/projects/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
http://www.w3schools.com/jsref/jsref_obj_date.asp

cheers
Geert
0
 
greg_robertsAuthor Commented:
Thanks for the extra info !
0
 
Geert BormansCommented:
welcome
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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