Abhijeet
Abhijeet

Reputation: 417

in xslt2.0 , how to add extra minute value to existing time stamp

I had a time stamp in my input xml as below

2012-01-19T21:36:33.085+01:00

now i want to add some minute value to it . the different values of minutes are as below 30,-30,60,-60

Is there any xslt function to do this addition and give the output as of type xs:dateTime

Upvotes: 0

Views: 3365

Answers (2)

Daniel Haley
Daniel Haley

Reputation: 52858

You can cast your minutes as a duration and then add/subtract. (Subtract if it's a negative number.)

Example...

XML Input

<test>2012-01-19T21:36:33.085+01:00</test>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" extension-element-prefixes="xs">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:param name="minutesToAdd" select="30" as="xs:integer"/>

    <xsl:template match="/*">
        <results>
            <xsl:choose>
                <xsl:when test="0 > $minutesToAdd">
                    <xsl:value-of select="xs:dateTime(.) - xs:dayTimeDuration(concat('PT',abs($minutesToAdd),'M'))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="xs:dateTime(.) + xs:dayTimeDuration(concat('PT',$minutesToAdd,'M'))"/>
                </xsl:otherwise>
            </xsl:choose>
        </results>  
    </xsl:template>

</xsl:stylesheet>

Output

<results>2012-01-19T22:06:33.085+01:00</results>

Upvotes: 2

David Carlisle
David Carlisle

Reputation: 5652

<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        >

<xsl:template name="main">
 <xsl:value-of select="xs:dateTime('2012-01-19T21:36:33.085+01:00') + 
               xs:dayTimeDuration('PT0H30M')"/>
</xsl:template>

</xsl:stylesheet>

produces

2012-01-19T22:06:33.085+01:00

Upvotes: 0

Related Questions