I need some help creating an XSLT to group RSS-like data by unique category (sorted alphabetically) and then sort each item by date under each sorted category. (Yes I realize this will produce a Cartesian product).

Here is a trimmed down version of the XML source...

<channel>
    <item>
        <title>test1</title>
        <pubDate>2009-07-16</pubDate>
        <category catid="81">Technology</category>
        <category catid="80">Healthcare</category>
    </item>
    <item>
        <title>test2</title>
        <pubDate>2009-07-12</pubDate>
        <category catid="80">Healthcare</category>
    </item>
    <item>
        <title>test3</title>
        <pubDate>2009-07-14</pubDate>
        <category catid="82">Business</category>
        <category catid="81">Technology</category>
        <category catid="80">Healthcare</category>
    </item>
</channel>

I would like to produce this...

Business

2009-07-14: test3

Healthcare

2009-07-16: test1

2009-07-14: test3

2009-07-12: test2

Technology

2009-07-16: test1

2009-07-14: test3

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text"/>
	<xsl:key name="only" match="//category" use="."/>
	<xsl:template match="/">
		<xsl:apply-templates select="channel"/>
	</xsl:template>
	<xsl:template match="channel">
		<xsl:for-each select="//item/category[generate-id() = generate-id(key('only',.)[1])]">
			<xsl:sort/>
			<xsl:value-of select="concat(.,'&#xA;')"/>
			<xsl:variable name="category" select="text()"/>
			<xsl:for-each select="//category[.=$category]">
			<xsl:sort select="../pubDate"/>
				<xsl:value-of select="concat(../pubDate,' : ',../title,'&#xA;')"/>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.