Note that there are some explanatory texts on larger screens.

plurals
  1. POXPath and negation searches
    primarykey
    data
    text
    <p>I have the following code sample in an <code>xmlns</code> root:</p> <pre><code>&lt;ol class="stan"&gt; &lt;li&gt;Item one.&lt;/li&gt; &lt;li&gt; &lt;p&gt;Paragraph one.&lt;/p&gt; &lt;p&gt;Paragraph two.&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;pre&gt;Preformated one.&lt;/pre&gt; &lt;p&gt;Paragraph one.&lt;/p&gt; &lt;/li&gt; &lt;/ol&gt; </code></pre> <p>I would like to perform a different operation on the first item in <code>&lt;li&gt;</code> depending on the type of tag it resides in, or no tag, i.e. the first <code>&lt;li&gt;</code> in the sample.</p> <hr> <p>EDIT:</p> <p>My logic in pursuing the task turns out to be incorrect.</p> <ol> <li>How do I query a <code>&lt;li&gt;</code> that has no descendants as in the first list item?</li> </ol> <p>I tried negation:</p> <pre><code>@doc.xpath("//xmlns:ol[@class='stan']//xmlns:li/xmlns:*[1][not(p|pre)]") </code></pre> <p>That gives me the exact opposite for what I think I am asking for.</p> <p>I think I am making the expression more complicated since I can't find the right solution.</p> <hr> <p>UPDATE:</p> <p>Navin Rawat has answered this one in the comments. The correct code would be:</p> <pre><code>@doc.xpath("//xmlns:ol[@class='stan']/xmlns:li[not(xmlns:*)]") </code></pre> <hr> <p>CORRECTION:</p> <p>The correct question involves both an XPath search and a Nokogiri method.</p> <p>Given the above <code>xhtml</code> code, how do I search for first descendant using <code>xpath</code>? And how do I use <code>xpath</code> in a conditional statement, e.g.:</p> <pre><code>@doc.xpath("//xmlns:ol[@class='stan']/xmlns:li").each do |e| if e.xpath("e has no descendants") perform task elsif e.xpath("e first descendant is &lt;p&gt;") perform second task elsif e.xpath("e first descendant is &lt;pre&gt;") perform third task end end </code></pre> <p>I am not asking for complete code. Just the part in parenthesis in the above <code>Nokogiri</code> code.</p>
    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. 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