kanwarpal
kanwarpal

Reputation: 103

How to modify the value of variable in XSL?

I have a xsl file which is fetching data and storing in a variables .I have a value 1 stored in a variable . Now i want to modify the variable value i.e. if it contains 1 it should be replaced by activated . How can i do it ?

Thanks in advance

Upvotes: 3

Views: 11180

Answers (1)

Lukasz
Lukasz

Reputation: 7662

Once you have set a variable's value, you cannot change or modify that value!

http://www.w3schools.com/xsl/el_variable.asp

Let's say you have this:

<xsl:variable name="var">1</xsl:variable>

Then, everywhere you need, you can use the following section (works in XSLT 1.0) and it will put activated value in your output if $var is equal to 1 (or the value of $var otherwise).

<xsl:choose>
    <xsl:when test="$var=1">activated</xsl:when>
    <xsl:otherwise><xsl:value-of select="$var"/></xsl:otherwise>
</xsl:choose>

Or you can declare new variable:

<xsl:variable name="var2">
    <xsl:choose>
        <xsl:when test="$var=1">activated</xsl:when>
        <xsl:otherwise><xsl:value-of select="$var"/></xsl:otherwise>
    </xsl:choose>
</xsl:variable>

In this case, you will have to use instruction to print it in the output:

<xsl:value-of select="$var2" />

Upvotes: 5

Related Questions