XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Bruce CantorSubject: 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 &gt;= $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.

Postnext
Ivan PedruzziSubject: How does one add working days to a date in XSLT?
Author: Ivan Pedruzzi
Date: 09 Mar 2023 06:29 PM
Originally Posted: 09 Mar 2023 06:28 PM
See attached solution which makes use of functions instead of template making the calling code less verbose


Ivan Pedruzzi
Stylus Studio Team


Documentadd-date.zip

Postnext
Bruce CantorSubject: How does one add working days to a date in XSLT?
Author: Bruce Cantor
Date: 10 Mar 2023 09:52 AM
yes, that actually runs, although it is not giving me the dates that I was expecting.

I am trying to achive this result, based on the fact that packages are not delivered not transported on weekends:

Start date being today Friday, the 10th of March

transit time(1) day => Delivery date 13 / 3
transit time(2) day => Delivery date 14 / 3
transit time(3) day => Delivery date 15 / 3
transit time(4) day => Delivery date 16 / 3
transit time(5) day => Delivery date 17 / 3
transit time(6) day => Delivery date 20 / 3
transit time(7) day => Delivery date 21 / 3

Which gives me an difference of:
diff:

2023-03-13+01:00 (0)
2023-03-13+01:00 (-1)
2023-03-13+01:00 (-2)
2023-03-14+01:00 (-2)
2023-03-15+01:00 (-2)
2023-03-16+01:00 (-4)
2023-03-17+01:00 (-4)

I think the problem lays in the offset function, will try to see if I can make it work.
Thank you very much for trying to help.


Posttop
Bruce CantorSubject: How does one add working days to a date in XSLT?
Author: Bruce Cantor
Date: 10 Mar 2023 10:49 AM
Originally Posted: 10 Mar 2023 09:52 AM
yes, that actually runs, although it is not giving me the dates that I was expecting.

I am trying to achive this result, based on the fact that packages are not delivered nor transported on weekends:

Start date being today Friday, the 10th of March

transit time(1) day => Delivery date 13 / 3
transit time(2) day => Delivery date 14 / 3
transit time(3) day => Delivery date 15 / 3
transit time(4) day => Delivery date 16 / 3
transit time(5) day => Delivery date 17 / 3
transit time(6) day => Delivery date 20 / 3
transit time(7) day => Delivery date 21 / 3

Which gives me an difference of:
diff:

2023-03-13+01:00 (0)
2023-03-13+01:00 (-1)
2023-03-13+01:00 (-2)
2023-03-14+01:00 (-2)
2023-03-15+01:00 (-2)
2023-03-16+01:00 (-4)
2023-03-17+01:00 (-4)

I think the problem lays in the offset function, will try to see if I can make it work.
Thank you very much for trying to help.



 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.