Can XSLT insert current date?

The program we use in my office exports reports by converting the XML file that it exports with the XSLT file to XHTML. I am rewriting XSLT to change the formatting and add additional information from the source XML file.

I would like to include the date when the file was created in the final report. But the current date / time is not included in the source XML file, and I cannot control how the XML file is created. XSLT doesn't seem to have any date constructing functions that will return the current date.

Does anyone know how I can include the current date during XSLT conversion?

+79
xhtml xslt
Oct. 15 '09 at 21:14
source share
6 answers

XSLT 2

Date functions are available initially, for example:

<xsl:value-of select="current-dateTime()"/> 

There is also current-date() and current-time() .

XSLT 1

Use the EXSLT date and time extension package.

  • Download the date and time from GitHub .
  • Extract date.xsl to the location of your XSL files.
  • Set the style sheet title.
  • Import date.xsl .

For example:

 <xsl:stylesheet version="1.0" xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="date" ...> <xsl:import href="date.xsl" /> <xsl:template match="//root"> <xsl:value-of select="date:date-time()"/> </xsl:template> </xsl:stylesheet> 

+99
Oct. 15 '09 at 21:16
source share

For the MSXML parser, try the following:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:my="urn:sample" extension-element-prefixes="msxml"> <msxsl:script language="JScript" implements-prefix="my"> function today() { return new Date(); } </msxsl:script> <xsl:template match="/"> Today = <xsl:value-of select="my:today()"/> </xsl:template> </xsl:stylesheet> 

Also read the XSLT Stylescript using msxsl: script and the XSLT Extension with JScript, C # and Visual Basic.NET

+13
Oct. 15 '09 at 21:23
source share

Do you have control over the conversion? If so, you can pass the current date to XSL and use the current date from within XSL. The following describes how you declare an input parameter, but knowing how you perform the conversion, I cannot tell you how to pass the value.

 <xsl:param name="current-date" /> 

For example, from a bash script, use:

 xsltproc --stringparam current-date `date +%Y-%m-%d` -o output.html path-to.xsl path-to.xml 

Then in xsl you can use:

 <xsl:value-of select="$current-date"/> 
+12
Oct. 15 '09 at 21:56
source share
 ... xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:local="urn:local" extension-element-prefixes="msxsl"> <msxsl:script language="CSharp" implements-prefix="local"> public string dateTimeNow() { return DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ"); } </msxsl:script> ... <xsl:value-of select="local:dateTimeNow()"/> 
+9
Dec 03
source share
 format-date(current-date(), '[M01]/[D01]/[Y0001]') = 09/19/2013 format-time(current-time(), '[H01]:[m01] [z]') = 09:26 GMT+10 format-dateTime(current-dateTime(), '[h1]:[m01] [P] on [MNn] [D].') = 9:26 am on September 19. 

reference: Formatting dates and times using XSLT 2.0 and XPath

+4
Oct 22 '15 at 0:21
source share

Late answer, but my solution works in Eclipse XSLT. Eclipse uses XSLT 1 at the time of this writing. You can install an XSLT 2 engine such as Saxon. Or you can use the XSLT 1 solution below to insert the current date and time.

 <xsl:value-of select="java:util.Date.new()"/> 

This will call the Java Data class to display the date. This will not work unless you also put the following definition of "java:" in the <xsl:stylesheet> tag.

 <xsl:stylesheet [...snip...] xmlns:java="java" [...snip...]> 

I hope this helps someone. This simple answer is hard for me to find.

+4
Feb 17 '16 at 16:25
source share



All Articles