A program we use in my office exports reports by translating a XML file it exports with an XSLT file into XHTML. I'm rewriting the XSLT to change the formatting and to add more information from the source XML File.
I'd like to include the date the file was created in the final report. But the current date/time is not included in the original XML file, nor do I have any control on how the XML file is created. There doesn't seem to be any date functions building into XSLT that will return the current date.
Does anyone have any idea how I might be able to include the current date during my XSLT transformation?
XSLT 2
Date functions are available natively, such as:
There is also
current-date()
andcurrent-time()
.XSLT 1
Use the EXSLT date and times extension package.
date.xsl
to the location of your XSL files.date.xsl
.For example:
Late answer, but my solution works in Eclipse XSLT. Eclipse uses XSLT 1 at time of this writing. You can install an XSLT 2 engine like Saxon. Or you can use the XSLT 1 solution below to insert current date and time.
This will call Java's Data class to output the date. It will not work unless you also put the following "java:" definition in your
<xsl:stylesheet>
tag.I hope that helps someone. This simple answer was difficult to find for me.
For MSXML parser, try this:
Also read XSLT Stylesheet Scripting using msxsl:script and Extending XSLT with JScript, C#, and Visual Basic .NET
Do you have control over running the transformation? If so, you could pass in the current date to the XSL and use $current-date from inside your XSL. Below is how you declare the incoming parameter, but with knowing how you are running the transformation, I can't tell you how to pass in the value.
For example, from the bash script, use:
Then, in the xsl you can use:
reference: Formatting Dates and Times using XSLT 2.0 and XPath