xpath尝试选择除div之外的div内的内容,包括文本

时间:2013-03-26 11:53:18

标签: xpath

我试图选择div中的内容,这个div里面有一些文本和一些额外的标签。我不想选择里面的第一个div。我正在尝试使用这个选择器,但只给我标签,没有文字

//div[@class='contentDealDescriptionFacts cf']/div[@class='viewHalfWidthSize' and position()=2]/*[not(@class='subHeadline')]

给我带来问题的div就是这个:

<div class="viewHalfWidthSize">
    .......
</div>

<div class="viewHalfWidthSize">
    <div class="subHeadline firefinder-match">The Fine Print</div> <----------Except this div I want everything inside of this div!!
    <strong class="firefinder-match">Validity: </strong>
    Expires 27 June 2013.
    <br class="firefinder-match">
    <strong class="firefinder-match">Purchase: </strong>
    Limit 1 per 2 people. May buy multiple as gifts.
    <br class="firefinder-match">
    <strong class="firefinder-match">Redemption: </strong>
    Booking required online at
    <a target="_blank" href="http://grouponbookings.co.uk/lautre-pied-march/"      class="firefinder-match">http://grouponbookings.co.uk/lautre-pied-march/</a>
. 48-hour cancellation policy; late cancellation incurs a £30 surcharge per person.
    <br class="firefinder-match">
    <strong class="firefinder-match">Further information: </strong>
    Valid Mon-Sun midday-2.45pm; Mon-Wed 6pm-10.45pm. Must be 18 or older, ID may be   requested. Valid only on set tasting menu only; menu is dependent on market changes and seasonality and is subject to change. Max. two hours seating time. Discretionary service charge will be added to the bill based on original price. Original value verified 19 March 2013 at 9.01am.
   <br class="firefinder-match">
   <a target="_blank" href="http://www.groupon.co.uk/universal-fine-print" style="color: #339933;" class="firefinder-match">See the rules</a>
that apply to all deals.
</div>

1 个答案:

答案 0 :(得分:0)

*匹配元素节点而不匹配文本节点。尝试用*替换node()以选择所有节点类型。

要分解你的XPath正在做什么:

您正在查找文档(//)中任何位于“contentDealDescriptionFacts cf”类的div。

然后你正在寻找同样拥有班级viewHalfWidthSize的第二个div。注意,这不是具有类的第二个div,而是第二个AND的div具有该类,因此如果具有该类的div是第3个和第4个,则它将不匹配任何内容,因为第2个div与该类具有{ {1}}。如果你想要第二个position() = 4 div,那么你需要viewHalfWidthSize

最后,您将返回没有类[@class='viewHalfWidthSize'][position()=2]的所有元素的节点列表。如果您将subHeadline更改为*,那么您将获得所有节点的节点列表。


以下XPath:

node()
只要第一个子节点是您想要忽略的div,

就应该返回您想要的内容。

如果您将其更改为:

//div[@class='contentDealDescriptionFacts cf']/div[@class='viewHalfWidthSize' and position()=2]/node()[not(name(.)='div' and position() = 1)]

那么无论如何都应该有效。它返回你的节点列表,然后计算出在第一个div之前有多少先前节点,并检查位置是否大于那个(即第一个div的位置)并从列表中排除。


作为另一种选择,您可以修改原始解决方案,但不应该执行//div[@class='contentDealDescriptionFacts cf']/div[@class='viewHalfWidthSize' and position()=2]/node()[position() != count(../div[1]/preceding-sibling::node()) + 1]

not(@class='subHeadline')

,假设您的类是空格分隔的,它将在字符串中的任何位置检查类属性是否包含not(contains(concat(' ', @class, ' '), ' subHeadline ')) 。然后,这将匹配具有类subHeadline

的片段
相关问题