tags:

views:

1305

answers:

4

Hi all,

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"&gt;

  <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.

+5  A: 

Try this:

<xsl:value-of select="$xmlElem"/>
Andrew Hare
You think I would have tried that! Thanks.
Lee Theobald
Sometimes the simplest solution is the most elusive! Glad I could help :)
Andrew Hare
+2  A: 

You can use:

<xsl:value-of select="$xmlElem" />
Razzie
+2  A: 

The {$xmlElem} syntax only works within an attribute. You need:

<xsl:value-of select="$xslElem" />
David M
+4  A: 

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) ):

  1. <xsl:value-of select="$yourscalarVariableName"/> Use this to produce a scalar value. Actually produces a text node, containing this scalar value.

  2. <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.

Dimitre Novatchev
+1 Very informative and nicely done!
Andrew Hare
@Andrew-Hare Thanks, I value very much your opinion!
Dimitre Novatchev