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 for this illustration.

XSL Source

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


<?xml version="1.0" encoding="UTF-8" ?>
<input xmlns="http://www.input.org">


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


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.