2

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?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Annie
  • 3,090
  • 9
  • 36
  • 74

2 Answers2

4

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.

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
kjhughes
  • 106,133
  • 27
  • 181
  • 240
3

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164