title: How Can I Test Whether a Variable Contains an Element Value or a String / Number Value? (XSL 1.0)
body: <p>I would like to know how to test for whether a variable contains an element value or a simple string / number in XSL 1.0.</p>
  
  <pre><code>&lt;xsl:choose&gt;
      &lt;xsl:when test="is the variable an element"&gt;
             &lt;!-- do some stuff --&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
             &lt;!-- do some other stuff --&gt;
      &lt;/xsl:otherwise&gt;
  &lt;/xsl:choose&gt;
  </code></pre>
  
  <h2>More Detail About My Problem</h2>
  
  <p>I have an XSL similar to the one below. Sometimes the variable may be an element and sometimes that element may have no value (i.e. nil = "true"). This is why I use the <code>$imAStringButICouldHaveBeenAnElementForAllYouKnow[normalize-space()]</code> test to check that the element has a value.</p>
  
  <p>However, running this XSL with Saxon 6.5.5 fails with 'The value is not a node-set'. I can see why, the variable is indeed not a node set; I need an additional check before this one to confirm whether or not the variable contains an appropriate value.</p>
  
  <p><strong>XSL</strong></p>
  
  <pre><code>&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:variable name="imAStringButICouldHaveBeenAnElementForAllYouKnow" select="'string'"/&gt;
  
      &lt;xsl:template match="/"&gt;
          &lt;!-- If this is an element I want to know does it have a value? --&gt;
          &lt;xsl:if test="$imAStringButICouldHaveBeenAnElementForAllYouKnow[normalize-space()]"&gt;
              &lt;!-- do some stuff --&gt;
          &lt;/xsl:if&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>