title: Can an XSLT 2.0 function return arbitrary types?
body: <p>I am trying to write an XSLT 2.0 function that returns a result of a specific type--let's say one or more  elements. Here's what I've tried, to no avail:</p>
  
  <pre><code>  &lt;xsl:function name="util:find-parents2" as="element(parent)*"&gt;
      &lt;xsl:variable name="output" as="element(parent)*"&gt;
        &lt;xsl:for-each select="('one','two')"&gt;
          &lt;parent&gt;
            &lt;xsl:sequence select="."/&gt;
          &lt;/parent&gt;
        &lt;/xsl:for-each&gt;
      &lt;/xsl:variable&gt;
      &lt;xsl:value-of select="$output"/&gt;
    &lt;/xsl:function&gt;
  </code></pre>
  
  <p>Here's the error I get from the Saxon processor:</p>
  
  <pre><code>Error at xsl:function on line 192 column 65 of file:/e:/mlsh/recursive.xsl:
    XTTE0780: Required item type of result of function util:find-parents2() is element(parent,
    xs:anyType); supplied value has item type text() Failed to compile stylesheet. 1 error detected.
  </code></pre>
  
  <p>But I expected to get something like this:</p>
  
  <pre><code>&lt;parent&gt;one&lt;/parent&gt;
  &lt;parent&gt;two&lt;/parent&gt;
  </code></pre>
  
  <p>What am I missing here? I thought I had specified the appropriate type for $output (one or more <code>&lt;parent&gt;</code> elements), but the processor clearly isn't getting the message and is seeing only text. How can I return a list of <code>&lt;parent&gt;</code> elements here? Thanks in advance...</p>
  
  <p>UPDATE:</p>
  
  <p>Summarizing, the answer to the question in the title is 'yes'. The key in the original example is the difference between <code>&lt;xsl:value-of&gt;</code> and <code>&lt;xsl:sequence&gt;</code>. Replacing the former with the latter is one way to get the desired behavior. Another way would be to 'unwrap' the contents from the <code>&lt;xsl:variable&gt;</code> element, which makes things a bit tidier:</p>
  
  <pre><code>  &lt;xsl:function name="util:find-parents2" as="element(parent)*"&gt;
      &lt;xsl:for-each select="('one','two')"&gt;
        &lt;parent&gt;
          &lt;xsl:sequence select="."/&gt;
        &lt;/parent&gt;
      &lt;/xsl:for-each&gt;
    &lt;/xsl:function&gt;
  </code></pre>
  
  <p>Thanks to Michael and Jim for catching my original oversight...</p>