title: Muenchian grouping, counting number of groups
body: <p>Am using muenchian grouping to group subjets and grades as per students. I can get count for number of subjects opted by each student. But I could not get count of number of students.</p>
  
  <p><strong>Input XML</strong></p>
  
  <pre><code>&lt;data&gt;
    &lt;school&gt;MIT Kindergarden&lt;/school&gt;
    &lt;year&gt;2013&lt;/year&gt;
    &lt;batch&gt;B&lt;/batch&gt;
    &lt;result&gt;
      &lt;student&gt;ABC&lt;/student&gt;
      &lt;id&gt;001&lt;/id&gt;
      &lt;subject&gt;ALG&lt;/subject&gt;
      &lt;grade&gt;C&lt;/grade&gt;
    &lt;/result&gt;
    &lt;result&gt;
      &lt;student&gt;ABC&lt;/student&gt;
      &lt;id&gt;001&lt;/id&gt;
      &lt;subject&gt;HIS&lt;/subject&gt;
      &lt;grade&gt;B&lt;/grade&gt;
    &lt;/result&gt;
    &lt;result&gt;
      &lt;student&gt;XYZ&lt;/student&gt;
      &lt;id&gt;002&lt;/id&gt;
      &lt;subject&gt;ALG&lt;/subject&gt;
      &lt;grade&gt;C&lt;/grade&gt;
    &lt;/result&gt;
    &lt;result&gt;
      &lt;student&gt;XYZ&lt;/student&gt;
      &lt;id&gt;002&lt;/id&gt;
      &lt;subject&gt;ALG&lt;/subject&gt;
      &lt;grade&gt;A&lt;/grade&gt;
    &lt;/result&gt;
  &lt;/data&gt;
  </code></pre>
  
  <p><strong>XSLT</strong> </p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"&gt;
  
    &lt;xsl:output method="xml" indent="yes"/&gt;
  
    &lt;xsl:key name="group-by-id" match="data/result" use="id" /&gt;
  
    &lt;xsl:template match="/"&gt;
      &lt;schoolresult&gt;
        &lt;schoolname&gt;&lt;xsl:value-of select="data/school" /&gt;&lt;/schoolname&gt;
        &lt;resultyear&gt;&lt;xsl:value-of select="data/year" /&gt;&lt;/resultyear&gt;
        &lt;batch&gt;&lt;xsl:value-of select="data/batch" /&gt;&lt;/batch&gt;
        &lt;totalstudent&gt;[Could not find]&lt;/totalstudent&gt;
        &lt;xsl:for-each select="data/result[generate-id() = generate-id(key('group-by-id', id)[1])]"&gt;
          &lt;student&gt;
            &lt;info&gt;
              &lt;name&gt;&lt;xsl:value-of select="student" /&gt;&lt;/name&gt;
              &lt;id&gt;&lt;xsl:value-of select="id" /&gt;&lt;/id&gt;
              &lt;subjectsopted&gt;
                &lt;xsl:value-of select="count(. | key('group-by-id', id))" /&gt;
              &lt;/subjectsopted&gt;
            &lt;/info&gt;
            &lt;xsl:for-each select="key('group-by-id', id)"&gt;
              &lt;subject&gt;
                &lt;name&gt;&lt;xsl:value-of select="subject" /&gt;&lt;/name&gt;
                &lt;grade&gt;&lt;xsl:value-of select="grade" /&gt;&lt;/grade&gt;
              &lt;/subject&gt;
            &lt;/xsl:for-each&gt;
          &lt;/student&gt;
        &lt;/xsl:for-each&gt;
      &lt;/schoolresult&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p><strong>Output XML</strong></p>
  
  <pre><code>&lt;schoolresult&gt;
    &lt;schoolname&gt;MIT Kindergarden&lt;/schoolname&gt;
    &lt;resultyear&gt;2013&lt;/resultyear&gt;
    &lt;batch&gt;B&lt;/batch&gt;
    &lt;studentscount&gt;[Count not find]&lt;/studentscount&gt;
    &lt;student&gt;
      &lt;info&gt;
        &lt;name&gt;ABC&lt;/name&gt;
        &lt;id&gt;001&lt;/id&gt;
        &lt;subjectsopted&gt;2&lt;/subjectsopted&gt;
      &lt;/info&gt;
      &lt;subject&gt;
        &lt;name&gt;ALG&lt;/name&gt;
        &lt;grade&gt;C&lt;/grade&gt;
      &lt;/subject&gt;
      &lt;subject&gt;
        &lt;name&gt;HIS&lt;/name&gt;
        &lt;grade&gt;B&lt;/grade&gt;
      &lt;/subject&gt;
    &lt;/student&gt;
    &lt;student&gt;
      &lt;info&gt;
        &lt;name&gt;XYZ&lt;/name&gt;
        &lt;id&gt;002&lt;/id&gt;
        &lt;subjectsopted&gt;2&lt;/subjectsopted&gt;
      &lt;/info&gt;
      &lt;subject&gt;
        &lt;name&gt;ALG&lt;/name&gt;
        &lt;grade&gt;C&lt;/grade&gt;
      &lt;/subject&gt;
      &lt;subject&gt;
        &lt;name&gt;ALG&lt;/name&gt;
        &lt;grade&gt;A&lt;/grade&gt;
      &lt;/subject&gt;
    &lt;/student&gt;
  &lt;/schoolresult&gt;
  </code></pre>