How do we test whether a date is within 180 days f

2020-02-07 06:05发布

问题:

I am writing an xslt for datapower and in that I am getting a date(Payment Date).I have to check whether that date(Paymentt Date) is within 180 days of current date

I am getting present date by the following

<xsl:variable name="timestamp" select="date:date-time()"/>

Now how to check the condition for 180 days

Following is my xslt

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dp="http://www.datapower.com/extensions"
    xmlns:date="http://exslt.org/dates-and-times"
    xmlns:dpconfig="http://www.datapower.com/param/config"
    extension-element-prefixes="dp"
    exclude-result-prefixes="dp dpconfig"
>

  <xsl:output method="xml"/>

  <xsl:template match="/">
<PaymentDate><xsl:value-of select="dp:http-request-header('X-payment-date')"/></PaymentDate>  (From request I am getting payment date)

<xsl:variable name="timestamp" select="date:date-time()"/>        (From here I am getting present date)

<xsl:if  (this is what I am confused)

Thanks

回答1:

Calculating date difference in XSLT 1.0:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <output>
        <difference>
            <xsl:call-template name="date-difference">
                <xsl:with-param name="date1" select="input/originalDate" />
                <xsl:with-param name="date2" select="date:date-time()" />
            </xsl:call-template>
        </difference>
    </output>
</xsl:template> 

<xsl:template name="date-difference">
    <xsl:param name="date1"/>
    <xsl:param name="date2"/>
    <xsl:param name="JDN1">
        <xsl:call-template name="JDN">
            <xsl:with-param name="date" select="$date1" />
        </xsl:call-template>
    </xsl:param>
    <xsl:param name="JDN2">
        <xsl:call-template name="JDN">
            <xsl:with-param name="date" select="$date2" />
        </xsl:call-template>
    </xsl:param>
    <xsl:value-of select="$JDN2 - $JDN1"/>
</xsl:template> 

<xsl:template name="JDN">
    <xsl:param name="date"/>
    <xsl:param name="year" select="substring($date, 1, 4)"/>
    <xsl:param name="month" select="substring($date, 6, 2)"/>
    <xsl:param name="day" select="substring($date, 9, 2)"/>
    <xsl:param name="a" select="floor((14 - $month) div 12)"/>
    <xsl:param name="y" select="$year + 4800 - $a"/>
    <xsl:param name="m" select="$month + 12*$a - 3"/>
    <xsl:value-of select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />
</xsl:template> 

</xsl:stylesheet>

When applied to the following input XML:

<input>
    <originalDate>2013-09-15</originalDate>
</input>

The result will be:

<?xml version="1.0" encoding="UTF-8"?>
<output>
  <difference>179</difference>
</output>

if the transformation is performed today, 2014-03-13.



回答2:

IBM Datapower provides extension to existing EXSLT function. There is a function called 'difference' with following signature.

difference() Returns the duration between the first date and the second date. Syntax date:difference(start-dateTime, end-dateTime)

You can utilize this function to find out the date-difference. For more information about extension function available in datapower see below link. Go to Chapter 5 and look for functions for manipulating 'date time'.

http://pic.dhe.ibm.com/infocenter/wsdatap/v3r8m1/topic/xm70/ExtensionFunctions.pdf

-Ajitabh



回答3:

DataPower Supports Date:Difference

difference()

Returns the duration between the first date and the second date. Syntax

date:difference(start-dateTime, end-dateTime)

You can pass current date as end-DateTime , date which wants to check as start-DateTime

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
xmlns:date="http://exslt.org/dates-and-times" version="1.0">
<!--<xsl:key name="lang" match="element" use="@language"></xsl:key>-->
<xsl:template match="/">
    <xsl:variable name="date1" select="date:date-time()"/>
    <xsl:variable name="date2" select="'2013-09-15'"/>
    <output>
        <difference>
    <xsl:value-of select="translate(date:difference($date2, $date1),'PD','')"/>
        </difference>
    </output>
</xsl:template>
</xsl:stylesheet>