-1

How to get start and end of a week of a specific date using xslt?

Abdelhameed Mahmoud
  • 2,118
  • 2
  • 22
  • 17

2 Answers2

0

Note: this was posted in response to the original post that included an XSLT 2.0 user-defined function. This is why the following is also in the same format. Doing this in XSLT 1.0 is a bit more difficult, but definitely possible.

Couldn't this be shorter/simpler? For example:

<xsl:function name="my:thisMonday">
    <xsl:param name="date"/> 
    <xsl:variable name="epoch" select="xs:date('0001-01-01')"/> 
    <xsl:variable name="dayNumber" select="fn:days-from-duration($date - $epoch)"/> 
    <xsl:variable name="dayOfWeek" select="$dayNumber mod 7"/> 
    <xsl:value-of select="$date - xs:dayTimeDuration(concat('P', $dayOfWeek, 'D' ))"/>
</xsl:function>

Note: the following namespaces must be declared in the stylesheet:

xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:my="http://www.example.com/my"
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
0

I had a look around on internet to find a xsl code to calculate start and end of week of a specific date but I was not lucky and I prepared the following function and I like to share it here in order to help others.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:myfn="http://myfn.com">    
    <xsl:output encoding="UTF-8"/>

    <xsl:function name="myfn:startAndEndOfWeek">
        <xsl:param name="date" />

        <xsl:variable name="days">
            <day weekday="0" name="Sunday" add="0" sub="6" />
            <day weekday="1" name="Monday" add="6" sub="0" />
            <day weekday="2" name="Tuesday" add="5" sub="1" />
            <day weekday="3" name="Wednesday" add="4" sub="2" />
            <day weekday="4" name="Thursday" add="3" sub="3" />
            <day weekday="5" name="Friday" add="2" sub="4" />
            <day weekday="6" name="Saturday" add="1" sub="5" />
        </xsl:variable>

        <xsl:variable name="specificDate" select="xs:date($date)" />
        <xsl:variable name="weekday" select="myfn:day-of-week($specificDate)" />


        <xsl:variable name="daysToSubtract" select="$days/*[@weekday=$weekday]/@sub" />
        <xsl:variable name="weekStartsOn" select="$specificDate - (xs:dayTimeDuration(concat('P', $daysToSubtract, 'D')))" />

        <xsl:variable name="daysToAdd" select="$days/*[@weekday=$weekday]/@add" />
        <xsl:variable name="weekEndsOn" select="$specificDate + (xs:dayTimeDuration(concat('P', $daysToAdd, 'D')))" />

        <xsl:value-of select="concat('WeekStartsOn: ', $weekStartsOn,' and WeekEndsOn: ',$weekEndsOn)"/>
    </xsl:function> 

    <xsl:function name="myfn:day-of-week" as="xs:integer?" >
        <xsl:param name="date" as="xs:anyAtomicType?"/> 
        <xsl:sequence select=" if (empty($date)) then () else xs:integer((xs:date($date) - xs:date('1901-01-06')) div xs:dayTimeDuration('P1D')) mod 7"/>
    </xsl:function>

</xsl:stylesheet>

assuming that week starts on Monday and ends on Sunday.

hope this helps.

if more help is needed, leave a comment.

Abdo

Abdelhameed Mahmoud
  • 2,118
  • 2
  • 22
  • 17