Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old September 4th, 2006, 04:39 AM
Authorized User
Join Date: Sep 2006
Location: Cologne, , Germany.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

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&gt;='200602' and mnth&lt;='200603')]" use="team"/>

  <xsl:template match="root">
    <table border="1">
      <tr><th colspan="4"> Team Overview <xsl:value-of select="team"/></th></tr>

      <xsl:for-each select="xmlout[(generate-id()=generate-id(key('kteam',team)[1]))]">
          <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>
   <br />

XML ... simplified version of the data

<?xml-stylesheet href="team.xsl" type="text/xsl"?>
<?xml version="1.0" encoding="UTF-8"?>

Old September 4th, 2006, 07:14 AM
Authorized User
Join Date: Sep 2006
Location: Cologne, , Germany.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts

Resetting keys ...

I was wondering that maybe what I need to solve this problem is the ability to reset or refilter a key?

For example my first version the following unfiltered key works:

<xsl:key name="kteam" match="xmlout" use="team"/>

Then in the filtered version this also works:

<xsl:key name="kteam" match="xmlout[(mnth&gt;='200602' and mnth&lt;='200603')]" use="team"/>

... i.e. a filter is applied to the dataset 'xmlout'.

But then, when I try to add parameters, it falls over:

<xsl:key name="kteam" match="xmlout[(mnth&gt;=$monthfrom and mnth&lt;=$monthuntil)]" use="team"/>

Here I was wondering if there is any way to redefine a key at any other point in the XSL? My plan would be to declare the data as 'xmlout' but then, just before display, redeclare the key with the required filter?

What do you think? Am I on the right track?


Old September 4th, 2006, 07:22 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

No, you can't use keys in this way. You have to define all the dynamic conditions at the time you use the key (e.g. by applying a predicate to the result: key('kteam', $team)[mnth >= x and mnth <= y].

Michael Kay
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Old September 4th, 2006, 09:01 AM
Authorized User
Join Date: Sep 2006
Location: Cologne, , Germany.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts

Hi Michael,

Fantastic: Your snippet of syntax got me moving and now the grouping/summing is displaying correctly with the filter.

Many thanks,
Alan Searle

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use OR during filtering..... raaj81 Reporting Services 0 July 30th, 2008 05:34 AM
Parameter object malfunction - out parameter dash dev C# 2005 6 December 4th, 2007 12:58 PM
Filtering Content tommyready XSLT 3 September 11th, 2007 02:54 AM
Protect cells and allow grouping/un-grouping sfreuden Excel VBA 4 December 14th, 2006 08:01 AM
XSL Grouping and Filtering xrow XSLT 1 October 13th, 2004 11:12 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.