Java 选择属性以 XPath 中的某些内容开头的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3301898/
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
Selecting elements whose attribute begins with something in XPath
提问by Allen Gingrich
As the title says, is it possible to select elements in XPath that only begin with a certain string, but perhaps do not end with the same?
正如标题所说,是否可以在 XPath 中选择仅以某个字符串开头但可能不以相同结尾的元素?
For example there are 3 anchor elements:
例如有 3 个锚元素:
<a href="buy.php/onething"></a><a href="buy.php/twothing"></a><a href="sell.php/anotherthing"></a>
I only want to get anchor elements that begin with 'buy.php/'. I don't think the following will work, will it:
我只想获得以“buy.php/”开头的锚元素。我认为以下方法行不通,是吗:
getByXPath("//a[@href='buy.php/']")
How can I do this?
我怎样才能做到这一点?
采纳答案by MooGoo
//a[starts-with(@href, 'buy.php/')]
//a[starts-with(@href, 'buy.php/')]
http://www.zvon.org/xxl/XSLTreference/Output/function_starts-with.html
http://www.zvon.org/xxl/XSLTreference/Output/function_starts-with.html
回答by Michael Bazos
Not sure if this is exactly the correct syntax but you probably want to use the fn:contains xpath function. Other useful functions you can find here:
不确定这是否是正确的语法,但您可能想要使用 fn:contains xpath 函数。您可以在此处找到其他有用的功能:
http://www.w3schools.com/xpath/xpath_functions.asp#string
http://www.w3schools.com/xpath/xpath_functions.asp#string
getByXPath("//a[fn:contains(@href/text(), 'buy.php/')]")
getByXPath("//a[fn:contains(@href/text(), 'buy.php/')]")