I can declare a variable “myVariable” with a value “111” in the global scope. But how can I re-assign a value locally. Or is there a alternative way to achieve this.
Please help. Thank you. Ravi
XSLT is not an imperative programming language where you can change the value of a variable. If you want to pass a value from one template to another then you can use a parameter e.g. The fact that you need to modify an <xsl:variable> shows that you are not thinking in XSLT.
XSLT <xsl:variable>The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!
You use the <xsl:variable> element to declare a variable and assign a value to it. Remember that these two must always be done at the same time in XSLT, you can't declare a variable and later assign it a value.
You can re-define the same variable inside a template:
<xsl:variable name="myVariable" select="'111'"/>
<xsl:template match="/">
<xsl:variable name="myVariable" select="'112'"/>
. . .
</xsl:template>
Note though that 'variables' in XSLT are actually constant - you are not re-assigning a different value to the same variable, you are re-defining it inside the template - outside the template myVariable
will still have the value 111
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With