I want to be able to do some basic reassignments to variables in XSLT. How can this be achieved?
I just want to be able to convert this to XSLT (ignoring the appendM
XSLT is a declarative language, it doesn't use stateful variables. You need to work out how to express your output as a function of the input, rather than thinking of a way to give low-level procedural instructions to the computer.
In your case it seems very straightforward; just use different variables:
if(currentMonth + count > 12) {
m2 = (currentMonth + count) - 12;
if (m2 < 10) then appendMonthWithZero(m2) else m2;
} else {
currentMonth
}
I just almost believe the viewpoint in other replies, before I test the follow code that can really run well in xlst proccessor saxon-he 9.8.0.12 my code :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="xs map"
version="2.0">
<xsl:template match="/">
<xsl:variable name="i1" select="123" as="xs:integer"/>
<xsl:variable name="s1" select="'abcd'" as="xs:string"/>
<xsl:variable name="d1" select="234.5" as="xs:float"/>
<!-- we test that variable v1 can be assignment multi times and it is ok. -->
<xsl:variable name="v1" select="$i1"/>
v1 is: <xsl:value-of select="$v1"/>
<xsl:variable name="v1" select="$s1"/>
v1 is: <xsl:value-of select="$v1"/>
<xsl:variable name="v1" select="$d1"/>
v1 is: <xsl:value-of select="$v1"/>
<xsl:variable name="map1" select="map{'haha':119, 'good':110}"/>
<xsl:variable name="map2" select="map:put($map1, 'go', 122)"/>
<xsl:variable name="map1" select="map:put($map2, 'hello', 999)"/>
map1(haha) is <xsl:sequence select="$map1?haha"/>
map1(haha) is <xsl:sequence select="$map1?go"/>
map1(hello) is <xsl:sequence select="$map1?hello"/>
</xsl:template>
</xsl:stylesheet>
the screenshort of running result
for your question. the solution maybe like that:
<xsl:template match="/" priority="20">
<xsl:variable name="month" select="9"/>
<xsl:variable name="count" select="5"/>
<xsl:variable name="month" select="if (($month + $count) > 12) then ($month + $count) mod 12 else $month"/>
month is:<xsl:value-of select="$month"/>
</xsl:template>