Friday, March 7, 2014

Difference between dates using XSL

How to find difference between dates using XSL.



You will have to use xsl stylesheet version 2.0. 
Using version 2.0 is very important since it doesn't work as expected in version 1.0

The following function would subtract the dates and return duration between them as output.

xsd:dateTime(xp20:format-dateTime(/ns0:Input2/ns0:Date2,"[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01]")) - xsd:dateTime(xp20:format-dateTime(/ns0:Input2/ns0:Date1,"[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01]"))




Sample Input and output:

Date2: 2014-03-17T15:00:00
Date1: 2014-03-15T15:00:00

Output: P2D      [Since it return the period. P stands for Period,  D stands for Days and the prefix 2 is number of Days.]


Date2: 2014-03-17T15:12:00
Date1: 2014-03-17T15:12:00

Output: P34DT11M55S   [This implies difference is 34 Days, 11 Minutes and 55 Seconds. ]

Note: Difference will never be in Months or Years. It will always calculate only Days.




Complete XSL:



<?xml version="1.0" encoding="UTF-8" ?>
<?oracle-xsl-mapper
  <!-- SPECIFICATION OF MAP SOURCES AND TARGETS, DO NOT MODIFY. -->
  <mapSources>
    <source type="XSD">
      <schema location="../xsd/untitled1.xsd"/>
      <rootElement name="Input2" namespace="http://www.example.org"/>
      <param name="dataObject3" />
    </source>
  </mapSources>
  <mapTargets>
    <target type="XSD">
      <schema location="../xsd/untitled1.xsd"/>
      <rootElement name="Input" namespace="http://www.example.org"/>
    </target>
  </mapTargets>
  <!-- GENERATED BY ORACLE XSL MAPPER 11.1.1.7.0(build 130301.0647.0008) AT [THU JAN 16 14:42:39 IST 2014]. -->
?>
<xsl:stylesheet version="2.0"
                xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/"
                xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
                xmlns:mhdr="http://www.oracle.com/XSL/Transform/java/oracle.tip.mediator.service.common.functions.MediatorExtnFunction"
                xmlns:bpel="http://docs.oasis-open.org/wsbpel/2.0/process/executable"
                xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:ns0="http://www.example.org"
                xmlns:dvm="http://www.oracle.com/XSL/Transform/java/oracle.tip.dvm.LookupValue"
                xmlns:hwf="http://xmlns.oracle.com/bpel/workflow/xpath"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:med="http://schemas.oracle.com/mediator/xpath"
                xmlns:ids="http://xmlns.oracle.com/bpel/services/IdentityService/xpath"
                xmlns:bpm="http://xmlns.oracle.com/bpmn20/extensions"
                xmlns:xdk="http://schemas.oracle.com/bpel/extension/xpath/function/xdk"
                xmlns:xref="http://www.oracle.com/XSL/Transform/java/oracle.tip.xref.xpath.XRefXPathFunctions"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:bpmn="http://schemas.oracle.com/bpm/xpath"
                xmlns:ora="http://schemas.oracle.com/xpath/extension"
                xmlns:socket="http://www.oracle.com/XSL/Transform/java/oracle.tip.adapter.socket.ProtocolTranslator"
                xmlns:ldap="http://schemas.oracle.com/xpath/extension/ldap"
                exclude-result-prefixes="xsi xsl ns0 xsd bpws xp20 mhdr bpel oraext dvm hwf med ids bpm xdk xref bpmn ora socket ldap">
  <xsl:template match="/">
    <ns0:Output>
           <ns0:element5>
        <xsl:value-of select='xsd:dateTime(xp20:format-dateTime(/ns0:Input2/ns0:Date2,"[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01]")) - xsd:dateTime(xp20:format-dateTime(/ns0:Input2/ns0:Date1,"[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01]"))'/>
      </ns0:element5>
    </ns0:Output>
  </xsl:template>
</xsl:stylesheet>



Sample project available here: https://drive.google.com/folderview?id=0B-B6k-1tHlm4T01DV2ptNTRXTjQ&usp=sharing

If you find any issues, please leave a comment.


1 comment:

  1. Hi Vinod,

    Nice Post.. Just a small query, i have downloaded the sample project.. But can you please guide me how can I import or get this into my jdeveloper.

    Thanks
    Diwaker

    ReplyDelete