RE: [xsl] Multiple Grouping & Muenchian Method

Subject: RE: [xsl] Multiple Grouping & Muenchian Method
From: "Jacoby, Peter R." <PJACOBY@xxxxxxxxxxxx>
Date: Fri, 21 Jul 2006 15:17:17 -0400
>>Is it possible to have a group within a group.

Hi, Steve,

You were really close in your code.  The concept that you were missing is to
define one xsl:key with two parameters using the concat function.

If you change your key declaration to be
<xsl:key name="byDateAndCountry" match="entry"
use="concat(parent::group/parent::date/text(), country)" />
or more succinctly:
<xsl:key name="byDateAndCountry" match="entry" use="concat(../../text(),
country)" />

You can then call one key indexed by an entry's date as well as country.

A full stylesheet is below.  Please let us know if you have any other
questions.
This all becomes a lot easier in 2.0 with xsl:for-each-group.  Hopefully
someone
will post the 2.0 solution so we can see how much simpler it is.

-Peter

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" omit-xml-declaration="no" indent="yes"
encoding="UTF-8"/>

<xsl:key name="byDateAndCountry" match="entry" use="concat(../../text(),
country)" />

<xsl:template match="/">
	<table border="1">
		<xsl:apply-templates select="report/table/date"/>
	</table>
</xsl:template>

<xsl:template match="date">
	<tr>
		<td>
			<xsl:value-of select="normalize-space(text())" />
		</td>
	</tr>

	<xsl:for-each select="group/entry[generate-id() =
generate-id(key('byDateAndCountry', concat(../../text(), country))[1])]">
		<tr>
			<td>
				<xsl:value-of select="country" />
			</td>
		</tr>
		<tr>
			<td>Group</td>
			<td>Name</td>
			<td>ID</td>
		</tr>
		<xsl:apply-templates select="key('byDateAndCountry',
concat(../../text(), country))" />
	</xsl:for-each>
</xsl:template>

<xsl:template match="entry">
	<tr>
		<td>
			<xsl:value-of select="../@type" />
		</td>
		<td>
			<xsl:value-of select="name" />
		</td>
		<td>
			<xsl:value-of select="id" />
		</td>
	</tr>
</xsl:template>

</xsl:stylesheet>

Current Thread