XPath 3 in browser

青春壹個敷衍的年華 提交于 2020-08-08 05:17:25

问题


When we see conformance chart, such as https://caniuse.com/#search=xpath and https://developer.microsoft.com/en-us/microsoft-edge/platform/status/domlevel3xpath/?q=xpath, it's usually about DOM Level 3 XPath that focuses on accessing DOM object using XPath 1.0, but not necessarily XPath 3.0 or higher.

Question 1 - Is there any explicit version requirement for browsers by W3C etc. to support newer versions of XPath?

This XPath 1.0 feature works:

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

// => "X"

But this XPath 3.0 inline-function feature (ref) throws:

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

Edge:

XPATH15001: XPath query "let $incr := function($n as xs:integer) as xs:integer { $n +1 } return $incr(2)" not supported

Firefox:

SyntaxError: The expression is not a legal expression.

Question 2 - If XPath 3.0 is allowed in browser, assuming bug in my code; then how about v3.1 features (such as arrow-functions), are those also allowed?


回答1:


No browsers support XPath 2.0 natively, let alone XPath 3.0.

Note also that XPath, unlike XSLT, doesn't have an intrinsic way of determining the version of an XPath implementation other than trying a function supported by a given version and observing whether an error occurs, as you've done.




回答2:


Saxon-JS provides an implementation of XPath 3.1 that runs in any browser (it's written in Javascript).

However, it doesn't (yet) support higher-order functions, so the example using an inline function won't work.

July 2020 Update

Saxon-JS 2.0 supports the whole of XPath 3.1 including higher order functions.



来源:https://stackoverflow.com/questions/48043476/xpath-3-in-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!