101

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?

Eric Anastas
  • 21,675
  • 38
  • 142
  • 236
  • I don't know what parser is being used that's the problem. The program I used exports reports directly and uses the XSLT file in its program directory to generate the reports. – Eric Anastas Oct 15 '09 at 21:59

6 Answers6

125

XSLT 2

Date functions are available natively, such as:

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

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

XSLT 1

Use the EXSLT date and times extension package.

  1. Download the date and times package from GitHub.
  2. Extract date.xsl to the location of your XSL files.
  3. Set the stylesheet header.
  4. 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>

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 7
    For anyone using [tag:.net]/[tag:c#], don't spend too much time trying to get EXSLT imports working with the standard .NET XSLT transformers - use [MVP.XML](http://mvpxml.codeplex.com/) right away. It has [built-in support for ESXLT](http://mvpxml.codeplex.com/wikipage?title=EXSLT.NET). – Joel Purra Jul 25 '12 at 20:43
18

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.

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

For example, from the bash script, use:

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

Then, in the xsl you can use:

<xsl:value-of select="$current-date"/>
mirelon
  • 4,896
  • 6
  • 40
  • 70
Kevin Hakanson
  • 41,386
  • 23
  • 126
  • 155
  • 1
    Indeed, how to pass the value is dependent on the system. One possible option: with [xsltproc](http://xmlsoft.org/xslt/xsltproc2.html) on UNIX, it might be: `xsltproc --stringparam current-date \`date +%Y-%m-%d\` -o output.html path-to.xsl path-to.xml`. Some systems also just take parameters as `$param=value`, so in that case `$current-date=\`date +%Y-%m-%d\`` somewhere. Or otherwise look for ways to specify parameters in whatever XSLT processor you're using. – lindes Jun 28 '12 at 21:23
15

For MSXML parser, try this:

<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="msxsl">

    <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 XSLT Stylesheet Scripting using msxsl:script and Extending XSLT with JScript, C#, and Visual Basic .NET

Scoregraphic
  • 7,110
  • 4
  • 42
  • 64
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • This does not work with Apache FOP as the transformer. Error message: Instance method call to method today requires an Object instance as first argument – Trey Carroll Nov 16 '11 at 15:22
  • 1
    oops: extension-element-prefixes="msxml" should be extension-element-prefixes="msxsl". Fails to work on my system as well. – Jay Sep 03 '14 at 21:06
11
...
    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()"/>
Evgeny Glazov
  • 111
  • 1
  • 2
8

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.

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

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.

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

I hope that helps someone. This simple answer was difficult to find for me.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Mark Hamby
  • 116
  • 1
  • 3
7
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 a.m. on September 19.

reference: Formatting Dates and Times using XSLT 2.0 and XPath

Black
  • 5,023
  • 6
  • 63
  • 92