2

I have a strange requirement.

I have a variable in xslt containing the months, with their id (1-12)

The issue is I need to display them all, but starting with a month other than January(1).

Currently I have the following

<xsl:variable name="months">
    <Months>
        <Month ID="1">JAN</Month>
        <Month ID="2">FEB</Month>
        <Month ID="3">MAR</Month>
        <Month ID="4">APR</Month>
        <Month ID="5">MAY</Month>
        <Month ID="6">JUN</Month>
        <Month ID="7">JUL</Month>
        <Month ID="8">AUG</Month>
        <Month ID="9">SEP</Month>
        <Month ID="10">OCT</Month>
        <Month ID="11">NOV</Month>
        <Month ID="12">DEC</Month>
    </Months>
</xsl:variable>

Then I iterate with this so that I can start at a given month

<xsl:for-each select="msxsl:node-set($months)//Month[@ID >= $startAtMonth]">
    <xsl:sort data-type="number" select="@ID"/>
    <th>
        <xsl:value-of select="text()"/>
    </th>
</xsl:for-each>
<xsl:for-each select="msxsl:node-set($months)//Month[not(@ID >= $startAtMonth)]">
    <xsl:sort data-type="number" select="@ID"/>
    <th>
        <xsl:value-of select="text()"/>
    </th>
</xsl:for-each>

But it requires two for-each statements, and this for-each will be needed in a few places. Is there a more concise way to write this so it is one loop?

1 Answer 1

3

How about using a modulo operation?

<xsl:sort data-type="number" select="(number(@ID)+12-$startAtMonth) mod 12"/>
Sign up to request clarification or add additional context in comments.

1 Comment

Nice, I was trying to come up with something similar and just couldn't...Mondays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.