Vinay Katta
Vinay Katta

Reputation: 27

current time date format with milliseconds by using XSLT Version 1.0

My XML:

<root>
<Name>Test</Name>
<DOB>10-11-1989</DOB>
<root>

I need to get current time date format with milliseconds by using XSLT. I am using XSLT version-1.0..

After Trasformation my XML should look like below:

<root>
<Name>Test</Name>
<DOB>10-11-1989</DOB>
<currentDate>2014-05-21-01.25.32.000000</currentDate>
<root>

I found solutions for version-2.0 but, that doesn't help me.

Upvotes: 1

Views: 5472

Answers (2)

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

Here is an example, using Java, that works with Xalan and is default available in the ORACLE JRE.

<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 indent="yes"/>

    <xsl:strip-space elements="*"/>

    <xsl:template match="/root">
        <xsl:copy>
            <xsl:copy-of select="@*|node()"/>
            <dateTimeStamp>
                <xsl:value-of select="java:format(java:java.text.SimpleDateFormat.new('yyyy-MM-dd-HH.mm.ss.SSSSSS a'), java:java.util.Date.new())" />
            </dateTimeStamp>
        </xsl:copy>        
    </xsl:template>

</xsl:stylesheet>

From http://www.heber.it/?p=1053.

If you want to have 12-hr format, simply change

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

to

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

it outputs:

<root>
    <Test>tested</Test>
    <dateTimeStamp>2014-05-23-01.57.04.000441 PM</dateTimeStamp>
</root>

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 116992

XSLT 1.0 alone does not provide any means to get the current date or time.

If your processor supports it, you can use the date-time() EXSLT extension function - however, I don't think this will include milliseconds.

Alternatively, you could pass the timestamp as a parameter to the stylesheet during runtime.

Upvotes: 1

Related Questions