Getting current date and time in XSLT 1.0

With XSLT 2.0, a method is provided, which quite simply determines the current date or time.

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

Unfortunately, this is not possible with XSLT 1.0, but there is the option to realize this using Java or JavaScript resources within the stylesheet. Here is an example, using Java, that works with Xalan and is default available in the ORACLE JRE.

First, create the appropriate namespace:

<?xml version="1.0" encoding="UTF-8"?>  
<xsl:stylesheet version="1.0"   
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   
                xmlns:java="http://xml.apache.org/xslt/java"  
                exclude-result-prefixes="java">  
...
</xsl:stylesheet>

Next, use the Java language to retrieve and format the current date and time. Here is an example:

<xsl:value-of select="java:format(java:java.text.SimpleDateFormat.new('yyyy-MM-dd HH:mm:ss.SSS'), java:java.util.Date.new())" />

Finally use a appropriate XML file including the XSL. The generated output look like:

2012-07-18 23:30:10.552

Below the complete listing

Example XML File:

<?xml version="1.0" encoding="UTF-8" ?> 
<?xml-stylesheet type="text/xsl" href="temp.xsl" ?>
<books>
    <book>
        <publisher>Addison-Wesley</publisher>
        <author>Walter Savitch</author>
        <title>Absolute Java</title>
        <publicationDate/>
    </book>
    <book>
        <publisher>O'Reilly</publisher>
        <author>Ryan Benedetti &#x26; Ronan Cranley</author>
        <title>Head First jQuery</title>
        <publicationDate/>
    </book>
</books>

Example XSL File:

<?xml version="1.0" encoding="UTF-8"?>  
<xsl:stylesheet version="1.0"   
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   
                xmlns:java="http://xml.apache.org/xslt/java"  
                exclude-result-prefixes="java">  
                
    <xsl:output method="html" version="4.0" encoding="utf-8" indent="yes"/>
    
    <xsl:template match="/">
        <html>
            <body>
                <h2>My Book Collection</h2>
                <table cellpadding="2" cellspacing="2" border="1">
                    <tr>
                        <th>Publisher</th>
                        <th>Author</th>
                        <th>Title</th>
                        <th>PublicationDate</th>
                    </tr>
                    <xsl:for-each select="books/book">
                        <tr>
                            <td>
                                <xsl:value-of select="publisher" disable-output-escaping="yes"/>
                            </td>
                            <td>
                                <xsl:value-of select="author" disable-output-escaping="yes"/>
                            </td>
                            <td>
                                <xsl:value-of select="title" disable-output-escaping="yes"/>
                            </td>
                            <td>
                                <xsl:value-of select="java:format(java:java.text.SimpleDateFormat.new('yyyy-MM-dd HH:mm:ss.SSS'), java:java.util.Date.new())" />
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
 
</xsl:stylesheet> 

Example XSLT Output:

<html>
    <body>
        <h2>My Book Collection</h2>
        <table cellpadding="2" cellspacing="2" border="1">
            <tr>
                <th>Publisher</th>
                <th>Author</th>
                <th>Title</th>
                <th>PublicationDate</th>
            </tr>
            <tr>
                <td>Addison-Wesley</td>
                <td>Walter Savitch</td>
                <td>Absolute Java</td>
                <td>2012-07-18 23:30:10.552</td>
            </tr>
            <tr>
                <td>O'Reilly</td>
                <td>Ryan Benedetti & Ronan Cranley</td>
                <td>Head First jQuery</td>
                <td>2012-07-18 23:30:10.552</td>
            </tr>
        </table>
    </body>
</html>

This entry was posted in Java, XML, XSL and tagged , , . Bookmark the permalink.

6 Responses to Getting current date and time in XSLT 1.0

  1. Goodman says:

    Thank you for your post about XSLT date convertions. It was very usfuel. I was looking for a way to convert a dates without using explicit Xpath date convertion functions and you gave just that. Thank you again.

  2. Amit says:

    This is very helpful, simple and effective solution!!!
    Thanks for sharing!!

  3. Sunit Tyagi says:

    Hi,
    By this function java:format(java:java.text.SimpleDateFormat.new(‘yyyy-MM-dd HH:mm:ss.SSS’), java:java.util.Date.new())” we are getting date & time but my zone is +5:30 so i am gettting less then approx 6 hour more then our time.

    It will be a great help….
    Thanks

  4. Awanish Kumar says:

    Awesome man….

  5. Munazir Hashim says:

    Thank you Uwe!,

    You made my life that much more easier!

    Thanks pal!

  6. Morten Slott Hansen says:

    This is really good stuff !!

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.