What can I do to make this code work?
<xsl:choose> <xsl:when test='type = 6'> <xsl:variable name='title' select='root/info/title' /> </xsl:when> <xsl:when test='type = 7'> <xsl:variable name='title' select='root/name' /> </xsl:when> <xsl:otherwise> <xsl:variable name='title'>unknown</xsl:variable> </xsl:otherwise> </xsl:choose> <div class='title'> <xsl:value-of select='$title'/> </div>
This doesn't work because when i do <xsl:value-of select='$title'/>
, $title
is out of scope. I tried to add the line <xsl:variable name='title'/>
outside of the scope, but that won't work either, because then when i call <xsl:variable name='title' select='root/info/title' />
for example, i already have set this variable before. How should I solve this?
XSLT <xsl:variable>The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!
The difference is that the value of an xsl:param could be set outside the context in which it is declared.
I second @scunliffe - if $var exists but is empty, test="$var" will still return true. However, you may need to write it test="not($var = '')" if you are using earlier versions of XSL.
Definition and Usage. The <xsl:number> element is used to determine the integer position of the current node in the source. It is also used to format a number.
You can move the choose inside the setting of the variable, like this:
<xsl:variable name="title"> <xsl:choose> <xsl:when test='type=6'> <xsl:value-of select="root/info/title" /> </xsl:when> ... </xsl:choose> </xsl:variable> <div class='title'> <xsl:value-of select="$title" /> </div>
<xsl:choose> <xsl:when test='type = 6'> <xsl:variable name='title' select='root/info/title' /> </xsl:when> <xsl:when test='type = 7'> <xsl:variable name='title' select='root/name' /> </xsl:when> <xsl:otherwise> <xsl:variable name='title'>unknown</xsl:variable> </xsl:otherwise> </xsl:choose> <div class='title'> <xsl:value-of select='$title'/> </div>
This doesn't work
This is a FAQ:
You are defining several variables, each named $title
and each useless, because it goes out of scope immediately.
The proper way in XSLT 1.0 to define a variable based on conditions is:
<xsl:variable name="vTitle"> <xsl:choose> <xsl:when test='type = 6'> <xsl:value-of select='root/info/title' /> </xsl:when> <xsl:when test='type = 7'> <xsl:value-of select='root/name' /> </xsl:when> <xsl:otherwise> <xsl:value-of select="'unknown'"/> </xsl:otherwise> </xsl:choose> </xsl:variable>
Another way of defining the same variable: In this particular case you want the variable to have a string value. This can be expressed in a more compact form:
<xsl:variable name="vTitle2" select= "concat(root/info/title[current()/type=6], root/name[current()/type=7], substring('unknown', 1 div (type > 7 or not(type > 5))) ) "/>
Finally, in XSLT 2.0 one can define such a variable even more conveniently:
<xsl:variable name="vTitle3" as="xs:string" select= "if(type eq 6) then root/info/title else if(type eq 7) then root/name else 'unknown' "/>
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