10

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?

Community
  • 1
  • 1
talkaboutquality
  • 1,312
  • 2
  • 16
  • 34

1 Answers1

17

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.

Mystic Odin
  • 269
  • 1
  • 2
  • 13
Tim C
  • 70,053
  • 14
  • 74
  • 93
  • Thanks. Just so I learn all common syntaxes, isn't there also a syntax where I declare the variable first, and then set its value? With your comment on that, I will likely mark this as the accepted answer. In any case, I searched your note "xsl:variables are not variable" and realize I'll have to read, for example, "http://xml.apache.org/xalan-j/xsltc/xsl_variable_design.html" carefully to understand all about xsl variables and parameters. – talkaboutquality Sep 26 '09 at 20:55
  • www.w3schools.com is another good place to go to learn about XSLT. See http://www.w3schools.com/xsl/xsl_w3celementref.asp for a list of XSLT elements, for example. – Tim C Sep 27 '09 at 08:14
  • Now I've tried both options and they work. Thanks for answer and additional xsl reference. I think I'm on my way now! – talkaboutquality Sep 28 '09 at 22:44
  • Thank you much for a solid XSL usage example! – asherrard Oct 14 '15 at 16:13