title: Returning QNames from user-defined function
body: <p>I am trying to transform a WSDL definition into another format for further processing, but I ran into a problem. I can't seem to return QName-values from a function, without them being turned into a string.</p>
  
  <p>I reduced the file to the following, while retaining the error behavior. The original function was supposed to translate non-prefixed values using the <code>target-namespace</code> attribute from the containing <code>&lt;xs:schema&gt;</code> or <code>&lt;wsdl:definitions&gt;</code>.</p>
  
  <p><strong>Example XSLT document:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:my="http://www.example.com/xslt"
      exclude-result-prefixes="#all" version="2.0"&gt;
  
      &lt;xsl:output method="xml" indent="yes"/&gt;
  
      &lt;xsl:function name="my:resolve-QName2" as="xs:QName"&gt;
          &lt;xsl:param name="name" as="xs:string"/&gt;
          &lt;xsl:param name="element" as="element()"/&gt;
          &lt;xsl:value-of select="resolve-QName($name,$element)"/&gt;
      &lt;/xsl:function&gt;
  
      &lt;xsl:template match="/definitions"&gt;
          &lt;xsl:variable name="qname" select="my:resolve-QName2('xs:string',.)" as="xs:QName"/&gt;
          &lt;QName&gt;
              &lt;prefix&gt;&lt;xsl:value-of select="prefix-from-QName($qname)"/&gt;&lt;/prefix&gt;
              &lt;local&gt;&lt;xsl:value-of select="local-name-from-QName($qname)"/&gt;&lt;/local&gt;
              &lt;namespace&gt;&lt;xsl:value-of select="namespace-uri-from-QName($qname)"/&gt;&lt;/namespace&gt;
          &lt;/QName&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p><strong>Input file:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;definitions xmlns:xs="http://www.w3.org/2001/XMLSchema"/&gt;
  </code></pre>
  
  <p><strong>Expected result:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;QName&gt;
      &lt;prefix&gt;xs&lt;/prefix&gt;
      &lt;local&gt;string&lt;/local&gt;
      &lt;namespace&gt;http://www.w3.org/2001/XMLSchema&lt;/namespace&gt;
  &lt;/QName&gt;
  </code></pre>
  
  <p>When running the above code, the Saxon 9.3.0.5 XSLT processor is stopping with an error:</p>
  
  <blockquote>
    <p><code>Required item type of result of function my:resolve-QName2() is xs:QName. Cannot convert string to type {xs:QName}</code></p>
  </blockquote>
  
  <p>It seems the engine is first casting the QName into a string, and then trying to cast it back to QName, which fails. If I remove the two <code>as="xs:QName"</code>, I get the following error:</p>
  
  <blockquote>
    <p><code>Required item type of first argument of prefix-from-QName() is xs:QName. Cannot convert string to type {xs:QName}</code></p>
  </blockquote>
  
  <p>How can I return a QName from a function?</p>
  
  <p>Is this a bug in Saxon XSLT processor, or something I have misunderstood about XSLT?</p>
  
  <p>Is there any XSLT engine that is able to process the above file?</p>