xpath以某种模式获取名称

时间:2014-04-04 17:47:58

标签: xpath

我希望获得如下的类名:

class="hostHostGrid0_body"

hostHostGrid和_body之间的整数可以改变,但我想要的其他一切就像顺序中那样。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

在XPath 1.0中,您可以使用:

//*[starts-with(@class,'hostHostGrid') and substring-after(@class,'_') = 'body']

选择包含一个类的任何元素。它将匹配任何上下文中的标记。它将匹配以下所有三个元素:

<div class="hostHostGrid0_body">
    <span class="hostHostGrid123_body"/>
    <b class="hostHostGrid1_body">xxx</b>
</div>

限制:它并不限制它们之间的数字。它可以是任何东西,包括空格(例如:它也将匹配:class="hostHostGrid xyz abc_body"

这个允许在其他类中出现的类:

//*[contains(substring-before(@class,'_body'),'hostHostGrid')]

它将匹配:

<div class="other-class hostHostGrid0_body">
    <span class="hostHostGrid123_body other-class"/>
    <b class="hostHostGrid1_body">xxx</b>
</div>

(它也有相同的限制 - 将匹配&#39; hostHostGrid&#39;和&#39; _body&#39;)