Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT 1.0 Get Current DateTime

I have a node in my XML file containing the following:

<Apple>2011-12-01T16:33:33Z</Apple>

I wish to take this line and replace it with the current date and time using the same format as shown above.

YYYY-MM-DDTHH:MM:SSZ

The node is within a namespace declared as 'x'

like image 438
Mike Avatar asked Feb 22 '12 11:02

Mike


People also ask

How can I get current date and time in XSLT?

The function current-dateTime() returns the current time including milliseconds, for example: 2008-05-07T18:12:23.593+03:00 where . 593 represents the milliseconds. If you want to extract only a fragment of the returned time you can apply the format-dateTime() function to the result of current-dateTime().

How to get current date in XSL?

Get current date with function current-date() : current date « XSLT stylesheet « XML.

What is current group () in XSLT?

Returns the contents of the current group selected by xsl:for-each-group. Available in XSLT 2.0 and later versions. Available in all Saxon editions. current-group() ➔ item()*

What is XSLT format?

XSLT (Extensible Stylesheet Language Transformations) is a language originally designed for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text or XSL Formatting Objects, which may subsequently be converted to other formats, such as PDF, PostScript and PNG.


3 Answers

Playing with DateTime is not possible with XSLT 1.0 alone .. In a similar situations I took help of scripting .. (C#)

Sample XML:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <Apple>2011-12-01T16:33:33Z</Apple>
</root>

Sample XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:cs="urn:cs">
  <xsl:output method="xml" indent="yes"/>
  <msxsl:script language="C#" implements-prefix="cs">
    <![CDATA[
      public string datenow()
     {
        return(DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"));
     }
     ]]>
    </msxsl:script>
      <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Apple">
      <xsl:copy>
      <xsl:value-of select="cs:datenow()"/>
      </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Resulting Output:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <Apple>2012-02-22T18:03:12Z</Apple>
</root>

The script may reside in a same file (like I have it in my sample XSLT code) or if the code triggering XSLTransformation is C# then move the same code in the calling place :)

like image 102
InfantPro'Aravind' Avatar answered Nov 16 '22 02:11

InfantPro'Aravind'


It's better to pass current datetime from your XML engine. Declare <xsl:param name="current-datetime"/> in your xsl:stylesheet, and pass the value from processor.

like image 43
Kirill Polishchuk Avatar answered Nov 16 '22 02:11

Kirill Polishchuk


You'll better pass the current data as an input / xsl:param to the template.

The XSLT aims to be purely functional language; that is, all templates / functions should conform to e.g. the following condition: If a pure function is called with parameters that cause no side-effects, the result is constant with respect to that parameter list (sometimes called referential transparency), i.e. if the pure function is again called with the same parameters, the same result will be returned (this can enable caching optimizations such as memoization).

Although there are workarounds on this (as InfantPro'Aravind' pointed out), it is not recommended to do such things; by doing it, you're ruining one of the most significant XSLT benefits.

like image 45
penartur Avatar answered Nov 16 '22 02:11

penartur