Select adjacent sibling elements without intervening non-whitespace text nodes

SpliFF-SmOkeR

New Member
Given markup like:\[code\]<p> <code>foo</code><code>bar</code> <code>jim</code> and then <code>jam</code></p>\[/code\]I need to select the first three \[code\]<code>\[/code\]—but not the last. The logic is "Select all \[code\]code\[/code\] elements that have a preceding-or-following-sibling-element that is also a \[code\]code\[/code\], unless there exist one or more text nodes with non-whitespace content between them.Given that I am using Nokogiri (which uses libxml2) I can only use XPath 1.0 expressions.Although a tricky XPath expression is desired, Ruby code/iterations to perform the same on a Nokogiri document are also acceptable.Note that the CSS adjacent sibling selector ignores non-element nodes, and so selecting \[code\]nokodoc.css('code + code')\[/code\] will incorrectly select the last \[code\]<code>\[/code\] block.\[code\]Nokogiri.XML('<r><a/><b/> and <c/></r>').css('* + *').map(&:name)#=> ["b", "c"]\[/code\]Edit: More test cases, for clarity:\[code\]<section><ul> <li>Go to <code>N</code> and then <code>Y</code><code>Y</code><code>Y</code>. </li> <li>If you see <code>N</code> or <code>N</code> then
 
Back
Top