I need to sum certain nodes in xslt based on the index and value of sibling nodes
<p>In the xml structure below I need to use xsl to sum the values of each record type's costs if the record type is "ADD"</p>

<pre><code>&lt;records&gt; 
   ...irrelevant nodes...
   &lt;recordType&gt;NO&lt;/recordType&gt;
   &lt;recordType&gt;ADD&lt;/recordType&gt;
   &lt;recordType&gt;ADD&lt;/recordType&gt;
   ... irrelevant nodes...
   &lt;LdgCost&gt;1&lt;/LdgCost&gt;
   &lt;LabCostIn&gt;2&lt;/LabCostIn&gt;
   &lt;LabCostOut&gt;3&lt;/LabCostOut&gt;
   &lt;LdgCost&gt;4&lt;/LdgCost&gt;
   &lt;LabCostIn&gt;5&lt;/LabCostIn&gt;
   &lt;LabCostOut&gt;6&lt;/LabCostOut&gt;
   &lt;LdgCost&gt;7&lt;/LdgCost&gt;
   &lt;LabCostIn&gt;8&lt;/LabCostIn&gt;
   &lt;LabCostOut&gt;9&lt;/LabCostOut&gt;
   ...irrelevant nodes...
&lt;/records&gt;
</code></pre>

<p>(the record type's costs are the elements below in the same index as the record type). This means the recordType in the first position that is of type "NO" does not need to be added so its values </p>

<pre><code>&lt;LdgCost&gt;1&lt;/LdgCost&gt;
&lt;LabCostIn&gt;2&lt;/LabCostIn&gt;
&lt;LabCostOut&gt;3&lt;/LabCostOut&gt;
</code></pre>

<p>do not need to be summed. However, the next two record types are "ADD" and therefore I need to sum the values of </p>

<pre><code>&lt;LdgCost&gt;4&lt;/LdgCost&gt;
&lt;LabCostIn&gt;5&lt;/LabCostIn&gt;
&lt;LabCostOut&gt;6&lt;/LabCostOut&gt;
&lt;LdgCost&gt;7&lt;/LdgCost&gt;
&lt;LabCostIn&gt;8&lt;/LabCostIn&gt;
&lt;LabCostOut&gt;9&lt;/LabCostOut&gt;
</code></pre>

<p>and set that as my total. Output would just be a total element </p>

<pre><code>&lt;total&gt;39&lt;/total&gt;
</code></pre>

<p>Logically the structure that the above xml structure represents if it were using parent-child nodes is as follows.</p>

<pre><code>&lt;records&gt; 
   &lt;record&gt;
       &lt;recordType&gt;NO&lt;/recordType&gt;
       &lt;LdgCost&gt;1&lt;/LdgCost&gt;
       &lt;LabCostIn&gt;2&lt;/LabCostIn&gt;
       &lt;LabCostOut&gt;3&lt;/LabCostOut&gt;
   &lt;/record&gt;
   &lt;record&gt;
       &lt;recordType&gt;ADD&lt;/recordType&gt;
       &lt;LdgCost&gt;4&lt;/LdgCost&gt;
       &lt;LabCostIn&gt;5&lt;/LabCostIn&gt;
       &lt;LabCostOut&gt;6&lt;/LabCostOut&gt;
   &lt;/record&gt;
   &lt;record&gt;
       &lt;recordType&gt;ADD&lt;/recordType&gt;
       &lt;LdgCost&gt;7&lt;/LdgCost&gt;
       &lt;LabCostIn&gt;8&lt;/LabCostIn&gt;
       &lt;LabCostOut&gt;9&lt;/LabCostOut&gt;
   &lt;/record&gt;
&lt;/records&gt;
</code></pre>

<p>However instead of using this structure I have to base the parent child relationships based on the index of the sibling elements.</p>

<p>The way I would do this manually is as follows:</p>

<ol>
<li><p>Find the first <code>&lt;recordType&gt;</code> element, Check if value is ADD or NO. Since value is NO I skip this one.</p></li>
<li><p>Find the next <code>&lt;recordType&gt;</code> element, Check if value is ADD or NO. Since value is ADD then I would need to find the 3 costs related to this second record. These cost elements are guaranteed to exist and be in the index corresponding to their recordType.</p></li>
<li><p>Find <code>&lt;LdgCost&gt;</code>[2] element value and add it to sum. (the index is two because we are on the second recordType since we skipped the first one and its costs were ignored) </p></li>
<li><p>Find <code>&lt;LdgCostIn&gt;</code>[2] element value and add it to sum.</p></li>
<li><p>Find <code>&lt;LdgCostOut&gt;</code>[2] element value and add it to the sum.</p></li>
<li><p>Now that we have added the three costs from the second record to the sum and we move on to the third <code>&lt;recordType&gt;</code> element and check its value. Since the value is ADD we find the 3 costs associated to the third record.</p></li>
<li><p>Find <code>&lt;LdgCost&gt;</code>[3] element value and add it to sum.(the index is three because we are on the third recordType)</p></li>
<li><p>Find <code>&lt;LdgCostIn&gt;</code>[3] element value and add it to sum.</p></li>
<li><p>Find <code>&lt;LdgCostOut&gt;</code>[3] element value and add it to the sum.</p></li>
<li><p>There are no more <code>&lt;recordType&gt;</code> elements to process so we return the sum of 39.</p></li>
</ol>