title: Muenchian group a selection of elements in XSLT 1.0
body: <p>Consider this XML document of football games with a title, category and league.</p>
  
  <pre><code>&lt;?xml version='1.0'?&gt;
  &lt;games&gt;
      &lt;game&gt;
          &lt;title&gt;Manchester City - Arsenal&lt;/title&gt;
          &lt;category&gt;Live&lt;/category&gt;
          &lt;league&gt;Premier League&lt;/league&gt;
      &lt;/game&gt;
      &lt;game&gt;
          &lt;title&gt;Barcelona - Real Madrid&lt;/title&gt;
          &lt;category&gt;Live&lt;/category&gt;
          &lt;league&gt;Primera Division&lt;/league&gt;
      &lt;/game&gt;
      &lt;game&gt;
          &lt;title&gt;Arsenal - Hull City&lt;/title&gt;
          &lt;category&gt;Recap&lt;/category&gt;
          &lt;league&gt;Premier League&lt;/league&gt;
      &lt;/game&gt;
      &lt;game&gt;
          &lt;title&gt;Everton - Liverpool&lt;/title&gt;
          &lt;category&gt;Live&lt;/category&gt;
          &lt;league&gt;Premier League&lt;/league&gt;
      &lt;/game&gt;
      &lt;game&gt;
          &lt;title&gt;Zaragoza - Deportivo&lt;/title&gt;
          &lt;category&gt;Short Recap&lt;/category&gt;
          &lt;league&gt;Primera Division&lt;/league&gt;
      &lt;/game&gt;
  &lt;/games&gt;
  </code></pre>
  
  <p>I'm trying to group these games by <strong>category</strong>, but I only want to retain the records for which the <code>&lt;league&gt;</code> element is 'Premier League'. The category should also have a count attribute which lists the number of corresponding records.</p>
  
  <p>Following XSL-file will work, but has the disadvantage that it will also list categories for which no 'Premier League' games are found. Ideally there shouldn't be a category tag for those at all.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
      &lt;xsl:output method="xml" encoding="UTF-8" indent="yes" /&gt;
  
   &lt;xsl:key name="prod-cat" match="game" use="category"/&gt;
  
      &lt;xsl:template match="/"&gt;
          &lt;root&gt;
              &lt;xsl:for-each select="games/game[count(. | key('prod-cat', category)[1]) = 1]"&gt;
  
                  &lt;category&gt;
  
                  &lt;xsl:variable name="cat"&gt;
                      &lt;xsl:value-of select="category"/&gt;
                  &lt;/xsl:variable&gt;
  
                  &lt;xsl:variable name="count"&gt;
                      &lt;xsl:value-of select="count(//game[category = $cat][league = 'Premier League'])"/&gt;
                  &lt;/xsl:variable&gt;
  
                  &lt;xsl:attribute name="name"&gt;
                      &lt;xsl:value-of select="category"/&gt;
                  &lt;/xsl:attribute&gt;
  
                  &lt;xsl:attribute name="count"&gt;
                      &lt;xsl:value-of select="$count"/&gt;
                  &lt;/xsl:attribute&gt;
  
                      &lt;xsl:for-each select="key('prod-cat', $cat)[league = 'Premier League']"&gt;
                          &lt;game&gt;
                              &lt;title&gt;
                                  &lt;xsl:value-of select="title"/&gt;
                              &lt;/title&gt;
                              &lt;cat&gt;
                                  &lt;xsl:value-of select="category"/&gt;
                              &lt;/cat&gt;
                              &lt;series&gt;
                                  &lt;xsl:value-of select="league"/&gt;
                              &lt;/series&gt;
                          &lt;/game&gt;
                      &lt;/xsl:for-each&gt;
                  &lt;/category&gt;
              &lt;/xsl:for-each&gt;
          &lt;/root&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:transform&gt;
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;root&gt;
     &lt;category name="Live" count="2"&gt;
        &lt;game&gt;
           &lt;title&gt;Manchester City - Arsenal&lt;/title&gt;
           &lt;cat&gt;Live&lt;/cat&gt;
           &lt;series&gt;Premier League&lt;/series&gt;
        &lt;/game&gt;
        &lt;game&gt;
           &lt;title&gt;Everton - Liverpool&lt;/title&gt;
           &lt;cat&gt;Live&lt;/cat&gt;
           &lt;series&gt;Premier League&lt;/series&gt;
        &lt;/game&gt;
     &lt;/category&gt;
     &lt;category name="Recap" count="1"&gt;
        &lt;game&gt;
           &lt;title&gt;Arsenal - Hull City&lt;/title&gt;
           &lt;cat&gt;Recap&lt;/cat&gt;
           &lt;series&gt;Premier League&lt;/series&gt;
        &lt;/game&gt;
     &lt;/category&gt;
     &lt;category name="Short Recap" count="0"/&gt; &lt;!--This one needs to go--&gt;
  &lt;/root&gt;
  </code></pre>