 <p><strong>This transformation</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:key name="kOffspring" match="Horse" use="SireID"/&gt; &lt;xsl:template match="/*"&gt; &lt;xsl:apply-templates select="Sires/Sire"&gt; &lt;xsl:sort select="sum(key('kOffspring', ID)/*/Stakes)" data-type="number" order="descending"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Sire"&gt; Sire &lt;xsl:value-of select="concat(ID,' (', Name, ') Stakes: ')"/&gt; &lt;xsl:value-of select="sum(key('kOffspring', ID)/*/Stakes)"/&gt; 3 year old winning offspring: &lt;xsl:value-of select="count(key('kOffspring', ID)[Age = 3 and */Wins &gt; 0])"/&gt; Offspring that ever were a winner: &lt;xsl:value-of select="count(key('kOffspring', ID)[*/Wins &gt; 0])"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When applied on the provided XML</strong> (the provided fragment, enclosed in a single top element to make it a well-formed XML document):</p> <pre><code>&lt;t&gt; &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;Age&gt;3&lt;/Age&gt; &lt;Pace&gt; &lt;Stakes&gt;20&lt;/Stakes&gt; &lt;Wins&gt;0&lt;/Wins&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;101&lt;/SireID&gt; &lt;Age&gt;6&lt;/Age&gt; &lt;Pace&gt; &lt;Stakes&gt;1600&lt;/Stakes&gt; &lt;Wins&gt;9&lt;/Wins&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;101&lt;/SireID&gt; &lt;Age&gt;3&lt;/Age&gt; &lt;Trot&gt; &lt;Stakes&gt;200&lt;/Stakes&gt; &lt;Wins&gt;2&lt;/Wins&gt; &lt;/Trot&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;Age&gt;4&lt;/Age&gt; &lt;Pace&gt; &lt;Stakes&gt;50&lt;/Stakes&gt; &lt;Wins&gt;0&lt;/Wins&gt; &lt;/Pace&gt; &lt;Trot&gt; &lt;Stakes&gt;100&lt;/Stakes&gt; &lt;Wins&gt;1&lt;/Wins&gt; &lt;/Trot&gt; &lt;/Horse&gt; &lt;Horse&gt; &lt;ID&gt;5&lt;/ID&gt; &lt;Name&gt;hrsE&lt;/Name&gt; &lt;SireID&gt;101&lt;/SireID&gt; &lt;Age&gt;3&lt;/Age&gt; &lt;Pace&gt; &lt;Stakes&gt;100&lt;/Stakes&gt; &lt;Wins&gt;1&lt;/Wins&gt; &lt;/Pace&gt; &lt;Trot&gt; &lt;Stakes&gt;300&lt;/Stakes&gt; &lt;Wins&gt;1&lt;/Wins&gt; &lt;/Trot&gt; &lt;/Horse&gt; &lt;/Horses&gt; &lt;Sires&gt; &lt;Sire&gt; &lt;ID&gt;101&lt;/ID&gt; &lt;Name&gt;srA&lt;/Name&gt; &lt;LiveFoalsALL&gt;117&lt;/LiveFoalsALL&gt; &lt;/Sire&gt; &lt;/Sires&gt; &lt;/t&gt; </code></pre> <p><strong>produces the wanted, correct result:</strong></p> <pre><code> Sire 101 (srA) Stakes: 2370 3 year old winning offspring: 2 Offspring that ever were a winner: 4 </code></pre> <hr> <p><strong>Alternatively, a probably more efficient solution is to use another key that is composite and its two parts are the <code>SireID</code> and the <code>Age</code>:</strong></p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:key name="kOffspring" match="Horse" use="SireID"/&gt; &lt;xsl:key name="kOffspringBySireIdAndAge" match="Horse" use="concat(SireID, '+', Age)"/&gt; &lt;xsl:template match="/*"&gt; &lt;xsl:apply-templates select="Sires/Sire"&gt; &lt;xsl:sort select="sum(key('kOffspring', ID)/*/Stakes)" data-type="number" order="descending"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Sire"&gt; Sire &lt;xsl:value-of select="concat(ID,' (', Name, ') Stakes: ')"/&gt; &lt;xsl:value-of select="sum(key('kOffspring', ID)/*/Stakes)"/&gt; 3 year old winning offspring: &lt;xsl:value-of select="count(key('kOffspringBySireIdAndAge', concat(ID, '+3')) [*/Wins &gt; 0] )"/&gt; Offspring that ever were a winner: &lt;xsl:value-of select="count(key('kOffspring', ID)[*/Wins &gt; 0])"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this transformation is applied on the same XML document (above), the same correct result is produced</strong>:</p> <pre><code> Sire 101 (srA) Stakes: 2370 3 year old winning offspring: 2 Offspring that ever were a winner: 4 </code></pre>