选择一个 XML 元素,而不考虑 XPATH 的级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4814498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select an XML element regardless of level with XPATH
提问by Simeon
I have this:
我有这个:
<a>
<b>
<t>text</t>
</b>
</a>
<a>
<t>text</t>
</a>
So I want to select the text regardless of where is it. (note that it can be anywhere not just 1/2 levels down, it can have no parents for instance)
所以我想选择文本,不管它在哪里。(请注意,它可以位于不只是 1/2 级以下的任何位置,例如它可以没有父级)
Is this possible?
这可能吗?
回答by
You are looking for the descendantaxis:
您正在寻找的descendant轴:
the
descendantaxis contains the descendants of the context node; a descendant is a child or a child of a child and so on; thus the descendant axis never contains attribute or namespace nodes
所述
descendant轴包含上下文节点的后代; 后代是孩子或孩子的孩子等等;因此后代轴从不包含属性或命名空间节点
In your case: /descendant:t
在你的情况下: /descendant:t
Of course, as others have answered, there is an abbreviated syntax for this:
当然,正如其他人所回答的那样,这有一个缩写的语法:
//is short for/descendant-or-self::node()/. For example,//parais short for/descendant-or-self::node()/child::paraand so will select anyparaelement in the document (even aparaelement that is a document element will be selected by//parasince the document element node is a child of the root node)
//是 的缩写/descendant-or-self::node()/。例如,//para是/descendant-or-self::node()/child::paraand so 的缩写 ,因此将选择para文档中的任何元素(即使para是文档元素的元素也会被选择,//para因为文档元素节点是根节点的子节点)
回答by Chris Cameron-Mills
You can use //to select all nodes from the current node. So //text()would select all text nodes.
您可以使用//从当前节点中选择所有节点。所以//text()会选择所有文本节点。
If you wanted all t elements you would do //t. If you wanted to do do all telements from a certain point you might then do /x/y//t.
如果你想要所有 t 元素,你会这样做//t。如果你想t从某个点开始做所有的元素,你可能会这样做/x/y//t。
回答by mkraemerx
just //tif you want all <t>tags
只要//t你想要所有<t>标签
回答by cbaldan
W3Schools has really good free courses regarding everything that is HTML related. I highly recommend reading it and making the examples. https://www.w3schools.com/xml/xpath_intro.asp
W3Schools 有关于 HTML 相关所有内容的非常好的免费课程。我强烈建议阅读它并制作示例。 https://www.w3schools.com/xml/xpath_intro.asp
HINT: you can use your browser's console to evalute expressions. It's under Developer Tools - F12 key for Chorme and Firefox:
提示:您可以使用浏览器的控制台来评估表达式。它位于开发人员工具 - Chorme 和 Firefox 的 F12 键下:
$x('<your expressions>');
So, as everyone said, you can use the //syntax to find an element anywhere in the page.
e.g. //awill return you allaelements within the page.
因此,正如大家所说,您可以使用//语法在页面中的任何位置查找元素。eg//a将返回页面中的所有a元素。
Most likely you will want a specific one, so, that's where you use the predicates. They are contained between brackets. Using this very page as example, here is a xquery:
您很可能需要一个特定的,因此,这就是您使用谓词的地方。它们包含在括号之间。以这个页面为例,这是一个 xquery:
//a[text()="Simeon"]
This xpath will return all aelements that have Simeon as it's text.
In many situations you might need to improve you xpath to include more identifiers, be more specific.
这个 xpath 将返回所有a具有 Simeon 作为文本的元素。在许多情况下,您可能需要改进 xpath 以包含更多标识符,更具体。
//a[text()="Simeon" AND @href="/users/274344/simeon"]
So, you can use pretty much any HTML attribute, or even CSS to identify a specific node you want.
因此,您几乎可以使用任何 HTML 属性,甚至 CSS 来标识您想要的特定节点。
But now, let′s take it up a notch.
Let′s say you want to get the aelement that is below user's mkimdanswer from Jan 27.
If you look at this page structure, you have to get you aand dive back a few levels, untill you are able to reach the spanthat holds the posting date.
但是现在,让我们更上一层楼。假设您想从Jan 27获取a低于用户mkimd答案的元素。如果你看看这个页面结构,你必须让你回到几个级别,直到你能够到达保存发布日期的跨度。a
//a[text()="mkimd" AND ../../div[@class="user-action-time"]/span[contains(.,"Jan")]]
There are many ways of doing these queries,this last example I gave can be achieved with different xqueries.
有很多方法可以做这些查询,我给出的最后一个例子可以用不同的 xquery 来实现。
I find that xqueries are very similar to navigation in directories in console, like Linux BASH - relative and absolute paths, and the identifiers are like SQL WHEREclauses.
我发现 xqueries 与控制台中的目录导航非常相似,例如 Linux BASH - 相对和绝对路径,标识符就像SQL WHERE子句。
If you research, there are many functions available in XPATH syntax, such as
如果你研究一下,XPATH 语法中有很多可用的函数,比如
- lower-case()
- upper-case()
- concat()
- ends-with()
- operators (
+,-,*,div,!=,<,<=,>, ...)
- 小写()
- 大写()
- 连接()
- 以。。结束()
- 运算符 (
+,-,*,div,!=,<,<=,>, ...)
I highly advise you to use some tool like Firefox Firefug FirePathaddon to practice xquery and check if you are getting the element you want - it highlights the elements found.
我强烈建议您使用诸如Firefox Firefug FirePath插件之类的工具来练习 xquery 并检查您是否获得了所需的元素 - 它会突出显示找到的元素。
====================
====================
EDIT - May-8th-15
编辑 - 5 月 8 日至 15 日
In case you are using Xpath in Selenium automation, know that it will not help you select CSS pseudo elements.
如果您在 Selenium 自动化中使用 Xpath,请知道它不会帮助您选择 CSS 伪元素。

