title: XSLT: Sorting based on sum of values from other nodes
body: <p>I'm rather new to coding xslt and have got rather stuck trying to do the following.</p>
  
  <p>I have an xml file that has breeding info for horses broken into two main sections. 1. Horses node has the performance details of individual horses as well as an id to who their sire was.  2. Sires node is list of the Sires also holding breeding specific statistics.</p>
  
  <p>I need to sort the list of sires based on the sum of the 'stake' money won by their foals (i.e. in the horses node).</p>
  
  <p>So a cut down xml file looks like this:</p>
  
  <pre><code>&lt;Horses&gt;
      &lt;Horse&gt;
          &lt;ID&gt;1&lt;/ID&gt;
          &lt;Name&gt;hrsA&lt;/Name&gt;
          &lt;SireID&gt;101&lt;/SireID&gt;
          &lt;Pace&gt;
              &lt;Stakes&gt;4800&lt;/Stakes&gt;
          &lt;/Pace&gt;
      &lt;/Horse&gt;
      &lt;Horse&gt;
          &lt;ID&gt;2&lt;/ID&gt;
          &lt;Name&gt;hrsB&lt;/Name&gt;
          &lt;SireID&gt;102&lt;/SireID&gt;
          &lt;Pace&gt;
              &lt;Stakes&gt;3600&lt;/Stakes&gt;
          &lt;/Pace&gt;
      &lt;/Horse&gt;
      &lt;Horse&gt;
          &lt;ID&gt;3&lt;/ID&gt;
          &lt;Name&gt;hrsC&lt;/Name&gt;
          &lt;SireID&gt;102&lt;/SireID&gt;
          &lt;Pace&gt;
              &lt;Stakes&gt;2800&lt;/Stakes&gt;
          &lt;/Pace&gt;
      &lt;/Horse&gt;
      &lt;Horse&gt;
          &lt;ID&gt;4&lt;/ID&gt;
          &lt;Name&gt;hrsD&lt;/Name&gt;
          &lt;SireID&gt;101&lt;/SireID&gt;
          &lt;Pace&gt;
              &lt;Stakes&gt;56&lt;/Stakes&gt;
          &lt;/Pace&gt;
      &lt;/Horse&gt;
      &lt;Horse&gt;
          &lt;ID&gt;5&lt;/ID&gt;
          &lt;Name&gt;hrsE&lt;/Name&gt;
          &lt;SireID&gt;100&lt;/SireID&gt;
          &lt;Pace&gt;
              &lt;Stakes&gt;20000&lt;/Stakes&gt;
          &lt;/Pace&gt;
      &lt;/Horse&gt;
      &lt;Horse&gt;
          &lt;ID&gt;6&lt;/ID&gt;
          &lt;Name&gt;hrsF&lt;/Name&gt;
          &lt;SireID&gt;101&lt;/SireID&gt;
          &lt;Trot&gt;
              &lt;Stakes&gt;20000&lt;/Stakes&gt;
          &lt;/Trot&gt;
      &lt;/Horse&gt;
      &lt;Horse&gt;
          &lt;ID&gt;7&lt;/ID&gt;
          &lt;Name&gt;hrsG&lt;/Name&gt;
          &lt;SireID&gt;101&lt;/SireID&gt;
          &lt;Trot&gt;
              &lt;Stakes&gt;559&lt;/Stakes&gt;
          &lt;/Trot&gt;
      &lt;/Horse&gt;
      &lt;Horse&gt;
          &lt;ID&gt;8&lt;/ID&gt;
          &lt;Name&gt;hrsH&lt;/Name&gt;
          &lt;SireID&gt;102&lt;/SireID&gt;
          &lt;Pace&gt;
              &lt;Stakes&gt;386&lt;/Stakes&gt;
          &lt;/Pace&gt;
          &lt;Trot&gt;
              &lt;Stakes&gt;10000&lt;/Stakes&gt;
          &lt;/Trot&gt;
      &lt;/Horse&gt;
  &lt;/Horses&gt;
  &lt;Sires&gt;
      &lt;Sire&gt;
          &lt;ID&gt;100&lt;/ID&gt;
          &lt;Name&gt;srA&lt;/Name&gt;
          &lt;LiveFoalsALL&gt;117&lt;/LiveFoalsALL&gt;
      &lt;/Sire&gt;
      &lt;Sire&gt;
          &lt;ID&gt;101&lt;/ID&gt;
          &lt;Name&gt;srB&lt;/Name&gt;
          &lt;LiveFoalsALL&gt;774&lt;/LiveFoalsALL&gt;
      &lt;/Sire&gt;
      &lt;Sire&gt;
          &lt;ID&gt;102&lt;/ID&gt;
          &lt;Name&gt;srC&lt;/Name&gt;
          &lt;LiveFoalsALL&gt;43&lt;/LiveFoalsALL&gt;
      &lt;/Sire&gt;
  &lt;/Sires&gt;
  </code></pre>
  
  <p>So summing the various stakes the output would have this ordering:</p>
  
  <pre><code>Sire 101 (srB) Stakes: $25415
  Sire 100 (srA) Stakes: $20000
  Sire 103 (srC) Stakes: $16768.
  </code></pre>
  
  <p>When I was just summing and ordering the individual horse stakes for other web pages I was able to use:</p>
  
  <pre><code>&lt;xsl:apply-templates select="Horse"&gt;
      &lt;xsl:sort select="sum(descendant::Stakes)" data-type="number"
          order="descending"/&gt;
  &lt;/xsl:apply-templates&gt;
  </code></pre>
  
  <p>I just can't figure out how to do the same for the sires referencing the horse node for summing to get the right ordering... possibly something like this where I try to say the Sire/ID is equal to the Horse/SireID:</p>
  
  <pre><code>&lt;xsl:apply-templates select="Sire"&gt;
      &lt;xsl:sort select="sum(//Horses/Horse[@SireID=ID]/descendant::Stakes)" 
          data-type="number" order="descending"/&gt;
  &lt;/xsl:apply-templates&gt;
  </code></pre>
  
  <p>But that doesn't work, the debugger jumps straight out of the current template when it hits the sort line so my syntax must be invalid.
  I've been trying variations on this theme without success.</p>
  
  <p>Can anyone please give me a pointer of how to call my Sire template and get the correct ordering?</p>
  
  <p>Thanks,</p>
  
  <p>Bryce Stenberg</p>