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

前端之家收集整理的这篇文章主要介绍了xml – 为什么在xsl:variable中存储属性导致错误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:无法创建父文件节点的父节点.

解决方法

关键信息在 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,但可以将其更改为用作:

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

猜你在找的XML相关文章