xpath 中的//node 和/descendant::node 有什么区别? [英] What's the difference between //node and /descendant::node in xpath?

查看:22
本文介绍了xpath 中的//node 和/descendant::node 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Selenium 定位网页中的元素时使用了很多 XPath,并且最近从使用 node1//node2 转向使用 node1/descendant::node2.这两种方法有什么区别?一种比另一种更有效吗?

用于演示的示例 XML 片段:

<表格><tr><td class="title">指环王</td><td class="author">JRR Tolkein</td></tr><tr><td class="title">银河系搭便车指南</td><td class="author">Douglas Adams</td></tr>

那就是:

id('books')//td[@class='title']

或:

id('books')/descendant::td[@class='title']

解决方案

参见 http://www.w3.org/TR/xpath#path-abbrev

//只是后代::轴的缩写

编辑

引用:

<块引用>

//para 是/descendant-or-self::node()/child::para 的缩写

也就是说,它指的是上下文节点的子节点或上下文节点的任何子节点的所有para.据我所知,这可以转换为上下文节点的任何后代段.

I use a lot of XPath when locating elements in web pages using Selenium, and have moved away from using node1//node2 towards using node1/descendant::node2 more recently. What's the difference between the two methods? Is one more efficient than the other?

Example XML snippet to demonstrate:

<div id="books">
  <table>
    <tr><td class="title">Lord of the Rings</td><td class="author">JRR Tolkein</td></tr>
    <tr><td class="title">The Hitch-Hikers Guide to the Galaxy</td><td class="author">Douglas Adams</td></tr>
  </table>
</div>

So it'd be:

id('books')//td[@class='title']

or:

id('books')/descendant::td[@class='title']

解决方案

see http://www.w3.org/TR/xpath#path-abbrev

// is just an abbreviation for the descendant:: axis

Edit

To quote:

//para is short for /descendant-or-self::node()/child::para

That is, it refers to all para which are a child of the context node or any node descended from the context node. As far as I can tell that translates into any descendant para of the context node.

这篇关于xpath 中的//node 和/descendant::node 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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