Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="https://github.com/sparklemotion/nokogiri/issues/350" rel="nofollow">problem with Nokogiri's current implementation of <code>:has()</code></a> is that it creates XPath that requires the contents to be a direct child, not any descendant:</p> <pre><code>puts Nokogiri::CSS.xpath_for( "a:has(b)" ) #=&gt; "//a[b]" #=&gt; Should output "//a[.//b]" to be correct </code></pre> <p>To make this XPath match what jQuery does, you need to allow the <code>span</code> to be a descendant element. For example:</p> <pre><code>require 'nokogiri' d = Nokogiri.XML('&lt;r&gt;&lt;a/&gt;&lt;a&gt;&lt;b&gt;&lt;c/&gt;&lt;/b&gt;&lt;/a&gt;&lt;/r&gt;') d.at_css('a:has(b)') #=&gt; #&lt;Nokogiri::XML::Element:0x14dd608 name="a" children=[#&lt;Nokogiri::XML::Element:0x14dd3e0 name="b" children=[#&lt;Nokogiri::XML::Element:0x14dd20c name="c"&gt;]&gt;]&gt; d.at_css('a:has(c)') #=&gt; nil d.at_xpath('//a[.//c]') #=&gt; #&lt;Nokogiri::XML::Element:0x14dd608 name="a" children=[#&lt;Nokogiri::XML::Element:0x14dd3e0 name="b" children=[#&lt;Nokogiri::XML::Element:0x14dd20c name="c"&gt;]&gt;]&gt; </code></pre> <p>For your specific case, here's the full "broken" XPath:</p> <pre><code>puts Nokogiri::CSS.xpath_for( "li:has(span.string:not(:empty)) &gt; h1 &gt; a" ) #=&gt; //li[span[contains(concat(' ', @class, ' '), ' string ') and not(not(node()))]]/h1/a </code></pre> <p>And here it is fixed:</p> <pre><code># Adding just the .// //li[.//span[contains(concat(' ', @class, ' '), ' string ') and not(not(node()))]]/h1/a # Simplified to assume only one CSS class is present on the span //li[.//span[@class='string' and not(not(node()))]]/h1/a # Assuming that `not(:empty)` really meant "Has some text in it" //li[.//span[@class='string' and text()]]/h1/a # ..or maybe you really wanted "Has some text anywhere underneath" //li[.//span[@class='string' and .//text()]]/h1/a # ..or maybe you really wanted "Has at least one element child" //li[.//span[@class='string' and *]]/h1/a </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload