有没有可能使用通配符选择项目的XPath?例如,我尝试//img[@class='poly *']
选择这些img
元素,
<img class="poly x1" title="title1">
<img class="poly x2" title="title2">
<img class="poly x3" title="title3">
但这没用。
最佳答案
*
在XPath中不是通配符,因此
//img[@class='poly *']
将使所有
img
元素与class
属性值完全等于poly *
的字面值匹配-字面上以*
字符结尾。您可以改用
//img[starts-with(@class,'poly')]
或更强大的习惯用法,即允许
poly
出现在class
属性值的任何位置,而不匹配polymorphism
等。等因为失误://img[contains(concat(' ',@class,' '), ' poly ')]
注意:XPath 2.0具有支持正则表达式的
matches()
函数。