XPath 1.0 中括号的作用是什么? [英] What is the role of parentheses in XPath 1.0?

查看:44
本文介绍了XPath 1.0 中括号的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Chrome DevTools > Elements 中,当我搜索 //tr/td/span 时,我找到了一个元素(因为这样的元素存在于我的页面上).

In Chrome DevTools > Elements, when I search for //tr/td/span I find an element (because such an element exists on my page).

当我搜索 (//tr)/td/span(//tr/td)/span 时,我也找到了这个元素.

When I search for (//tr)/td/span or (//tr/td)/span I also find this element.

但既不是 //tr(/td)/span 也不是 //tr/(td)/span 也不是 //tr/(td/)span 找到任何东西.

But neither //tr(/td)/span nor //tr/(td)/span nor //tr/(td/)span find anything.

XPath 中这些括号的含义是什么?

What is the meaning of these parentheses in XPath?

推荐答案

XPath 中的括号与在其他编程语言中的用法一样:

Parenthesis in XPath are used as they are in other programming languages:

  1. 函数参数分组: 例如://tr/td[contains(.,"e")]
  2. 评估优先级指示: 例如:普通算术表达式分组以及引导路径分组(跟踪 LocationPathPrimaryExpr 在 XPath 语法中)如 (//td)[1] 到查找文档中的第一个 td,而不是 //td[1],后者查找作为其各自第一个子元素的 td 元素父元素.
  1. Function argument grouping: e.g: //tr/td[contains(.,"e")]
  2. Evaluation precedence indication: e.g: normal arithmetic expression grouping as well as leading path grouping (trace LocationPath through to PrimaryExpr in the XPath grammar) as in (//td)[1] to find the first td in the document as opposed to //td[1] which finds the td elements that are the first child of their respective parent elements.

它们也用于

  1. 节点测试:例如:node(), element(), ...
  2. 处理说明:例如:PageBreak().
  1. node tests: e.g: node(), element(), ...
  2. processing instructions: e.g: PageBreak().

你没有找到任何东西的例子(例如://tr(/td)/span, //tr/(td)/span1 等)在不属于上述类别之一的路径中嵌入了括号.括号的这种使用实际上在语法上是无效的,应该被报告为这样而不是默默地失败.

Your examples that do not find anything (e.g: //tr(/td)/span, //tr/(td)/span1, etc) have parenthesis embedded within the path that do not follow in one of the above categories. Such use of parenthesis are actually syntactically invalid and should have been reported as such rather than silently failing.

1请注意,此表达式在 XPath 2.0/3.0 下实际上在语法上是有效的.谢谢@Andersson 注意到.

1Note that this expression would actually be syntatically valid under XPath 2.0/3.0. Thanks, @Andersson, for noticing.

这篇关于XPath 1.0 中括号的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆