I am having trouble grouping using xslt 1.0
Below is a sample stylesheet and xml file. My end result would be a list of distinct "@Keyword" for a given Store and the sum of their "@Count"
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="distinct-keywords" match="Klog" use="@Keyword"></xsl:key>
<xsl:template match="/">
<div>
<table width="100%" cellpadding="4" cellspacing="4" border="0">
<tr>
<td colspan="3"><h3><xsl:value-of select="doc/ReportName"/></h3></td></tr>
<tr><td colspan="3"><h5><xsl:value-of select="doc/StartDate"/> -
<xsl:value-of select="doc/EndDate"/></h5></td>
</tr>
<xsl:for-each select="doc/StatsReport/Store">
<tr valign="top" align="left">
<td>
<br></br>
<h5>
Store: <xsl:value-of select="@Number"/>
</h5>
</td>
<td/>
<td/>
</tr>
<xsl:for-each select=".//Klog[generate-id(.) = generate-id(key('distinct-keywords', .//@Keyword)[1])]">
<tr valign="top" align="left" >
<td>
<xsl:value-of select="@Keyword"/>
</td>
<td>
<xsl:value-of select="sum(@Count)"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</div>
</xsl:template>
</xsl:stylesheet>
XML Below:
<?xml version="1.0"?>
<doc>
<ReportName>SampleReport</ReportName>
<StartDate>Sep 12 2007 12:00AM</StartDate>
<EndDate>Sep 17 2007 11:59PM</EndDate>
<StatsReport>
<Store Number="ST101">
<ID ID="ST1010">
<Klog Keyword=" Key 1" SubKeyword="" Count="1"/>
<Klog Keyword=" Key 1" SubKeyword="" Count="1"/>
<Klog Keyword=" Key 2" SubKeyword="" Count="2"/>
<Klog Keyword=" Key 1" SubKeyword="" Count="1"/>
<Klog Keyword=" Key 2" SubKeyword="" Count="1"/>
<Klog Keyword=" Key 1" SubKeyword="" Count="3"/>
</ID>
<ID ID="ST1011">
<Klog Keyword=" Key 4" SubKeyword="" Count="1"/>
<Klog Keyword=" Key 1" SubKeyword="" Count="1"/>
<Klog Keyword=" Key 2" SubKeyword="" Count="2"/>
<Klog Keyword=" Key 1" SubKeyword="" Count="1"/>
<Klog Keyword=" Key 2" SubKeyword="" Count="1"/>
<Klog Keyword=" Key 1" SubKeyword="" Count="3"/>
</ID>
<ID ID="ST1012">
<Klog Keyword=" Key 1" SubKeyword="" Count="1"/>
<Klog Keyword=" Key 3" SubKeyword="" Count="1"/>
<Klog Keyword=" Key 2" SubKeyword="" Count="2"/>
<Klog Keyword=" Key 1" SubKeyword="" Count="1"/>
<Klog Keyword=" Key 2" SubKeyword="" Count="1"/>
<Klog Keyword=" Key 1" SubKeyword="" Count="3"/>
</ID>
</Store>
</StatsReport>
</doc>
Here is the output I am looking for:
Store Number : ST101
Key 1 16
Key 2 9
Key 3 1
Key 4 1
Here is what I am getting :
Key 1 6
Key 2 3
Key 3 1
Key 4 1
I always get the correct list of Keywords but the sum is only for one ID. Anyone have any ideas? Any help is appreciated