Conditional assignment in XSLT? I've written some XSLT where I want to assign values to some variables.

Depending on whether or not a condition is met, I want the variable to be populated with a computed value and I want it to have a default value otherwise.

I've written the following code that appears to work, but I think that there may be a better way.

&lt;xsl:variable name='HasValue' select='count($Catalog/lookup) &amp;gt; 0' /&gt;
&lt;xsl:variable name='Product' &gt;
    &lt;xsl:choose&gt;&lt;xsl:when test='$HasValue'&gt;
      &lt;xsl:value-of select='$Catalog/lookup/Product' /&gt;
    &lt;/xsl:when&gt;&lt;xsl:otherwise&gt;UNK&lt;/xsl:otherwise&gt;&lt;/xsl:choose&gt;
&lt;/xsl:variable&gt;
&lt;xsl:variable name='Area' &gt;
    &lt;xsl:choose&gt;&lt;xsl:when test='$HasValue'&gt;
      &lt;xsl:value-of select='$Catalog/lookup/Area' /&gt;
    &lt;/xsl:when&gt;&lt;xsl:otherwise&gt;UNK&lt;/xsl:otherwise&gt;&lt;/xsl:choose&gt;
&lt;/xsl:variable&gt;


Yes, my XSLT processor uses XSLT 2.0
IMHO your code is fine. The "problem" with XSLT variables is that they are NOT variables but more resemble constants: once set they cannot be changed anymore. Also, their scope is restricted to anything that follows their definition until the closing tag. In particular they are NOT visible in any ancestor tag. The combination of both results in the type of constructs that you have used above.

What is most disturbing is the fact that you usually have to repeat the testing of the criterion again and again if several variables depend on the same criterion. Again there's nothing generic that you can do about this, but of course, you can always at least create another variable holding the result of the test and then use that several times.

Depending on the context of your XSLT I would suggest two alternatives which may or may not be more elegant:

a) works in XSLT 1.0 and 2.0: If you just define the variables to be used as parameters to a template-call consider writing the template-call twice once in the when as once in the otherwise portion. This way you will end up with just one &lt;choose&gt;.

b) Works only well in XSLT 2.0 (or a little awkward in XSLT with node-set extensions): move the check of the criterion and the selection of the definition values into a template and return the "set" of values to the assigned in a node-set. In a way you can regard this node-set as a record whose attributes have been set according to the criterion. See example below.

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xsl:stylesheet 
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /&gt;

  &lt;xsl:template name="get_values"&gt;
    &lt;xsl:param name="catalog"/&gt;

      &lt;xsl:choose&gt;

        &lt;xsl:when test="count($catalog/lookup) &amp;gt; 0"&gt;
          &lt;PRODUCT VALUE="{$catalog/lookup/Product}"/&gt;
          &lt;AREA VALUE="{$catalog/lookup/Area}"/&gt;
        &lt;/xsl:when&gt;

        &lt;xsl:otherwise&gt;
          &lt;PRODUCT VALUE="UNK"/&gt;
          &lt;AREA VALUE="UNK"/&gt; 
        &lt;/xsl:otherwise&gt;

      &lt;/xsl:choose&gt;

  &lt;/xsl:template&gt;

  &lt;xsl:template match="/"&gt;

    &lt;xsl:variable name="Catalog" select="."/&gt;

    &lt;xsl:variable name="my_config"&gt;
      &lt;xsl:call-template name="get_values"&gt;
        &lt;xsl:with-param name="catalog" select="$Catalog"/&gt;
      &lt;/xsl:call-template&gt;
    &lt;/xsl:variable&gt;

    &lt;xsl:message&gt;
      The value of PRODUCT is &lt;xsl:value-of select="$my_config/PRODUCT/@VALUE"/&gt; 
      and the value of AREA is &lt;xsl:value-of select="$my_config/AREA/@VALUE"/&gt; 
    &lt;/xsl:message&gt;

  &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;


This solution is definitely not shorter than yours but it resembles more other standard programming languages. The record type approach may come in especially handy if you have a lot of templates requiring the attributes in the record.