0
votes

I am new to XSLT code and need experties help. I have got a requirement to generate the beginning of the month dynamically from 2018-10-01 (YYYY-MM-DD) till previous month beginning of the month i.e 2019-04-01 (current month is May1st). So every month when we run this XSLT code will add the previous month into the snippet.

I tried using below function also included namespaces as below xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:functx="http://www.functx.com" xmlns:fn="http://www.w3.org/2005/xpath-functions".

But it is saying "Cannot find a 2-argument function named {http://www.functx.com}yearMonthDuration()". I am using this with Oxygen XML editor.

Expected Output:

<?xml version="1.0" encoding="UTF-8"?>
<Year> 
<Month>2018-10-01</Month>     <Month>2018-11-01</Month>     <Month>2018-12-01</Month>     <Month>2019-01-01</Month>     <Month>2019-02-01</Month>     <Month>2019-03-01</Month>    <Month>2019-04-01</Month>      </Year>
1
yearMonthDuration is a datatype, not a function. You probably want to use the subtract-yearMonthDuration-from-date() function. Note that this requires an XSLT 2.0 processor. Oxygen is a testing environment that features several XSLT processors; you must use one that supports XSLT 2.0 or higher.michael.hor257k
Added: I see that Saxon does not support this function or its opposite add-yearMonthDuration-to-date(). But it's not necessary, because you can just use the + and - operators instead.michael.hor257k
functx is a library of functions implemented by Priscilla Walmsley in both XQuery and XSLT, which you can incorporate into your stylesheet or query - but you will need to download it and incorporate it into your code using xsl:include.Michael Kay

1 Answers

2
votes

Using an XSLT 2.0 processor, the following stylesheet:

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:template match="/">
    <Year>
        <xsl:call-template name="enumerate-dates"/>
    </Year>
</xsl:template>

<xsl:template name="enumerate-dates">
    <xsl:param name="startdate" as="xs:date" select="xs:date('2018-10-01')"/>
    <xsl:if test="$startdate le current-date() - xs:yearMonthDuration('P1M')">
        <Month>
            <xsl:value-of select="$startdate" />
        </Month>
        <xsl:call-template name="enumerate-dates">
            <xsl:with-param name="startdate" select="$startdate + xs:yearMonthDuration('P1M')" />
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

applied to any XML input, will return (in the current month of May 2019):

Result

<?xml version="1.0" encoding="utf-8"?>
<Year>
   <Month>2018-10-01</Month>
   <Month>2018-11-01</Month>
   <Month>2018-12-01</Month>
   <Month>2019-01-01</Month>
   <Month>2019-02-01</Month>
   <Month>2019-03-01</Month>
   <Month>2019-04-01</Month>
</Year>

Demo: https://xsltfiddle.liberty-development.net/94rmq6w


Added:

If you prefer, you can use a shorter but less obvious version:

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:template match="/">
    <Year>
        <!-- 12*2018 + 10 + 1 = 242257 -->
        <xsl:for-each select="0 to 12*year-from-date(current-date()) + month-from-date(current-date()) - 24227">
            <Month>
                <xsl:value-of select="xs:date('2018-10-01') + xs:yearMonthDuration(concat('P', ., 'M'))" />
            </Month>
        </xsl:for-each>
    </Year>
</xsl:template>

</xsl:stylesheet>

Demo: https://xsltfiddle.liberty-development.net/94rmq6w/1