Oracle SOA Suite Online Training

Interested in learning Oracle SOA Suite 12c?
Learn from the author of this blog!
A complete and comprehensive course on the #1 platform on SOA - Oracle SOA Suite

Click here to find the complete course details
Click here to check the first session on Oracle SOA Suite 12c

================================================================================================

XSLT : Duration between 2 Dates

XSLT 2.0 lets you calculate the date difference very easily.

My Input is as below

<?xml version="1.0" encoding="UTF-8" ?>
<input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.example.org">
   <benefitsDeliveryServiceEndDate>2015-02-11-05:00</benefitsDeliveryServiceEndDate>
   <cboNextChargeDate><![CDATA[2015-12-11]]></cboNextChargeDate>
</input>

One thing to make sure is that the formats has to be the same. If the date formats are different, then make them into the same either using xp20:format-dateTime or using substrings.

Here is the xslt that calculates the duration b/w the two dates. I'm trimming the result to get only the number of days, you can do as per your requirement.

<xsl:stylesheet version="2.0"
                xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
                xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/"
                xmlns:bpel="http://docs.oasis-open.org/wsbpel/2.0/process/executable"
                xmlns:mhdr="http://www.oracle.com/XSL/Transform/java/oracle.tip.mediator.service.common.functions.MediatorExtnFunction"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:ns0="http://www.example.org"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
  <xsl:template match="/">
    <ns0:dateDiff>
      <ns0:daysToBump>
        <xsl:variable name="bfDlvServEndDate" select="/ns0:input/ns0:benefitsDeliveryServiceEndDate"/>
        <xsl:variable name="cboServiceDate" select="/ns0:input/ns0:cboNextChargeDate"/>
            <!--<xsl:variable name="currentDate" select="xp20:current-date()"/>-->

            <!-- DateDifference Calculation -->
            <xsl:variable name="daysToBump" select="(xsd:date($bfDlvServEndDate) - xsd:date($cboServiceDate))"/>
            <!-- Convert date to string so that you can trim to get the Date -->
            <xsl:variable name="daysToBump" select="xsd:string($daysToBump)"/>
            <xsl:choose>
                <xsl:when test="contains($daysToBump,'D')">
                    <xsl:value-of select="substring-before(substring-after(xsd:string($daysToBump),'P'),'D')"/>
                </xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
      </ns0:daysToBump>
    </ns0:dateDiff>
  </xsl:template>
</xsl:stylesheet>

Will give the following result

<?xml version = '1.0' encoding = 'UTF-8'?>
<ns0:dateDiff xmlns:mhdr="http://www.oracle.com/XSL/Transform/java/oracle.tip.mediator.service.common.functions.MediatorExtnFunction" xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20" xmlns:bpel="http://docs.oasis-open.org/wsbpel/2.0/process/executable" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns0="http://www.example.org" xsi:schemaLocation="http://www.example.org file:/C:/JDeveloper/mywork/SampleSOAApps/Project1/xsl/untitled1.xsd">
   <ns0:daysToBump>302</ns0:daysToBump>
</ns0:dateDiff>

No comments:

Post a Comment