当我们看到一致性图表时,比如https://caniuse.com/#search=xpath和 https://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 query "let $incr := function($n as xs:integer) as xs:integer { $n +1 } return $incr(2)" not supported
火狐:
SyntaxError: The expression is not a legal expression.
问题 2 - 如果浏览器允许 XPath 3.0,假设我的代码中存在错误;那么 v3.1 功能(例如 arrow-functions )是否也被允许?
最佳答案
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除了像您所做的那样尝试给定版本支持的功能并观察是否发生错误。
关于javascript - 浏览器中的 XPath 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48043476/