Re: [xsl] Set variable via contents of <variable> element

Subject: Re: [xsl] Set variable via contents of <variable> element
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Thu, 17 Dec 2009 14:38:09 +0100
Rowan Sylvester-Bradley wrote:
As I understand it, I can set the value of a variable in two ways:
1. By the "select" attribute of the <variable> element:
<xsl:variable name="myvariable" select="'myvalue'"/>

That way the variable 'myvariable' is of type string.


2. By the contents of the <variable> element:
<xsl:variable name="myvariable">
  My value
</xsl:variable>

That way, with XSLT 1.0, the variable 'myvariable is of type result tree fragment. With XSLT 2.0, the variable is of type document-node() I think.



The reason I need to do this is because I want to set the variable 'usethis'
to the result of a <choose> element which (as far as I know) I cannot
include in a "select" attribute. Also in my real example 'this' is a
parameter.

Do you use XSLT 1.0 or 2.0?


With 2.0, are you aware of the XPath 2.0
  if (condition) then expression1 else expression2
conditional expression that might help to use
  <xsl:variable name="myvariable"
                select="if (someCondition) then foo else bar"/>
to use the select attribute.

For XSLT 2.0 also http://www.w3.org/TR/xslt20/#variable-values might help, in particular that table explaining how combining the 'select' attribute and/or the 'as' attribute and/or the content of xsl:variable decide the type of the variable.

--

	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/

Current Thread