【问题标题】:XPath 3 in browser浏览器中的 XPath 3
【发布时间】:2018-06-11 03:02:15
【问题描述】:

当我们看到一致性图表时,例如https://caniuse.com/#search=xpathhttps://developer.microsoft.com/en-us/microsoft-edge/platform/status/domlevel3xpath/?q=xpath,通常是关于DOM Level 3 XPath 专注于使用XPath 1.0 访问DOM 对象,但不一定是XPath 3.0 或更高版本。

问题 1 - W3C 等是否对浏览器有明确的版本要求以支持较新版本的 XPath?

此 XPath 1.0 功能有效:

document.evaluate(
  'normalize-space(" X  ")',
  document, null, XPathResult.ANY_TYPE, null ).stringValue

// => "X"

但是这个 XPath 3.0 内联函数特性 (ref) 抛出:

document.evaluate(
  'let $incr := function($n as xs:integer) as xs:integer { $n +1 } return $incr(2)',
  document, null, XPathResult.ANY_TYPE, null ).stringValue

边缘:

XPATH15001:不支持 XPath 查询“让 $incr := function($n as xs:integer) as xs:integer { $n +1 } return $incr(2)”

火狐:

SyntaxError: 该表达式不是合法表达式。

问题 2 - 如果浏览器中允许使用 XPath 3.0,假设我的代码中有错误;那么 v3.1 的特性(如arrow-functions)怎么样,是否也允许?

【问题讨论】:

    标签: javascript xml dom xpath browser


    【解决方案1】:

    No browsers support XPath 2.0 natively,更不用说 XPath 3.0。

    还要注意XPath, unlike XSLT, doesn't have an intrinsic way of determining the version of an XPath implementation 除了尝试给定版本支持的功能并观察是否发生错误,就像你所做的那样。

    【讨论】:

    • 由于该标准不需要更新的 XPath 支持,因此没有做出任何努力。
    • XPath2.0 在 2014 年被 XPath3.0 取代。
    • 标准可以规定合规条款,但不能强制采用。
    【解决方案2】:

    Saxon-JS 提供了可在任何浏览器中运行的 XPath 3.1 实现(它是用 Javascript 编写的)。

    但是,它(尚)不支持高阶函数,因此使用内联函数的示例将不起作用。

    2020 年 7 月更新

    Saxon-JS 2.0 支持整个 XPath 3.1,包括高阶函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-15
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 2010-12-05
      • 1970-01-01
      相关资源
      最近更新 更多