views:

902

answers:

2

See http://stackoverflow.com/questions/1468522/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?

+1  A: 

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"></xsl:variable>

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.

Tim C
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
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
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
A: 

How to transform an input xml to output xml just by adding a value to an element.

Input XMl DSL DP

Output xml

DSL DP Changed the value

Srinivasa Moorte
Thanks but I do not understand this answer at all.
talkaboutquality