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
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> + Use of before and after string (3) Sticky Topic
-> - How do I substitute element ty... (1)
-> - How does one add working days ... (4)
-> ->How does one add working ...
-> ->How does one add working ...
-> ->How does one add working ...
-> - Help, I have existing XLT and... (1)
-> + Need help on XSLT issue - (2)
-> + EDI to XML Conversion (7)
-> - XML To JSON Conversion using X... (1)
-> + Formatting Paragraphs to same ... (2)
-> - Grouping of records (1)
-> + Problems with xsd 1.1 (4)
-> + XML to HL7 mapping (3)
-> + XSLT 3 and Iterate (2)
-> + XSL-FO to PDF preview (3)
-> + java.lang.RuntimeException: Er... (2)
-> + Create Acroforms with Stylus X... (2)
-> + How to change XSLT parameter s... (3)
-> + how to change format of the da... (2)
-> + Search "Next 8 Results " doesn... (2)
-> - Support for Git (1)
-> + newbee (8)
-- [1-20] [21-40] [41-60] Next
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
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.



   
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.