Grouping and Filtering: With parameter variables?
Dear All ...
I thought that I had solved my Grouping/Filtering problem with the following XSL (see below) ... but it wasn't as easy as I believed:
In my example, I use a filter on the 'match' clause of the 'key' statement to limit the data to two months (here February and March 2006): mnth>='200602' and mnth<='200603' (see XSL below).
This filter works OK and the key returns exactly the data that I need:
1. A count of the number of teams: count(key('kteam',team)/team)
2. Sum of total sales by team: sum(key('kteam',team)/sales)
... for the specific period Feb to Mar.
But I found that, when I tried to integrate this into my live version, the XML/XSL parser refused to process the 'key' statement with the variables I was trying to pass ($monthfrom and $monthuntil). Here in my test version the filtering works because I declare literal values (e.g. '200602') but in my live version I need to pass filter variables (i.e. any combination of months) and that doesn't work (on the key statement).
I hope that someone can take a quick look at my solution and suggest a method to apply variables to the filter? Indeed, I tried applying the filter to the 'for-each select=' clause (along with the generate-id() statement) but this returned incorrect data (i.e. didn't filter correctly). Maybe there is another location (for the filter) or an alternative syntax that I could use?
Many thanks for any tips that you can give me.
Regards,
Alan Searle
PS: And this XSL might also be an interesting example for any newbies out there who are trying to understand principles of grouping and filtering.
-------------------------------
XSL ... Filter on mnth, count and sum on team
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kteam" match="xmlout[(mnth>='200602' and mnth<='200603')]" use="team"/>
<xsl:template match="root">
<table border="1">
<tr><th colspan="4"> Team Overview <xsl:value-of select="team"/></th></tr>
<tr><th>Team</th><th>Count</th><th>sales</th><th>id</th></tr>
<xsl:for-each select="xmlout[(generate-id()=generate-id(key('kteam',team)[1]))]">
<tr>
<td><xsl:value-of select="team"/></td>
<td><xsl:value-of select="count(key('kteam',team)/team)"/></td>
<td><xsl:value-of select="sum(key('kteam',team)/sales)"/></td>
<td><xsl:value-of select="generate-id(.)"/></td>
</tr>
</xsl:for-each>
</table>
<br />
</xsl:template>
</xsl:stylesheet>
-----------------------------------
XML ... simplified version of the data
<?xml-stylesheet href="team.xsl" type="text/xsl"?>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<xmlout><mnth>200601</mnth><team>A</team><sales>23</sales>
</xmlout><xmlout>
<mnth>200601</mnth><team>B</team><sales>33</sales>
</xmlout><xmlout>
<mnth>200601</mnth><team>C</team><sales>98</sales>
</xmlout><xmlout>
<mnth>200602</mnth><team>A</team><sales>14</sales>
</xmlout><xmlout>
<mnth>200602</mnth><team>B</team><sales>65</sales>
</xmlout><xmlout>
<mnth>200602</mnth><team>C</team><sales>76</sales>
</xmlout><xmlout>
<mnth>200603</mnth><team>A</team><sales>34</sales>
</xmlout><xmlout>
<mnth>200603</mnth><team>B</team><sales></sales>
</xmlout><xmlout>
<mnth>200603</mnth><team>C</team><sales>56</sales>
</xmlout>
</root>
|