<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
<!-- multilevel grouping using the Muench method -->

<!-- Establish a key for each grouping level.  Note that each lower level key value -->
<!-- must include the key value of the higher order key -->
  <xsl:key name="kGenusnaam" match="Boktor" use="Genusnaam"/>
  <xsl:key name="kSoortnaam" match="Boktor" use="concat(Genusnaam,':',Soortnaam)"/>
  <xsl:key name="kVindplaats" match="Boktor" use="concat(Genusnaam,':',Soortnaam,':',Vindplaats)"/>

  <xsl:template match="/">
    <html>
<head>
  <title>Boktorren</title>
  <link rel="stylesheet" type="text/css" href="boktorren.css" title="default" />
</head>
      <body>
        <h1><xsl:value-of select="local-name(/*[1])"/></h1>
        <!-- the outer for-each iterates once for each unique value of Genusnaam -->
        <xsl:for-each select="/*/Boktor[generate-id() = generate-id(key('kGenusnaam', Genusnaam)[1])]">
          <h2 class="Genusnaam"><xsl:value-of select="Genusnaam" /></h2>

          <!-- this for-each iterates over the <Boktor> nodes, once for each unique value of Soortnaam -->
          <!-- that have the same value of Genusnaam as the containing for-each iteration -->
          <xsl:for-each select="key('kGenusnaam', Genusnaam)
                                [generate-id()=
                                  generate-id(key('kSoortnaam',concat(Genusnaam,':',Soortnaam))[1])]">
              <h3 class="Soortnaam">
                <xsl:value-of select="Soortnaam"/>
              </h3>

            <!-- this for-each iterates over the <Boktor> nodes, once for each unique value of Vindplaats -->
            <!-- that have the same value of Genusnaam and Soortnaam as the containing for-each iteration -->
            <xsl:for-each select="key('kSoortnaam', concat(Genusnaam,':',Soortnaam))
                                  [generate-id()=
                                    generate-id(key('kVindplaats',concat(Genusnaam,':',Soortnaam,':',Vindplaats))[1])]">
                <h4>
                  <span class="Vindplaats"><xsl:value-of select="Vindplaats"/></span><br />
                  <span class="AmersX"><xsl:value-of select="AmersX"/></span>
                  <span class="AmersY"><xsl:value-of select="AmersY"/></span><br />
                  <span class="aantal"><xsl:value-of select="aantal"/></span><br />
                  <span class="Leg"><xsl:value-of select="Leg"/></span><br />
                  <span class="dag"><xsl:value-of select="dag"/></span>
                  <span class="maand"><xsl:value-of select="maand"/></span>
                  <span class="jaar"><xsl:value-of select="jaar"/></span><br />
                  <span class="Collectie"><xsl:value-of select="Collectie"/></span>
                 </h4>

        </xsl:for-each>
        </xsl:for-each>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>