title: xpath get last of specific preceding-sibling
body: <p>Here's something really simple (at least I guess), I just do not get the clue.</p>
  
  <p>I have to parse a large XML document to get a specific node, identified by one of its subnode values. Thats easy so far. But when I try to parse onward from that node relatively upward, selecting the preceding-siblings of its ancestor by using a predicate I get a list of nodes, from that on I have to walk downward again.</p>
  
  <p>In Theorie, that is a table, with 5 columns and two rows (in the shown example below). I get just the id element of one field, and need to find the name given in the first field of the row. The first field is always of type 'Link' and has a name subnode with text - which is the thing to get.</p>
  
  <p>In other words, I need to move from any node with an <code>&lt;id&gt;XXX_X&lt;/i&gt;</code> to the next preceding-sibling cell with a control of <code>xsi:type='Label'</code> and a name node. From the node <code>&lt;id&gt;MyItemId_1&lt;/&gt;</code> I need to get the second preceding-sibling, from the node <code>&lt;id&gt;MyItemId_4&lt;/id&gt;</code> I need to get the 5th preceding-sibling.</p>
  
  <p>This is a sample xml piece:</p>
  
  <pre><code>&lt;cell&gt;
      &lt;control xsi:type="Label"&gt;
          &lt;id&gt;1234&lt;/id&gt;
          &lt;name&gt;MyOtherItemName&lt;/name&gt;
          &lt;message/&gt;
      &lt;/control&gt;
      &lt;selected&gt;false&lt;/selected&gt;
      &lt;style&gt;Odd&lt;/style&gt;
  &lt;/cell&gt;
  &lt;cell&gt;
      &lt;control xsi:type="Label"&gt;
          &lt;id&gt;MyOtherItemId_0&lt;/id&gt;
          &lt;name/&gt;
          &lt;message/&gt;
      &lt;/control&gt;
      &lt;selected&gt;false&lt;/selected&gt;
      &lt;style&gt;Odd&lt;/style&gt;
  &lt;/cell&gt;
  &lt;cell&gt;
      &lt;control xsi:type="Label"&gt;
          &lt;id&gt;MyOtherItemId_1&lt;/id&gt;
          &lt;name/&gt;
          &lt;message/&gt;
      &lt;/control&gt;
      &lt;selected&gt;false&lt;/selected&gt;
      &lt;style&gt;Odd&lt;/style&gt;
  &lt;/cell&gt;
  &lt;cell&gt;
      &lt;control xsi:type="Button"&gt;
          &lt;id&gt;MyOtherItemId_2&lt;/id&gt;
          &lt;name&gt;552&lt;/name&gt;
          &lt;message/&gt;
          &lt;type&gt;Link&lt;/type&gt;
          &lt;selected&gt;false&lt;/selected&gt;
      &lt;/control&gt;
      &lt;selected&gt;false&lt;/selected&gt;
      &lt;style&gt;Odd&lt;/style&gt;
  &lt;/cell&gt;
  &lt;cell&gt;
      &lt;control xsi:type="Button"&gt;
          &lt;id&gt;MyOtherItemId_3&lt;/id&gt;
          &lt;name&gt;432&lt;/name&gt;
          &lt;message/&gt;
          &lt;type&gt;Link&lt;/type&gt;
          &lt;selected&gt;false&lt;/selected&gt;
      &lt;/control&gt;
      &lt;selected&gt;false&lt;/selected&gt;
      &lt;style&gt;Odd&lt;/style&gt;
  &lt;/cell&gt;
  &lt;cell&gt;
      &lt;control xsi:type="Button"&gt;
          &lt;id&gt;MyOtherItemId_4&lt;/id&gt;
          &lt;name&gt;33&lt;/name&gt;
          &lt;message/&gt;
          &lt;type&gt;Link&lt;/type&gt;
          &lt;selected&gt;false&lt;/selected&gt;
      &lt;/control&gt;
      &lt;selected&gt;false&lt;/selected&gt;
      &lt;style&gt;Odd&lt;/style&gt;
  &lt;/cell&gt;
  &lt;cell&gt;
      &lt;control xsi:type="Label"&gt;
          &lt;id&gt;1234&lt;/id&gt;
          &lt;name&gt;MyItemName&lt;/name&gt;
          &lt;message/&gt;
      &lt;/control&gt;
      &lt;selected&gt;false&lt;/selected&gt;
      &lt;style&gt;Odd&lt;/style&gt;
  &lt;/cell&gt;
  &lt;cell&gt;
      &lt;control xsi:type="Label"&gt;
          &lt;id&gt;MyItemId_0&lt;/id&gt;
          &lt;name/&gt;
          &lt;message/&gt;
      &lt;/control&gt;
      &lt;selected&gt;false&lt;/selected&gt;
      &lt;style&gt;Odd&lt;/style&gt;
  &lt;/cell&gt;
  &lt;cell&gt;
      &lt;control xsi:type="Label"&gt;
          &lt;id&gt;MyItemId_1&lt;/id&gt;
          &lt;name/&gt;
          &lt;message/&gt;
      &lt;/control&gt;
      &lt;selected&gt;false&lt;/selected&gt;
      &lt;style&gt;Odd&lt;/style&gt;
  &lt;/cell&gt;
  &lt;cell&gt;
      &lt;control xsi:type="Button"&gt;
          &lt;id&gt;MyItemId_2&lt;/id&gt;
          &lt;name&gt;552&lt;/name&gt;
          &lt;message/&gt;
          &lt;type&gt;Link&lt;/type&gt;
          &lt;selected&gt;false&lt;/selected&gt;
      &lt;/control&gt;
      &lt;selected&gt;false&lt;/selected&gt;
      &lt;style&gt;Odd&lt;/style&gt;
  &lt;/cell&gt;
  &lt;cell&gt;
      &lt;control xsi:type="Button"&gt;
          &lt;id&gt;MyItemId_3&lt;/id&gt;
          &lt;name&gt;432&lt;/name&gt;
          &lt;message/&gt;
          &lt;type&gt;Link&lt;/type&gt;
          &lt;selected&gt;false&lt;/selected&gt;
      &lt;/control&gt;
      &lt;selected&gt;false&lt;/selected&gt;
      &lt;style&gt;Odd&lt;/style&gt;
  &lt;/cell&gt;
  &lt;cell&gt;
      &lt;control xsi:type="Button"&gt;
          &lt;id&gt;MyItemId_4&lt;/id&gt;
          &lt;name&gt;33&lt;/name&gt;
          &lt;message/&gt;
          &lt;type&gt;Link&lt;/type&gt;
          &lt;selected&gt;false&lt;/selected&gt;
      &lt;/control&gt;
      &lt;selected&gt;false&lt;/selected&gt;
      &lt;style&gt;Odd&lt;/style&gt;
  &lt;/cell&gt;
  </code></pre>
  
  <p>I do get the item i have to get with this xpath:</p>
  
  <pre><code>//cell[control[type='Link']]/control[type='Link' and selected='false' and id='MyItemId_3']/id
  </code></pre>
  
  <p>That selects the id of the control of the cell, namely the 4th column in the second row, of the rendered table.</p>
  
  <p>From that node on I try moving to the first cell in the row by following this path:</p>
  
  <pre><code>../../preceding-sibling::cell[control[@xsi:type='Label' and name[node()]]]/control[name[node()]]/name
  </code></pre>
  
  <p>That gives me the two correct cells of the first column of the table. </p>
  
  <pre><code>&lt;name&gt;MyOtherItemName&lt;/name&gt;
  * * * * * * * * * *
  &lt;name&gt;MyItemName&lt;/name&gt;
  </code></pre>
  
  <p>Now it breaks my back since I can't get it to just give me back the last one of the two selected.</p>
  
  <p>I tried this:</p>
  
  <pre><code>../../preceding-sibling::cell[control[@xsi:type='Label' and name[node()]]][1]/control[name[node()]]/name
  </code></pre>
  
  <p>which is a preceding-sibling selection with a predicate to exactly the sort of siblings I search for, but it seems I can not combine that predicate with a [1] selector. Instead of selecting the desired first preceding sibling "MyItemName" it selects the first sibling from all preceding ones "MyOtherItemName".</p>
  
  <p>I need help, hope someone here has a clue and can pinpoint me in the right direction.</p>
  
  <p>Exactly what I set up to get this work is copying the xml into <a href="http://www.bit-101.com/xpath/" rel="noreferrer">http://www.bit-101.com/xpath/</a>
  and working with the concatenated xpathes on it to simulate what the software should do:</p>
  
  <pre><code>//cell[control[type='Link']]/control[type='Link' and selected='false' and id='MyItemId_3']/id/../../preceding-sibling::cell[control[@xsi:type='Label' and name[node()]]]/control[name[node()]]/name
  </code></pre>