[xsl] grouping question

Subject: [xsl] grouping question
From: "Blake, Stephen" <Stephen.Blake@xxxxxxxxxxxx>
Date: Fri, 23 Mar 2001 15:38:04 -0500
Hello all,

I am having trouble with a grouping problem. I've checked a book and the faq
and I'm having a hard time figuring out how to do it. We have an input
source like this:

<domain>
	<host name="foo">
		<service name="svc1"/>
		<service name="svc2"/>
		<service name="svc3"/>
	</host>
	<host name="bar">
		<service name="svc2"/>
		<service name="svc4"/>
	</host>
	<host name="baz">
		<service name="svc1"/>
		<service name="svc4/>
		<service name="svc7"/>
	</host>
</domain>

We want to find all unique services, then iterate over them finding each
host that is running that service, generating somthing like this:

<p>Hosts running svc1:</p>
<ul>
	<li>foo</li>
	<li>baz</li>
</ul>
<p>Hosts running svc2:</p>
<ul>
	<li>foo</li>
	<li>bar</li>
</ul>
<p>Hosts running svc3:</p>
<ul>
	<li>foo</li>
</ul>
<p>Hosts running svc4:</p>
<ul>
	<li>bar</li>
	<li>baz</li>
</ul>
<p>Hosts running svc7</p>
<ul>
	<li>baz</li>
</ul>

So, looking through Steve Muench's excellent book, I see that Chapter 9 has
a very similar example, taking all unique developers and which bugs they
have assigned to them. I tried to modify his example, but the snag is that I
have multiple <service>s inside each <host> whereas in the example in the
book, there is only one <dev>eloper for each <bug>. Hmmm. The hardest thing
is finding the unique services, iterating over the hosts at that point I can
do using a key index.

Here's what I've tried so far:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:output indent="yes"/>
 
<xsl:key name="host-service-index" match="/domain/host" use="service"/>
<xsl:template match="/">
<xsl:for-each
select="/domain/host/service[generate-id(.)=generate-id(key('host-service-in
dex',.)[1]/service)]">
	<xsl:sort select="@name"/>
	<p>
	<b><xsl:value-of select="@name"/></b>
	<ul>
		<xsl:for-each select="key('host-service-index',.)">
			<li><xsl:value-of select="@name"/></li>
		</xsl:for-each>
	</ul>
	</p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

But I only get one service like so:

<p>
	<b>svc1</b>
	<ul>
		<li>foo</li>
		<li>baz</li>
	</ul>
</p>

Can anyone help?

Thanks for your consideration.

Stephen Blake
Veritect
s t e p h e n . b l a k e @ v e r i t e c t . c o m

P.S. I'm using MS XML parser v 3.0, but I get the same results with
Infoteria iXSLT 2.0c and Instant-Saxon 6.0.2.


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread