tags:

views:

1516

answers:

3

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?

+4  A: 

For XSLT 2.0 you have a wealth of date functions, i.e.:

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

as well as current-date() and current-time(). For XSLT 1 you'll have to use the dates-and-times EXSLT extension package. Here's a usage example for XSLT 1:

<xsl:stylesheet ... 
    xmlns:ex="http://exslt.org/dates-and-times" 
    extension-element-prefixes="ex">

    ...

       <xsl:value-of select="ex:date-time()"/>
    ...
</xsl:stylesheet>
Jim Garrison
+2  A: 

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

Rubens Farias
+2  A: 

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" />
Kevin Hakanson