XSLT 1.0 Group By

Muenchian grouping is the best approach; something like:

<?xml version="1.0" encoding="utf-8"?>
<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">

  <xsl:output method="xml" indent="yes"/>      
  <xsl:key name="groups" match="/results/result" use="Store" />

  <xsl:template match="/results">
    <xsl:apply-templates select="result[generate-id() = generate-id(key('groups', Store)[1])]"/>
  </xsl:template>
  <xsl:template match="result">
    <h1><xsl:value-of select="Store"/></h1>
    <table id="{Store}">
      <tr class="heading">
        <th scope="col">Member Id</th>
        <th scope="col">First Name</th>
        <th scope="col">Last Name</th>
      </tr>
      <xsl:for-each select="key('groups', Store)">
        <tr>
          <td><xsl:value-of select="MemberLogin"/></td>
          <td><xsl:value-of select="MemberFirstName"/></td>
          <td><xsl:value-of select="MemberLastName"/></td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

Leave a Comment