sum-aggregate function with non-unique values
<p>I have a set of documents with a structure like:</p>

<pre><code>&lt;DOCUMENT&gt;
    &lt;AMOUNTS&gt;
        &lt;ELEMENT&gt;
            &lt;AMOUNT&gt;10.00&lt;/AMOUNT&gt;
            &lt;INFO&gt;
                &lt;CODE1&gt;132&lt;/CODE1&gt;
                &lt;CODE2&gt;022&lt;/CODE2&gt;
            &lt;/INFO&gt;
        &lt;/ELEMENT&gt;
        &lt;ELEMENT&gt;
            &lt;AMOUNT&gt;10.00&lt;/AMOUNT&gt;
            &lt;INFO&gt;
                &lt;CODE1&gt;132&lt;/CODE1&gt;
                &lt;CODE2&gt;121&lt;/CODE2&gt;
            &lt;/INFO&gt;
        &lt;/ELEMENT&gt;
        &lt;ELEMENT&gt;
            &lt;AMOUNT&gt;15.00&lt;/AMOUNT&gt;
            &lt;INFO&gt;
                &lt;CODE1&gt;156&lt;/CODE1&gt;
                &lt;CODE2&gt;121&lt;/CODE2&gt;
            &lt;/INFO&gt;
        &lt;/ELEMENT&gt;      
    &lt;/AMOUNTS&gt;
&lt;/DOCUMENT&gt;
</code></pre>

<p>I’m looking to do various sums of the <strong>AMOUNT</strong> element so I’ve put a Path Range Index on the <code>DOCUMENT/AMOUNTS/ELEMENT/AMOUNT</code> element hoping to use the <code>sum-aggregate</code> function. However I’m seeing an issue when using the <code>cts:sum-aggregate</code> function when the sum involves documents that contain more than one <strong>AMOUNT</strong> element with the same value.  To illustrate the issue I’m seeing assume the XML above is stored at the '<code>/DOCS/DOC1.XML</code>' uri.  I then run the following xQuery to get the sum of all the <strong>AMOUNTs</strong> in the document. I'm doing the sum in two different ways and getting two different results:</p>

<pre><code>(
  fn:sum(doc('/DOCS/DOC1.XML')/DOCUMENT/AMOUNTS/ELEMENT/AMOUNT),
  cts:sum-aggregate(
      cts:path-reference("DOCUMENT/AMOUNTS/ELEMENT/AMOUNT"), 
      ("any"),
      cts:document-query('/DOCS/DOC1.XML')
  )
) 
</code></pre>

<p>The <code>fn:sum</code> function gives 35 and the <code>cts:sum-aggregate</code> gives 25.  The sum-aggregate function is only including one of the <strong>10</strong> values in the sum.</p>

<p>I think I’m doing something wrong but I can’t figure out what, can someone shed some light on this for me?</p>

<p>Thanks</p>

<p>David</p>