title: How to group data with xsl:for-each with XSL 1.0
body: <p>I am trying to get multiple html documents for every User in an XML document using XSLT based on the answer in: <a href="https://stackoverflow.com/questions/2146648/how-to-apply-group-by-on-xslt-elements">how to apply group by on xslt elements</a> however without succes.</p>
  
  <p>XML:</p>
  
  <pre><code>&lt;root&gt;
    &lt;ArrayOfResult&gt;
      &lt;Result&gt;
        &lt;Men&gt;
          &lt;BowlerResult&gt;
            &lt;Person&gt;
              &lt;Name&gt;name 1&lt;/Name&gt;
            &lt;/Person&gt;
            &lt;Data&gt;
              &lt;Score1&gt;1&lt;/Score1&gt;
              &lt;Score2&gt;1&lt;/Score2&gt;
              &lt;Score3&gt;1&lt;/Score3&gt;
            &lt;/Data&gt;
          &lt;/BowlerResult&gt;
          &lt;BowlerResult&gt;
            &lt;Person&gt;
              &lt;Name&gt;name 2&lt;/Name&gt;
            &lt;/Person&gt;
            &lt;Data&gt;
              &lt;Score1&gt;2&lt;/Score1&gt;
              &lt;Score2&gt;2&lt;/Score2&gt;
              &lt;Score3&gt;2&lt;/Score3&gt;
            &lt;/Data&gt;
          &lt;/BowlerResult&gt;
        &lt;/Men&gt;
        &lt;Women&gt;
          &lt;BowlerResult&gt;
            &lt;Person&gt;
              &lt;Name&gt;name 3&lt;/Name&gt;
            &lt;/Person&gt;
            &lt;Data&gt;
              &lt;Score1&gt;3&lt;/Score1&gt;
              &lt;Score2&gt;3&lt;/Score2&gt;
              &lt;Score3&gt;3&lt;/Score3&gt;
            &lt;/Data&gt;
          &lt;/BowlerResult&gt;
        &lt;/Women&gt;
      &lt;/Result&gt;
      &lt;Result&gt;...&lt;/Result&gt;
    &lt;/ArrayOfResult&gt;
    &lt;ArrayOfResult&gt;...&lt;/ArrayOfResult&gt;
  </code></pre>
  
  <p>Person can be in more Leagues</p>
  
  <p>What I want to achieve:
  for every Person (distinct on Person/Name) i want to accumulate data from all 'BowlerResult' elements</p>
  
  <p>The XSLT I have right now:</p>
  
  <p>
      </p>
  
  <pre><code>&lt;xsl:key name="keyPerson" match="BowlerResult" use="Person/Name" /&gt;
  
  &lt;xsl:template match="text()" /&gt;
  
  &lt;xsl:template match="/root"&gt;
    &lt;root&gt;
      &lt;xsl:apply-templates /&gt;
    &lt;/root&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template
   match="BowlerResult[generate-id(.)=generate-id(key('keyPerson',Person/Name)[1])]"&gt;
    &lt;Person value="{Person/Name}"&gt;
      &lt;xsl:for-each select="key('keyPerson', Person/Name)"&gt;
        &lt;Result&gt;
          &lt;id&gt;
            &lt;xsl:value-of select="Person/Name" /&gt;
          &lt;/id&gt;
        &lt;/Result&gt;
      &lt;/xsl:for-each&gt;
    &lt;/Person&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>
  But this only gives me the an iteration of the first Person.</p>