【问题标题】:Why does document.evaluate('.', document.body, null, XPathResult.ANY_TYPE, null).singleNodeValue throw an exception?为什么 document.evaluate('.', document.body, null, XPathResult.ANY_TYPE, null).singleNodeValue 会抛出异常?
【发布时间】:2012-11-24 04:14:24
【问题描述】:

如果我告诉 XPath 评估程序我想要一个 XPathResult.FIRST_ORDERED_NODE_TYPE (8) 或 ANY_UNORDERED_NODE_TYPE (9),我会得到 body 元素,正如预期的那样 - 但所有其他结果类型都会产生一个 The expression cannot be converted to return the specified type.例外(在 Firefox 中;WebKit 和 Opera 似乎不那么慷慨,只是将其命名为 TYPE_ERR: DOM XPath Exception 52

发生了什么事?而且,为了清楚起见重新表述问题:为什么选择上下文节点是类型错误,而不是清楚地推断为单节点结果,这从 XPath 表达式本身很明显(".""self::node()" 的同义词) ,至少对作为人类的我自己而言?

(作为参考,触发错误的并不是专门访问.singleNodeValue - XPathResult 的所有属性都表示同样的错误。)

【问题讨论】:

    标签: dom xpath document.evaluate


    【解决方案1】:

    好吧,有一个规范 http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathResult-singleNodeValue 清楚地说明了 singleNodeValue “如果 resultType 不是 ANY_UNORDERED_NODE_TYPE 或 FIRST_ORDERED_NODE_TYPE,则会引发 TYPE_ERR。”。实现遵循该规范。

    【讨论】:

      猜你喜欢
      • 2013-04-25
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 2017-03-14
      • 2022-07-22
      相关资源
      最近更新 更多