首页 > 解决方案 > 如何检查xpath中的节点是否有某个子x但也没有子y

问题描述

我正在尝试获取给定 github 用户的非分叉存储库。目前,我设法使用此 xpath 查询获取所有存储库:

parser.xpath("//ul[@data-filterable-for='your-repos-filter']/li/div/div/h3/a/@href").map{|repository| ...}

关键是我需要过滤掉最后一个 div 的下一个“兄弟”不是a 的那些span,例如:

parser.xpath("//ul[@data-filterable-for='your-repos-filter']/li/div/div/h3 and not span/a/@href").map{|repository| ...}

我正在寻找的 HTML如下(检查其中一个分叉的存储库):

<li class="col-12 d-flex width-full py-4 border-bottom public fork" itemprop="owns" itemscope itemtype="http://schema.org/Code">
  <div class="col-10 col-lg-9 d-inline-block">
    <div class="d-inline-block mb-1">
      <h3 class="wb-break-all">
        <a href="/DominikAngerer/rails-boilerplate" itemprop="name codeRepository" >
        rails-boilerplate</a>

      </h3>

        <span class="f6 text-gray mb-1">
          Forked from <a class="muted-link" href="/polomasta/rails-boilerplate">polomasta/rails-boilerplate</a>
        </span>

    </div>

    <div>
        <p class="col-9 d-inline-block text-gray mb-2 pr-4" itemprop="description">
          Ruby on Rails Storyblok Starter Boilerplate
        </p>
    </div>

什么时候不是分叉存储库,我正在寻找的那些,没有这样的<span class="f6 text-gray mb-1">

是否有可能进行这样的查询,如果可以,如何查询?

标签: rubyxpathnokogiri

解决方案


You can use the following XPath to select the links of non-forked repositories :

//div[@class="d-inline-block mb-1"][not(./span[contains(.,"Forked from")])]//@href

Output : 17 nodes for https://github.com/DominikAngerer?tab=repositories


推荐阅读