我的印象是所有最新的浏览器现在都支持 XPath 2。当我使用 lower-case()
和 uppser-case()
(在版本 2 中引入的功能)Chrome 会引发语法错误。但是,他们的旧替代方案 translate()
工作正常。
这是一个错误还是最新的 Chrome 实际上使用 XPath 1?有没有命令/方法可以找出 XPath 版本?
// Finds the element as expected.
$x('//h2/text()[. = "Delete"]')
// Doesn't find the element (also expected).
$x('//h2/text()[. = "delete"]')
// SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//h2/text()[lower-case(.) = "delete"]' is not a valid XPath expression.
$x('//h2/text()[lower-case(.) = "delete"]')
最佳答案
不,Chrome 使用 XPath 1.0。
您可以将 XPath 表达式简化为一个 v2.0 函数来查看:
$x("lower-case('ABC')")
SyntaxError: Failed to execute 'evaluate' on 'Document': The string 'lower-case('ABC')' is not a valid XPath expression.
尝试任何其他 XPath 2.0 功能,例如
current-date()
会产生类似的错误。除了通过此类探测之外,没有任何内置方法可以明确确定 XPath 实现的版本。
另一方面,XSLT 具有
system-property('xsl:version')
用于确定版本 1.0 与 2.0。
关于google-chrome - Chrome 是否使用 XPath 2.0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25455351/