XPath 2.0 前导 '/' 不能 select 包含上下文项的树的根节点:上下文项不是节点

XPath 2.0 Leading '/' cannot select the root node of the tree containing the context item: the context item is not a node

尝试检查一个值(标记化后)是否与我文档中的另一个值不匹配:

/foo/bar/baz/tokenize(value,',')[not(. =(/foo/biz/value/string(),'bing'))]

具体来说,检查 /foo/bar/baz/value(即 'ding,dong,bing')是否与 /foo/biz/value/string() 或值 'bong'.

匹配

但我得到 "Leading '/' cannot select the root node of the tree containing the context item: the context item is not a node"

有什么方法可以在 XPath 中执行此操作,或者我是否需要进入 XQuery 并开始担心变量?

鉴于您使用的是 Saxon,您可以利用 XPath 3.0 允许绑定变量这一事实:

let $foo := /foo return $foo/bar/baz/tokenize(value,',')
    [not(. =($foo/biz/value/string(),'bing'))]

或者您可以从谓词中提取表达式:

let $exceptions := (/foo/biz/value/string(),'bing') 
return /foo/bar/baz/tokenize(value,',')[not(. = $exceptions)]

如果您想要纯 XPath 2.0,您可以使用丑陋的 "for" 绑定来实现相同的目的:

for $foo in /foo return $foo/bar/baz/tokenize(value,',')
   [not(. =($foo/biz/value/string(),'bing'))]

如果您使用 XSLT,当然可以使用 current()