Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an XSLT insert the current date?

Tags:

xhtml

xslt

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?

like image 230
Eric Anastas Avatar asked Oct 15 '09 21:10

Eric Anastas


People also ask

How do I get todays date in XSLT?

XSLT 1.0 does not provide any standard way to get the current date/time. You can call an extension function to do it (depends on your processor), or you can pass it to the stylesheet as the value of a parameter.

What is current () XSLT?

XSLT current() Function The current() function returns a node-set that contains only the current node. Usually the current node and the context node are the same.

What type of input is accepted by XSLT?

The XSLT processor operates on two inputs: the XML document to transform, and the XSLT stylesheet that is used to apply transformations on the XML. Each of these two can actually be multiple inputs.

What can XSLT do?

XSLT allows a stylesheet author to transform a primary XML document in two significant ways: manipulating and sorting the content, including a wholesale reordering of it if so desired, and transforming the content into a different format.


1 Answers

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> 

like image 50
Jim Garrison Avatar answered Sep 19 '22 02:09

Jim Garrison