1 <?xml version="1.0" ?>
   2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   3 <xsl:output method="xml" indent="yes"/>
   4 <xsl:template match="/">
   5 <xsl:variable name="unique-countries" select="/cities/city[not(@country=preceding-sibling::city/@country)]/@country"/>
   6     <countries>
   7         <xsl:for-each select="$unique-countries">
   8           <country name="{.}">
   9                 <xsl:for-each select="//city[@country=current()]">
  10                   <city><xsl:value-of select="@name"/></city>
  11                 </xsl:for-each>
  12           </country> 
  13         </xsl:for-each>
  14     </countries>
  15 </xsl:template>
  16 </xsl:stylesheet>
  17