[xsl] Grouping and sorting data

Subject: [xsl] Grouping and sorting data
From: Verakso <verakso@xxxxxxxxx>
Date: Wed, 24 Nov 2004 13:36:23 +0100
Is there some great FAQ about grouping and sorting flat XML data?

Everytime I seem to get at grip of it, I still mess it up

I have a huge XML file with records that looks like this:
<csv_data_records>
<record num="1">
          <Country>Denmark</Country>
          <City>Copenhagen</City>
          <CompanyFull>Company Name</CompanyFull>
          <adr1>Street Address</adr1>
          <adr2></adr2>
          <adr3></adr3>
          <adr4></adr4>
          <PostalCode>1070</PostalCode>
          <ZipCode>1070</ZipCode>
          <MainPhone>+45 5555 55555</MainPhone>
          <MainFax>+45 6666 66666</MainFax>
          <CompanyType>Branch</CompanyType>
          <DepartmentTitle>Management/Client Manager</DepartmentTitle>
          <LastName>Nielsen</LastName>
          <FirstName>Thomas</FirstName>
          <Direct>+45 77777 7777</Direct>
          <Home></Home>
          <MobileHoliday></MobileHoliday>
          <Fax>+45 8888 8888</Fax>
     </record>
.
.
<csv_data_records>

I would like to group and sort, first on Country, second on City and
third on CompanyFullName.

I have tries keys and various approaches, but I haven't had any succes
with the secon and third grouping.

The stylesheet looks like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>
	<xsl:key name="kCountry" match="record" use="Country"/>
	<xsl:template match="*"/>
	<xsl:template match="csv_data_records">
		<test>
			<xsl:apply-templates
select="record[generate-id(.)=generate-id(key('kCountry', Country))]">
				<xsl:sort select="Country"/>
			</xsl:apply-templates>
		</test>
	</xsl:template>
	<xsl:template match="record">
		<country>
			<xsl:value-of select="Country"/>
		</country>
	</xsl:template>
</xsl:stylesheet>

I just have a problem getting back and selecting the City's to the
corresponding Country, and then next the grouping of the Companies.

What have I missed.

/Thomas

Current Thread