[SOLVED] How to add 2 days excluding sat and sun days in XSLT

Issue

I am using XSLT 2.0, there is no date input from the XML file so I have to use the current date for this operation and produce the output in HTML.

The condition is to add 2 days from the current date and exclude Saturday and Sunday from it.

Example:

The current date is 14th July 2022 Thursday
The Expected date is 18th July 2022 Monday
Another example:

The current date is 15th July 2022 Friday
The expected date is 19th July 2022 Tuesday

Could you help me with this?

Solution

Here’s a simple (not to say "primitive" ) way you could look at it:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:param name="today" select="current-date()"/>

<xsl:template match="/">
    <xsl:variable name="day-of-week" select="format-date($today, '[FNn, *-3]')" />
    <xsl:variable name="add">
        <xsl:choose>
            <xsl:when test="$day-of-week='Sat'">3</xsl:when>
            <xsl:when test="$day-of-week='Fri'">4</xsl:when>
            <xsl:when test="$day-of-week='Thu'">4</xsl:when>
            <xsl:otherwise>2</xsl:otherwise>
        </xsl:choose>
    </xsl:variable> 
    <result>
        <xsl:value-of select="$today + $add * xs:dayTimeDuration('P1D')"/>
    </result>
</xsl:template>
  
</xsl:stylesheet>

A smarter way would calculate the number of days to add – but I think in this case it’s not worth the bother.

A better way would examine each added day in a loop – subtracting it from the count only if it’s a work day. That is the only way I know of that would also consider holidays.

Answered By – michael.hor257k

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published.