See xslt to operate on element value before displaying? for the original XML and XSL. I got an answer to my question there.
My other question on this same XML/XSL is: if I would like to capture the value of an element (such as the "title" element) in an XSL local variable, and then operate on it, how do I capture that value and assign it to a variable? I have the feeling it has something to do with XSL "param", but I am not sure.
So, on that same code, what is the minimal change to the XSL so that I'll have the value of title in a variable?
XSLT <xsl:variable> Tip: You can add a value to a variable by the content of the <xsl:variable> element OR by the select attribute!
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.
Definition of XSLT Variable. XSLT variable is defined as special tags used to declare a local or global variable that we make use of to store any values. The declared variables are referenced within an Xpath expression. Once it is set we cannot overwrite or update the variables.
The content model of both elements is the same. The way these elements declare variables is the same. However, the value of the variable declared using <xsl:param> is only a default that can be changed with the <xsl:with-param> element, while the <xsl:variable> value cannot be changed.
You use the xsl:variable statement to create a variable. Either of the following will work
<xsl:variable name="cdtitle"><xsl:value-of select="title"/></xsl:variable>
<xsl:variable name="cdtitle" select="title"/>
They statement in this case would have to be within the loop.
To use the variable, you can then just do this, assuming the variable is in scope.
<xsl:value-of select="$cdtitle"/>
Please note, despite the name, xsl:variables are not variable. Once set, they cannot be changed. You would have to create a new variable with a new name if you wanted to modify the value.
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