简体   繁体   English

xpath:如何组合不同轴上的多个条件

[英]xpath: How to combine multiple conditions on different axes

I try to extract all links based on these three conditions:我尝试根据这三个条件提取所有链接:

  • Must be part of <div data-test="cond1">必须是<div data-test="cond1">一部分
  • Must have a <a href="..." class="cond2">必须有一个<a href="..." class="cond2">
  • Must not have a <img src="..." class="cond3">不能有<img src="..." class="cond3">

The result should be "/product/1234".结果应该是“/product/1234”。

<div data-test="test1">
  <div>
    <div data-test="cond1">
      <a href="/product/1234" class="cond2">Link 1</a>
      <div class="test4">
        <div class="test5">
          <div class="test6">
            <div class="test7">
              <div class="test8">
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<div data-test="test2">
  <div>
    <div data-test="cond1">
      <a href="/product/5678" class="cond2">Link 2</a>
      <div class="test4">
        <div class="test5">
          <div class="test6">
            <div class="test7">
              <div class="test8">
                <img src="bild.jpg" class="cond3">
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

I'm able to extract the links with the following xpath query.我能够使用以下 xpath 查询提取链接。

//div[starts-with(@data-test,"cond")]/a[starts-with(@class,"cond")]/@href

(I know the first part is not really neccessary. But better safe than sorry.) (我知道第一部分并不是真正必要的。但安全总比后悔好。)

But I'm still struggling with excluding the links containing an descendant img tag and how to add it to the query above.但我仍在努力排除包含后代 img 标签的链接以及如何将其添加到上面的查询中。

This should do what you want:这应该做你想做的:

//div[@data-test="cond1" and not(.//img[@class="cond3"])]
/a[@class="cond2"]
/@href
/product/1234

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM