|
next
|
Subject: How does one add working days to a date in XSLT? Author: Bruce Cantor Date: 09 Mar 2023 03:12 PM
|
Hi forum
I am a bit stuck on how to calculate a date:
I need to be able to add a number of days to a date and return the resulting date.
The tricky part is that the calculation must consider and skip any days being Saturday and Sunday.
I would like to be able to calculate Estimated time of Arrival, and the carriers do not transport or deliver on weekends.
I tried to create a template that call itself recursive - but every attempt i do ends up with an error "Too many nested template or function calls".
My latest attempt :
<xsl:template name="add-period" as="xs:date">
<xsl:param name="date" as="xs:date"/>
<xsl:param name="period" as="xs:dayTimeDuration"/>
<xsl:variable name="workdays" select="tokenize('Monday,Tuesday,Wednesday,Thursday,Friday', ',')"/>
<xsl:variable name="new-date" select="$date + $period"/>
<xsl:choose>
<xsl:when test="$date >= $new-date">
<xsl:value-of select="$date"/>
</xsl:when>
<xsl:when test="not($date = $new-date)">
<xsl:variable name="next-day" select="$date + xs:dayTimeDuration('P1D')" as="xs:date"/>
<xsl:choose>
<xsl:when test="index-of($workdays, format-date($next-day, '[FNn]'))">
<xsl:call-template name="add-period">
<xsl:with-param name="date" select="$next-day"/>
<xsl:with-param name="period" select="$period"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="add-period">
<xsl:with-param name="date" select="$date" as="xs:date"/>
<xsl:with-param name="period" select="$period" as="xs:dayTimeDuration"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$new-date"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Can anyone see why this happens?
The number of days that I am trying to add is small so it must be something that the 'compiler' sees, not an runtime error.
|
|
|