title: Xpath to select only direct siblings with matching attributes
body: <p>I have the following example document:</p>
  
  <pre><code>&lt;root&gt;
    &lt;p class="b"&gt;A&lt;/p&gt;
    &lt;p class="b"&gt;B&lt;/p&gt;
    &lt;p class="a"&gt;C&lt;/p&gt;
    &lt;p class="a"&gt;D&lt;/p&gt;
    &lt;p class="b"&gt;E&lt;/p&gt;
    &lt;x&gt;
      &lt;p class="b"&gt;F&lt;/p&gt;
    &lt;/x&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>I am looking for an xpath expression which selects all <em>direct</em> siblings of a given node with matching class attributes, not any sibling.  In above example, the first two <code>&lt;p class="b"&gt;</code> A-B should be selected; likewise the two <code>&lt;p class="a"&gt;</code> C-D, likewise the fifth single <code>&lt;p class="b"&gt;</code> E as it has no <em>direct</em> siblings; likewise the single <code>&lt;p class="b"&gt;</code> F inside of <code>&lt;x&gt;</code>. Note that in this context B and C are not direct siblings because they have different class attribute valued!</p>
  
  <p>What I have is this:</p>
  
  <pre><code>xml.xpath("//p") # This selects all six &lt;p&gt; elements.
  xml.xpath("//p[@class='b']") # This selects all four &lt;p class="b"&gt; elements.
  xml.xpath("//p/following-sibling::p[@class='b']") # This selects all &lt;p class="b"&gt; sibling elements, even though not direct siblings.
  </code></pre>
  
  <p>The last expression selects the fifth sibling as well, although there are non-matching siblings inbetween.</p>
  
  <p>How do I select only direct siblings with the same <code>class</code> value?</p>
  
  <p><strong>Edit</strong> To clarify: note how the last two are individual selections, not siblings!</p>
  
  <p><strong>Edit</strong> I have saved an example <a href="http://www.xpathtester.com/obj/d091aa13-df0b-455d-be9d-66a7ce66c477">here</a>.  The Xpath expression based on <code>/root/p[1]</code> is supposed to select <code>A, B, C, D</code>.</p>