title: xpath sum of all preceding-siblings matching a value of tag in node
body: <p>I'm stuck on the following problem:</p>
  
  <p>Using an xpath expression I have to sum all preceding siblings matching a tag's value.
  Actually I'm on the last node element with a certain id.</p>
  
  <p>Relative to this position I have to output the sum of all preceding siblings matching the the same id located in ./id</p>
  
  <p>I am limited to xpath 2.0 for architectural reasons and have to find a solution by using a xpath expression.</p>
  
  <p>sample structure</p>
  
  <pre><code>&lt;root&gt;
  ...
  &lt;element&gt;
      &lt;id&gt;198&lt;/id&gt;
      &lt;sum&gt;1&lt;/sum&gt;
  &lt;/element&gt;
  &lt;element&gt;
      &lt;id&gt;198&lt;/id&gt;
      &lt;sum&gt;0&lt;/sum&gt;
  &lt;/element&gt;
  &lt;element&gt;
      &lt;id&gt;267&lt;/id&gt;
      &lt;sum&gt;2&lt;/sum&gt;
  &lt;/element&gt;
  &lt;element&gt;
      &lt;id&gt;267&lt;/id&gt;
      &lt;sum&gt;0&lt;/sum&gt;
  &lt;/element&gt;
  &lt;element&gt;
      &lt;id&gt;267&lt;/id&gt;
      &lt;sum&gt;0&lt;/sum&gt;
  &lt;/element&gt;
  &lt;element&gt;
      &lt;id&gt;267&lt;/id&gt;
      &lt;sum&gt;0&lt;/sum&gt;
  &lt;/element&gt;
  &lt;element&gt;
      &lt;id&gt;3123&lt;/id&gt;
      &lt;sum&gt;34&lt;/sum&gt;
  &lt;/element&gt;
  &lt;element&gt;
      &lt;id&gt;3123&lt;/id&gt;
      &lt;sum&gt;0&lt;/sum&gt;
  &lt;/element&gt;
  &lt;element&gt;
      &lt;id&gt;3123&lt;/id&gt;
      &lt;sum&gt;0&lt;/sum&gt;
  &lt;/element&gt;
  ...
  &lt;/root&gt;
  </code></pre>
  
  <p>i tried:</p>
  
  <p><code>sum(preceding-sibling::element[./id = self::*/id]/sum)</code></p>
  
  <p>the result of the expression did not match the expected value: 
  The expression summs up the values of all  Tags that are located before the starting element and not only the specific ones.</p>
  
  <p>I'd like to get the following results:</p>
  
  <p>last element with id 198  the result should be 1 </p>
  
  <p>last element with id 267  the result should be 2</p>
  
  <p>last element with id 3123 the result should be 34</p>
  
  <p>Thanks for any suggestions.</p>