Strange sorting requirement with XSLT
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>
开发者_C百科<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?
How about using a modulo operation?
<xsl:sort data-type="number" select="(number(@ID)+12-$startAtMonth) mod 12"/>
精彩评论