Printing Out The Value Of An XSL Variable
All answers are missing something important: read further:
Can someone please tell me how to print out a variable in my XSL transform? Seems like an easy enough thing to do but I just can't seem to do it.
In XSLT 1.0 there are two main ways of producing the contents of an <xsl:variable>
, depending on whether it contains a scalar value (string, number or boolean), or has a structured value -- a node-set (one or more nodes from xml document(s) ):
<xsl:value-of select="$yourscalarVariableName"/>
Use this to produce a scalar value. Actually produces a text node, containing this scalar value.<xsl:copy-of select="$yourStructuredVariableName"/>
Use this to produce a copy of all nodes contained in the variable.
It is very important to know that if an xsl:variable
contains a list of nodes and the <xsl:value-of ...>
instruction is used, only the string value of the first node will be produced. This is a frequently committed error and a FAQ.
There is a third way: if the <xsl:variable>
should be used in producing an attribute:
<someLiteralResultElement someAttribute="{$theVariable}"/>
The XPath expression in the curly braces (called AVT -- attribute-value-template) is evaluated and the result is put into the attribute value.
In XSLT 2.0, the <xsl:value-of .../>
instruction , when run not in compatibility mode, produces a list of text nodes -- one for each node contained in the xsl:variable
. When run in compatibility mode (has the attribute version="1.0"
specified), the <xsl:value-of>
instruction behaves in the same way as it does in XSLT 1.0.
In Xslt 2.0 <xsl:copy-of>
behaves in the same way as in XSLT 1.0. However it is recommended to use the new <xsl:sequence>
instruction, because the former produces a new copy of every node, while <xsl:sequence>
does not produce new copies of nodes.
You can use:
<xsl:value-of select="$xmlElem" />
Try this:
<xsl:value-of select="$xmlElem"/>