Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL: How do I assign the value of an XML element to a variable (minimal change to page below)?

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?

like image 996
talkaboutquality Avatar asked Sep 25 '09 09:09

talkaboutquality


People also ask

How do I assign a value to a variable in XSLT?

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!

Can we reassign a value to variable in XSLT?

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.

How do you define an xsl variable?

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.

What is difference between param and variable in XSLT?

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.


1 Answers

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.

like image 163
Tim C Avatar answered Oct 15 '22 12:10

Tim C