处理 xsl:evaluate 返回的空序列

问题描述

我正在使用 xsl:evaluate 来获取 XPath 指向的值。

示例代码

<xsl:variable name="input" as="xs:string">
  <xsl:evaluate xpath="$someXPath" context-item="."/>
</xsl:variable>

它工作正常,但如果我传递给 xsl:evaluate 的 XPath 是错误的并且指向任何内容,当它尝试将空值分配给 $input 变量时,我会收到此错误

XTTE0570 不允许空序列作为变量的值 $输入

我也这样试过(在 xsl:evaluate 添加 as="xs:string")

<xsl:variable name="input" as="xs:string">
  <xsl:evaluate xpath="$Variable[@type=$EXnn]/@element" context-item="$Attribut" as="xs:string"/>
</xsl:variable>

但我得到相同类型的错误

Dynamic error in expression {myXPath} called using xsl:evaluate.
  Caused by net.sf.saxon.trans.XPathException: An empty sequence is not allowed as the
  result of the expression {myXPath} evaluated by xsl:evaluate

XPath 不返回任何东西的情况如何处理?

解决方法

如果您将变量键入为 as="xs:string?",它应该可以存储空序列或字符串。