X

Subtracting 2 dateTime values into a duration using XPath 2.0

Ramkumar Menon
Director, Product Strategy

Here is a simple XSLT that allows you to subtract two xsd:dateTime values into a xsd:duration value.



The given XSLT uses XPath 2.0. Hence to get it to work, ensure that the "version" attribute on the <xsl:stylesheet> element is set to "2.0". I have used Oracle BPEL 10.1.3.1 for this illustration.



XSL Source



<xsl:stylesheet version="2.0" xmlns:ns1="http://www.output.org"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:ns0="http://www.input.org"
                exclude-result-prefixes="xsl xsd ns0 ns1">
  <xsl:template match="/">
    <ns1:output>
      <ns1:durationValue>
        <xsl:value-of select="(xsd:dateTime(/ns0:input/ns0:date1) - xsd:dateTime(/ns0:input/ns0:date2))"/>
      </ns1:durationValue>
    </ns1:output>
  </xsl:template>
</xsl:stylesheet>



Input



<?xml version="1.0" encoding="UTF-8" ?>
<input xmlns="http://www.input.org">
   <date1>2007-07-17T12:50:28.256</date1>
   <date2>2007-07-18T12:52:28.257</date2>
</input>



Output



<?xml version = '1.0' encoding = 'UTF-8'?>
<ns1:output xmlns:ns1="http://www.output.org">
   <ns1:durationValue>-P1DT2M0.1S</ns1:durationValue>
</ns1:output>



 

Join the discussion

Comments ( 1 )
  • Eric Elzinga Thursday, July 19, 2007
    Nice post!
    We have been struckling with this too in my current project.
    Better skip the graphical xsl mapper of bpel/esb, otherwise all the handmade-changes are gone again.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.