How can I reassign a value to a variable previously assigned? I need it to works like this:
<xsl:variable name="variable2" select="'N'" />
....
<xsl:when test="@tip = '2' and $variable2 != 'Y'">
<xsl:variable name="variable2" select="'Y'" />
</xsl:when>
Variables in XSLT are not really variables, as their values cannot be changed. They resemble constants from conventional programming languages. The only way in which a variable can be changed is by declaring it inside a for-each loop, in which case its value will be updated for every iteration.
XSLT current() Function The current() function returns a node-set that contains only the current node. Usually the current node and the context node are the same.
XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.
The difference is that the value of an xsl:param could be set outside the context in which it is declared.
Variables in XSLT may only be assigned a value once. This is done by design. See Why Functional languages? for an appreciation of the motivation in general.
Rather than reassign a variable, write conditionals against the input document directly, or call a function (or named template) recursively with varying local parameters.
Anything you need to do can be done with an approach that does not require reassignment of variables. To receive a more specific answer, provide a more specific question.
See also:
Just use multiple variables. Here's your example made to work...
<xsl:variable name="variable1" select="'N'" />
....
<xsl:variable name="variable2">
<xsl:choose>
<xsl:when test="@tip = '2' and $variable1 != 'Y'">
<xsl:value-of select="'Y'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$variable1" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
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