[xsl] 2 level Grouping through attributes

Subject: [xsl] 2 level Grouping through attributes
From: "Yoav Broudo" <yoav@xxxxxxxxx>
Date: Mon, 11 Feb 2002 09:35:31 +0200
Hi,

I am trying to achieve multi level grouping (actually two levels) of the
next XML.

The first group is of the user and the second one is of the Categories that
belong to that user.

In each category I want to list all ContentItem from that category.


How can I achieve this?

Thanks in advance

Yoav


The XML file is:

<ROOT>
<XML_OUTPUT ID="1" UserID="3" UserName="Mark" CategoryID="1" CategoryTitle="Category A" ContentItemID="1" ItemTitle="Title 1"/>
<XML_OUTPUT ID="2" UserID="3" UserName="Mark" CategoryID="5" CategoryTitle="Category B" ContentItemID="45" ItemTitle="Title 45"/>
<XML_OUTPUT ID="3" UserID="3" UserName="Mark" CategoryID="5" CategoryTitle="Category B" ContentItemID="10" ItemTitle="Title 10"/>
<XML_OUTPUT ID="4" UserID="3" UserName="Mark" CategoryID="5" CategoryTitle="Category B" ContentItemID="11" ItemTitle="Title 11"/>
<XML_OUTPUT ID="5" UserID="3" UserName="Mark" CategoryID="5" CategoryTitle="Category B" ContentItemID="12" ItemTitle="Title 12"/>
<XML_OUTPUT ID="10" UserID="4" UserName="Joe" CategoryID="6" CategoryTitle="Category C" ContentItemID="16" ItemTitle="Title 16"/>
<XML_OUTPUT ID="11" UserID="4" UserName="Joe" CategoryID="6" CategoryTitle="Category C" ContentItemID="13" ItemTitle="Title 13"/>
<XML_OUTPUT ID="12" UserID="4" UserName="Joe" CategoryID="6" CategoryTitle="Category C" ContentItemID="15" ItemTitle="Title 15"/>
<XML_OUTPUT ID="13" UserID="4" UserName="Joe" CategoryID="10" CategoryTitle="Category E" ContentItemID="18" ItemTitle="Title 18"/>
<XML_OUTPUT ID="14" UserID="4" UserName="Joe" CategoryID="10" CategoryTitle="Category E" ContentItemID="19" ItemTitle="Title 19"/>
</ROOT>

The result as I want it to look like:

<ROOT>
<USER ID="3" NAME="MARK">
	<CATEGORY ID="1" TITLE="CATEGORY A">
		<CONTENT_ITEM ID="1" TITLE="TITLE1"/>
	</CATEGORY>
	<CATEGORY ID="5" TITLE="CATEGORY B">
		<CONTENT_ITEM ID="45" TITLE="TITLE 45"/>
		<CONTENT_ITEM ID="10" TITLE="TITLE 10"/>
		<CONTENT_ITEM ID="11" TITLE="TITLE 11"/>
		<CONTENT_ITEM ID="12" TITLE="TITLE 12"/>
	</CATEGORY>
</USER>
<USER ID="4" NAME="JOE">
	<CATEGORY ID="6" TITLE="CATEGORY C">
		<CONTENT_ITEM ID="16" TITLE="TITLE 16"/>
		<CONTENT_ITEM ID="13" TITLE="TITLE 13"/>
		<CONTENT_ITEM ID="15" TITLE="TITLE 15"/>
	</CATEGORY>
	<CATEGORY ID="10" TITLE="CATEGORY E">
		<CONTENT_ITEM ID="18" TITLE="TITLE 18"/>
		<CONTENT_ITEM ID="19" TITLE="TITLE 19"/>
	</CATEGORY>
</USER>
</ROOT>


The XSL that I am using to achieve the first level grouping is :
I saw that I could achieve two level of grouping with KEYs but I'm not sure how to achieve it.

<?xml version='1.0'?>

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

<xsl:template match="/">

<xsl:variable name="unique-USER"
select="/ROOT/XML_OUTPUT[not(@UserID=preceding-sibling::XML_OUTPUT/@UserID)]
/@UserID" />
<xsl:element name="DATA">
<xsl:for-each select="$unique-USER">
<USER USER_ID="{.}">
<xsl:for-each select="//XML_OUTPUT[@UserID=current()]">
<xsl:element name="CATEGORY_TITLE">
<xsl:attribute name="ID" >
<xsl:value-of select="@CategoryID"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</USER>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>


Thanks in advance,
Yoav 

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


Current Thread