<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

<xsl:output method="xhtml" indent="yes"/>

<xsl:template match="/">
  <html>
    <head>
      <title>Grouping By Key</title>
    </head>
    <body>
      <xsl:apply-templates/>
    </body>
  </html>
</xsl:template>

<xsl:template match="cities">
  <table>
    <tr>
      <th>Position</th>
      <th>Country</th>
      <th>City List</th>
    </tr>

<xsl:for-each-group select="city" group-by="@country">
  <tr>
   <td><xsl:value-of select="position()"/></td>
   <td><xsl:value-of select="@country"/></td>
   <td>
    <xsl:value-of select="current-group()/@name" separator=", "/>
   </td>
  </tr>
</xsl:for-each-group>

  </table>
</xsl:template>

</xsl:stylesheet>