Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change or reassign a variable in XSLT?

Tags:

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>
like image 480
Manuel Franqueira Avatar asked Oct 08 '13 18:10

Manuel Franqueira


People also ask

How do you reassign a variable in XSLT?

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.

What is current () XSLT?

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.

What is text () in XSLT?

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.

What is difference between Param and variable in XSLT?

The difference is that the value of an xsl:param could be set outside the context in which it is declared.


2 Answers

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:

like image 157
kjhughes Avatar answered Sep 16 '22 18:09

kjhughes


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>
like image 27
Gary Sheppard Avatar answered Sep 16 '22 18:09

Gary Sheppard