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="" xmlns="">

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"
  <xsl:template match="/">
        <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:when test="contains($daysToBump,'D')">
                    <xsl:value-of select="substring-before(substring-after(xsd:string($daysToBump),'P'),'D')"/>

Will give the following result

<?xml version = '1.0' encoding = 'UTF-8'?>
<ns0:dateDiff xmlns:mhdr="" xmlns:bpws="" xmlns:xsi="" xmlns:xp20="" xmlns:bpel="" xmlns:xsd="" xmlns:ns0="" xsi:schemaLocation=" file:/C:/JDeveloper/mywork/SampleSOAApps/Project1/xsl/untitled1.xsd">

No comments:

Post a Comment