How to define an XSL variable and assign a value in xsl: select

I want to define a variable called "category" in XSL, assign a value to it, and reuse this variable in my code. if objecttype = 1, the value of the variable should be "car"; if objecttype = 2, the value of the variable should be "bus"

How can i achieve this?

<xsl:template match="/"> <html> <head><style type="text/css">body{font-size:11px;font-family:Verdana;}</style></head> <body> Dear <xsl:for-each select="user"> <xsl:value-of select="firstname"/><xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text> <xsl:if test="middlename != ''"> <xsl:value-of select="middlename"/><xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text> </xsl:if> </xsl:for-each> <xsl:value-of select="user/lastname"/>,<br/> <br/> You have created a company listing for "<xsl:value-of select="user/objecttitle"/>".<br/> <br/> Did you know Google uses the number of Facebook 'likes' for webpages in its rankings?<br/> You can like you page here: <xsl:for-each select="user"> <xsl:variable name="category"> <xsl:choose> <xsl:when test="objecttype='1'">car</xsl:when> <xsl:when test="objecttype='2'">bus</xsl:when> </xsl:choose> </xsl:variable> </xsl:for-each> <a href="http://www.mydomain.com/{$category}/{user/objectid}/{user/objecturl}">Click here to go to your company listing now.</a><br/> Kind regards,<br/> <br/> <br/> </body> </html> </xsl:template> 
+4
source share
1 answer

This is a common mistake for XSL beginners. The right approach:

 <xsl:variable name="category"> <xsl:choose> <xsl:when test="objecttype='1'">car</xsl:when> <xsl:when test="objecttype='2'">bus</xsl:when> ... etc </xsl:choose> </xsl:variable> 

In your example, the variable is local to the <xsl:when...> .

+5
source

All Articles