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. Here's the code I have:
<?xml version='1.0' encoding='UTF-8' ?>
<xsl:stylesheet version="1.0"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="ControlledListStructure">
<xsl:param name="xmlElem" />
<xsl:param name="dataName" />
<xsl:element name="{$xmlElem}">
1: <xsl:text>{$xmlElem}</xsl:text>.
2: {$xmlElem}.
</xsl:element>
</xsl:template>
</xsl:stylesheet>
If I called this template with a value for xmlElem of "Wibble" (a string – not a node), I would get the following output:
<Wibble>
1: {$xmlElem}.
2: {$xmlElem}.
</Wibble>
So my parameter is coming over properly, I just can't access it properly. Can someone tell me how I can get $xmlElem to print out properly so that I see:
<Wibble>
1: Wibble.
2: Wibble.
</Wibble>
Thanks for any input.
Best Solution
All answers are missing something important: read further:
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: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 thexsl:variable
. When run in compatibility mode (has the attributeversion="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.