XSLT Date and Time Manipulation

July 03, 2024 ・0comments

In XSLT (Extensible Stylesheet Language Transformations) 2.0 or later, we can perform several date and time manipulation processes. I thought of writing a blog post so others can easily find examples as well.

Basic Date & Time XSLT Functions: 

current-date() - Returns the current date of the server that XSLT runs

current-time() - Returns the current time of the server that XSLT runs

current-dateTime() - Returns the current date and time of the server that XSLT runs

implicit-timezone() - Returns the implicit time zone of the server that XSLT runs

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:template match="/">
        <root>
            <currentDate>
                <xsl:value-of select="current-date()"/>
            </currentDate>
            <currentTime>
                <xsl:value-of select="current-time()"/>
            </currentTime>
            <currentdateTime>
                <xsl:value-of select="current-dateTime()"/>
            </currentdateTime>
            <implicitTimezone>
                <xsl:value-of select="implicit-timezone()"/>
            </implicitTimezone>
        </root>
    </xsl:template>
</xsl:stylesheet>

Date & Time Extracting XSLT Function:

year-from-date() - Extracts the year from a date.

month-from-date() - Extracts the month from a date.

day-from-date() - Extracts the day from a date.

hours-from-time() - Extracts the hours from a time.

minutes-from-time() - Extracts the minutes from a time.

seconds-from-time() - Extracts the seconds from a time.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:template match="/">
        <root>
            <year>
                <xsl:value-of select="year-from-date(current-date())"/>
            </year>
            <month>
                <xsl:value-of select="month-from-date(current-date())"/>
            </month>
            <day>
                <xsl:value-of select="day-from-date(current-date())"/>
            </day>
            <hours>
                <xsl:value-of select="hours-from-time(current-time())"/>
            </hours>
            <minutes>
                <xsl:value-of select="minutes-from-time(current-time())"/>
            </minutes>
            <seconds>
                <xsl:value-of select="seconds-from-time(current-time())"/>
            </seconds>
        </root>
    </xsl:template>
</xsl:stylesheet>


Date and Time Manipulation Functions:

Please note Saxon doesn't supports add-yearMonthDuration-to-date and other related 3 operations. So here is my workaround to manipulate dates.  Please refer my old blog post on this

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:template match="/">
        <root>
            <add-yearMonthDuration-to-date>
                <xsl:value-of select="current-dateTime() + xs:yearMonthDuration('P1Y6M')"/><!-- Add 1 year and 6 months -->
            </add-yearMonthDuration-to-date>
            <add-dayTimeDuration-to-date>
                <xsl:value-of select="current-dateTime() + xs:dayTimeDuration('P10DT2H')"/><!-- Add 10 days and 2 hours -->
            </add-dayTimeDuration-to-date>
            <subtract-yearMonthDuration-from-date>
                <xsl:value-of select="current-dateTime() + xs:yearMonthDuration('-P1Y6M')"/><!-- Subtract 1 year and 6 months -->
            </subtract-yearMonthDuration-from-date>
            <subtract-dayTimeDuration-from-date>
                <xsl:value-of select="current-dateTime() + xs:dayTimeDuration('-P10DT2H')"/><!-- Subtract 10 days and 2 hours -->
            </subtract-dayTimeDuration-from-date>
        </root>
    </xsl:template>
</xsl:stylesheet>

String Format Conversion Functions:

format-date() - Formats a date according to a given pattern.

format-time() - Formats a time according to a given pattern.

format-dateTime() - Formats a date time according to a given pattern.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:template match="/">
        <root>
            <formattedDate>
                <xsl:value-of select="format-date(current-date(), '[Y0001]-[M01]-[D01]')"/>
            </formattedDate>
            <formattedTime>
                <xsl:value-of select="format-time(current-time(), '[H01]:[m01]:[s01]')"/>
            </formattedTime>
            <formattedDateTime>
                <xsl:value-of select="format-dateTime(current-dateTime(), '[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01]')"/>
            </formattedDateTime>
        </root>
    </xsl:template>
</xsl:stylesheet>

Compare Dates :

When comparing dates with XSLT 2.0 or later, we can use default XSLT comparison operations such as 'lt' (less than), 'gt' (greater than), etc.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:template match="/">
        <root>
            <dateCompareLessThan>
                <xsl:value-of select="current-date() lt (current-date() + xs:yearMonthDuration('P1M'))"/>
            </dateCompareLessThan>
            <dateCompareNotLessThan>
                <xsl:value-of select="(current-date() + xs:dayTimeDuration('P1D')) lt current-date() "/>
            </dateCompareNotLessThan>
            <dateCompareGreaterThan>
                <xsl:value-of select="current-date() gt (current-date() - xs:dayTimeDuration('P1D'))"/>
            </dateCompareGreaterThan>
        </root>
    </xsl:template>
</xsl:stylesheet>

You can use an online XSLT playground such as xslttest to verify the examples listed above.

Please refrain from copying content to other websites

Post a Comment