Can an XSLT insert the current date?
Asked Answered
P

6

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?

Percy answered 15/10, 2009 at 21:14 Comment(1)
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.Percy
O
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>

Orchestra answered 15/10, 2009 at 21:16 Comment(1)
For anyone using .net/c#, don't spend too much time trying to get EXSLT imports working with the standard .NET XSLT transformers - use MVP.XML right away. It has built-in support for ESXLT.Studer
H
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"/>
Halloween answered 15/10, 2009 at 21:56 Comment(1)
Indeed, how to pass the value is dependent on the system. One possible option: with xsltproc 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.Closer
D
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

Dickenson answered 15/10, 2009 at 21:23 Comment(2)
This does not work with Apache FOP as the transformer. Error message: Instance method call to method today requires an Object instance as first argumentToggery
oops: extension-element-prefixes="msxml" should be extension-element-prefixes="msxsl". Fails to work on my system as well.Rakes
G
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()"/>
Girovard answered 3/12, 2012 at 14:2 Comment(0)
B
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.

Bridgework answered 17/2, 2016 at 16:25 Comment(0)
R
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

Rutilant answered 22/10, 2015 at 0:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.