11

I can't get this to work... I simply want to change the value of a globally defined variable:

                    <xsl:variable name="isBusiness"></xsl:variable>
                    <xsl:choose>
                        <xsl:when test="yes this is a business">
                               <xsl:variable name="isBusiness">true</xsl:variable>                        
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:variable name="isBusiness">false</xsl:variable>
                        </xsl:otherwise>
                    </xsl:choose>    

Obviously the code is invalid because is already defined, but how would I change the value?

FaNIX
  • 1,888
  • 6
  • 33
  • 60
  • 1
    Voting to close as [exact duplicate](http://stackoverflow.com/questions/6436272/how-to-update-the-variable-value-in-xslt/6438323#6438323). – Emiliano Poggi Jun 28 '11 at 04:31

1 Answers1

15

Check this link out:

http://www.dpawson.co.uk/xsl/sect2/N8090.html#d10874e187

Basically, your code should look like this:

<xsl:variable name="x">
   <xsl:choose>
     <xsl:when test="a">z</xsl:when>
     <xsl:when test="b">zz</xsl:when>
     <xsl:otherwise>zzz</xsl:otherwise>
   </xsl:choose>
 </xsl:variable>
code4life
  • 15,655
  • 7
  • 50
  • 82
  • Yes, except that the example binds the values true and false to x, so it's doing `x := if (c) then true else false` which can be simplified to `x := c`. – Michael Kay Jun 28 '11 at 09:58
  • @MichaelKay: sorry for resurrecting an old post, but I just bumped into this post again and saw your comment. I want to point out that the test criteria was for the text "yes this is a business", rather than a boolean eval. The xslt indicates that this is not a simple binary eval, but a choice of 2 or *more* items, of which only one is significant for evaluation. The result of the eval nets the true/false response on the xslt. Cheers! – code4life Aug 24 '12 at 14:45