为什么在 xsl:variable 中存储属性会导致错误 XTDE0420?

Why does storing attribute in xsl:variable result in error XTDE0420?

此 XSLT 构造一个属性并将结果存储在一个变量中。然后变量被复制为元素 <test>:

的唯一子元素
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all">    

  <xsl:template match="/">        
    <xsl:variable name="some-attribute">
      <xsl:attribute name="test">value</xsl:attribute>
    </xsl:variable>
    <test>
      <xsl:copy-of select="$some-attribute" />
    </test>
  </xsl:template>

</xsl:stylesheet>

虽然这看起来只是简单地插入一个属性作为元素的子元素,但结果却是抛出一个错误:XTDE0420: Cannot create an attribute node whose parent is a document node

关键信息解释在section 9.3 of the XSLT 2.0 spec, "Values of Variables and Parameters":

If a variable-binding element has no select attribute and has non-empty content (that is, the variable-binding element has one or more child nodes), and has no as attribute, then the content of the variable-binding element specifies the supplied value. The content of the variable-binding element is a sequence constructor; a new document is constructed with a document node having as its children the sequence of nodes that results from evaluating the sequence constructor and then applying the rules given in 5.7.1 Constructing Complex Content. The value of the variable is then a singleton sequence containing this document node. For further information, see 9.4 Creating implicit document nodes.

本质上,没有select属性和as属性的变量的值是文档节点。

无法修改示例中的变量以使用select,但可以更改为使用as:

<xsl:variable name="some-attribute" as="item()*">
  <xsl:attribute name="test">value</xsl:attribute>
</xsl:variable>