xml 具有多个条件的 XPath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10247978/
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
XPath with multiple conditions
提问by mjroodt
What XPath can I use to select any category with a name attribute specified and any child node author with the value specified.
我可以使用什么 XPath 来选择具有指定名称属性的任何类别以及具有指定值的任何子节点作者。
I've tried different variations of the path below with no success:
我尝试了以下路径的不同变体,但没有成功:
//quotes/category[@name='Sport' and author="James Small"]
The XML:
XML:
<?xml version="1.0" encoding="utf-8"?>
<quotes>
<category name="Sport">
<author>James Small<quote date="09/02/1985">Quote One</quote><quote date="11/02/1925">Quote nine</quote></author>
</category>
<category name="Music">
<author>Stephen Swann
<quote date="04/08/1972">Quote eleven</quote></author>
</category>
</quotes>
回答by Cylian
Try://category[@name='Sport' and ./author/text()='James Small']
尝试://category[@name='Sport' and ./author/text()='James Small']
回答by Dimitre Novatchev
Use:
使用:
/category[@name='Sport' and author/text()[1]='James Small']
or use:
或使用:
/category[@name='Sport' and author[starts-with(.,'James Small')]]
It is a good rule to try to avoid using the //pseudo-operator whenever possible, because its evaluation can typically be very slow.
尽可能避免使用//伪运算符是一个很好的规则,因为它的计算通常非常慢。
Also:
还:
./somename
is equivalent to:
相当于:
somename
so it is recommended to use the latter.
所以建议使用后者。
回答by akhter wahab
question is not clear, but what i understand you need to select a catagory that has name attribute and should have child author with value specified , correct me if i am worng
问题不清楚,但我的理解是你需要选择一个具有名称属性的类别,并且应该指定具有指定值的子作者,如果我穿了请纠正我
here is a xpath
这是一个xpath
//category[@name='Required value'][./author[contains(.,'Required value')]]
e.g
//category[@name='Sport'][./author[contains(.,'James Small')]]
回答by Suman
You can apply multiple conditions in xpath using and, or
您可以使用和,或在 xpath 中应用多个条件
//input[@class='_2zrpKA _1dBPDZ' and @type='text']
//input[@class='_2zrpKA _1dBPDZ' or @type='text']
回答by Avinash Pande
Here, we can do this way as well:
在这里,我们也可以这样做:
//category [@name='category name']/author[contains(text(),'authorname')]
//category [@name='category name']/author[contains(text(),'authorname')]
OR
或者
//category [@name='category name']//author[contains(text(),'authorname')]
To Learn XPATH in detail please visit- selenium xpath in detail
要详细了解 XPATH,请访问- selenium xpath 详细信息

