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.
The XSLT <xsl:value-of> element is used to extract a value from an expression defined in the select attribute. The expression defined in the mandatory select attribute is either an XPATH expression (for nodes and/or values) or a variable reference. The return of the <xsl:value-of> function is a literal value.
Definition and Usage. The <xsl:number> element is used to determine the integer position of the current node in the source. It is also used to format a number.
XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.
The <xsl:value-of> element extracts the value of a selected node. The <xsl:value-of> element can be used to select the value of an XML element and add it to the output.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With