XSLT - Modify Date and DateTime value

If you want to perform date and time related opertions with XSLT 'date' or 'dateTime' values, you can use 'xs:yearMonthDuration' or 'xs:dayTimeDuration' function to achieve this. You can add or reduce years, months, date, hours or even minutes through those functions.


 xs:yearMonthDuration

For this function input value is year month duration and it should be passed in ISO_8601 format

n- the [n] is replaced by the value for each of the date and time elements
P - The duration designator(for period).
nY- the number of years.
nM- the number of months.

ex: xs:yearMonthDuration('P1Y2M') - 1 Year and 2 Months
ex: xs:yearMonthDuration('-P1Y2M') - Negative 1 Year and 2 Months

xs:dayTimeDuration

For this function input value is day time duration and it also needs to be passed in ISO_8601 format

n- the [n] is replaced by the value for each of the date and time elements
P - The duration designator(for period).
nD- the number of days.
T- The date and time separator.
nH- the number of hours.
nM- the number of minutes.
nS - the number of seconds.

ex: xs:dayTimeDuration('P3D') - 3 Days
ex: xs:dayTimeDuration('-P3D') - Negative 3 days
ex: xs:dayTimeDuration('P6DT50H80S') - 6 days, 50 hours, and 80 seconds


Example XSLT :

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

  <xsl:output method="text"/>

  <xsl:variable name="dT" as="xs:dateTime"   select="xs:dateTime('2016-02-29T00:00:01')"/>
  <xsl:variable name="d" as="xs:date" select="xs:date('2016-02-29')"/>

  <xsl:template match="/">
       <xsl:text>Add 1 Year and 2 Months : </xsl:text> <xsl:value-of select="($d+ xs:yearMonthDuration('P1Y2M'))"/>
       <xsl:text>&#xA;Negative 1 Year and 2 Month : </xsl:text> <xsl:value-of select="($d+ xs:yearMonthDuration('-P1Y2M'))"/>

       <xsl:text>&#xA;Add 3 Days : </xsl:text> <xsl:value-of select="($dT+ xs:dayTimeDuration('P3D'))"/>
       <xsl:text>&#xA;Negative 3 days : </xsl:text> <xsl:value-of select="($dT+xs:dayTimeDuration('-P3D'))"/>
<xsl:text>&#xA;Add 6 days, 50 hours, and 80 seconds : </xsl:text> <xsl:value-of select="($dT+ xs:dayTimeDuration('P6DT50H80S'))"/>
  </xsl:template>
</xsl:stylesheet>


Comments

Popular posts from this blog

Secure Spring Boot REST API using Basic Authentication

Building a RESTFul Service using Spring Boot

Integrating Swagger with Spring Boot REST API