撒克逊错误"XPTY0019: Required item type of first operand of '/' is node(); supplied value has item type



我有一个XSL程序,它反过来生成一个XSL程序,根据输入,它可能看起来像这样:

<xsl:variable name="patterns"/> <!--empty in this particular case-->
<xsl:template name="token">
    <xsl:for-each select="$patterns/pattern">
...

当我运行生成的样式表时,Saxon,谢天谢地,显然在做某种静态分析并抱怨:

XPTY0019: Required item type of first operand of '/' is node(); supplied value has item type xs:string

,甚至不会编译样式表。

我的解决方法是在$patterns节点集中生成一个虚拟元素,但是这里有没有更干净的方法,或者抑制编译错误的方法?

根据http://www.w3.org/TR/xslt20/#variable-values, "如果变量绑定元素的内容为空,并且既没有select属性也没有as属性,那么提供的变量值是一个零长度字符串。"

所以你需要改变这一点,例如通过做<xsl:variable name="patterns" select="()"/>绑定一个空序列作为变量值。

在XSLT 1.0中(同样适用于XSLT 2.0)使用:

<xsl:variable name="patterns" select="/.."/>

这向XSLT处理器提供必要的信息,以得出$patterns变量的类型是节点集

相关内容

最新更新