title: Count the frequency of specific nodes in an XML document
body: <p>This may have been covered before, but after much googling, I can't seem to find any examples.  Essentially I'm trying to get a frequency count of the number of time a book is taken out on loan.  My XML tree is given below, along with the XSL and resulting output.</p>
  
  <pre><code>    &lt;?xml-stylesheet type="text/xsl" href="LoanStyler.xsl"?&gt;
  &lt;loans&gt;
    &lt;loan id="0001"&gt;
      &lt;summary&gt;
        &lt;user&gt;AAA&lt;/user&gt;
        &lt;dateOut&gt;2011-01-01&lt;/dateOut&gt;
        &lt;dateDue&gt;2011-01-14&lt;/dateDue&gt;
      &lt;/summary&gt;
      &lt;details&gt;
        &lt;books&gt;
          &lt;name&gt;Book7&lt;/name&gt;
          &lt;name&gt;Book4&lt;/name&gt;
        &lt;/books&gt;
      &lt;/details&gt;
    &lt;/loan&gt;
    &lt;loan id="0002"&gt;
      &lt;summary&gt;
        &lt;user&gt;BBB&lt;/user&gt;
        &lt;dateOut&gt;2011-01-10&lt;/dateOut&gt;
        &lt;dateDue&gt;2011-01-24&lt;/dateDue&gt;
      &lt;/summary&gt;
      &lt;details&gt;
        &lt;books&gt;
          &lt;name&gt;Book1&lt;/name&gt;
          &lt;name&gt;Book2&lt;/name&gt;
          &lt;name&gt;Book4&lt;/name&gt;
          &lt;name&gt;Book6&lt;/name&gt;
        &lt;/books&gt;
      &lt;/details&gt;
    &lt;/loan&gt;
    &lt;loan id="0003"&gt;
      &lt;summary&gt;
        &lt;user&gt;CCC&lt;/user&gt;
        &lt;dateOut&gt;2011-01-14&lt;/dateOut&gt;
        &lt;dateDue&gt;2011-01-28&lt;/dateDue&gt;
      &lt;/summary&gt;
      &lt;details&gt;
        &lt;books&gt;
          &lt;name&gt;Book1&lt;/name&gt;
          &lt;name&gt;Book3&lt;/name&gt;
          &lt;name&gt;Book4&lt;/name&gt;
          &lt;name&gt;Book7&lt;/name&gt;
          &lt;name&gt;Book8&lt;/name&gt;
        &lt;/books&gt;
      &lt;/details&gt;
    &lt;/loan&gt;
    &lt;loan id="0004"&gt;
      &lt;summary&gt;
        &lt;user&gt;DDD&lt;/user&gt;
        &lt;dateOut&gt;2011-02-01&lt;/dateOut&gt;
        &lt;dateDue&gt;2011-02-14&lt;/dateDue&gt;
      &lt;/summary&gt;
      &lt;details&gt;
        &lt;books&gt;
          &lt;name&gt;Book1&lt;/name&gt;
          &lt;name&gt;Book2&lt;/name&gt;
          &lt;name&gt;Book4&lt;/name&gt;
        &lt;/books&gt;
      &lt;/details&gt;
    &lt;/loan&gt;
  &lt;/loans&gt;
  
      &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
  &lt;xsl:key 
      name="books-by-name" 
      match="//loans/loan/details/books" 
      use="name" 
    /&gt;
  
  
  &lt;xsl:template match="/"&gt;
    &lt;html&gt;
    &lt;body&gt;
      &lt;h1&gt;Loan Records Log&lt;/h1&gt;
      &lt;table Border="1"&gt;
          &lt;tr&gt;
              &lt;th&gt;Loan ID&lt;/th&gt;
              &lt;th&gt;User&lt;/th&gt;
              &lt;th&gt;Date Out&lt;/th&gt;
              &lt;th&gt;Date Due&lt;/th&gt;
          &lt;/tr&gt;
          &lt;xsl:for-each select="//loans/loan"&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;xsl:value-of select="@id"/&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;xsl:value-of select="summary/user"/&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;xsl:value-of select="summary/dateOut"/&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;xsl:value-of select="summary/dateDue"/&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/xsl:for-each&gt;
      &lt;/table&gt;
  
      &lt;table Border="1"&gt;
          &lt;tr&gt;
              &lt;th&gt;Book name&lt;/th&gt;
              &lt;th&gt;Count&lt;/th&gt;
          &lt;/tr&gt;
  
          &lt;xsl:for-each select="books[count(. | key('books-by-name', name)[1]) = 1]"&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;xsl:value-of select="name" /&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;xsl:value-of select="count(key('books-by-name', name))" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/xsl:for-each&gt;
      &lt;/table&gt;
    &lt;/body&gt;
    &lt;/html&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  
  Loan Records Log
  Loan ID User    Date Out    Date Due
  0001    AAA 2011-01-01  2011-01-14
  0002    BBB 2011-01-10  2011-01-24
  0003    CCC 2011-01-14  2011-01-28
  0004    DDD 2011-02-01  2011-02-14
  Book name   Count
  </code></pre>
  
  <p>As you can see the attempt at Muenchian Grouping doesn't seem to be working.  Could someone please explain what I'm doing wrong, as the only examples I can find don't appear to have helped.</p>