当我们看到一致性图表,例如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查询“let $ incr:= function($ n as xs:integer)as xs:integer {$ n +1} return $ incr(2)”不支持
火狐:
SyntaxError:表达式不是合法表达式。
问题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,除了尝试给定版本支持的函数并观察是否发生错误,就像你所做的那样。
Saxon-JS提供了在任何浏览器中运行的XPath 3.1的实现(它是用Javascript编写的)。
但是,它(尚)不支持高阶函数,因此使用内联函数的示例将不起作用。