RE: [xsl] Grouping the same set by multiple criteria

Subject: RE: [xsl] Grouping the same set by multiple criteria
From: Américo Albuquerque <aalbuquerque@xxxxxxxxxxxxxxxx>
Date: Wed, 28 May 2003 19:05:00 +0100
Hi

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of 
> Erika Marlow
> Sent: Wednesday, May 28, 2003 3:49 PM
> To: XSL List
> Subject: [xsl] Grouping the same set by multiple criteria
> 
> 
> Hello.  I'm trying to group a set of transactions by date 
> then site then payer. Then I have to count how many are in 
> each group, i.e., how many total transactions by date, how 
> many in a date group are from a specific site, and how many 
> in a date/site group are the same payer.  I have used the 
> Muenchian technique to find the unique groups, but I am stuck 
> on how to get the counts for the different groupings.  I can 
> find how many unique dates there are, but not how many 
> transactions are in a given date group.  Is this possible with XSL?
> 

Try this:

 <xsl:key name="G1" match="transaction"
use="original_request/inquiry/@date"/>
 <xsl:key name="G2" match="transaction"
use="concat(original_request/inquiry/@date,'
',original_request/inquiry/site)"/>
 <xsl:key name="G3" match="transaction"
use="concat(original_request/inquiry/@date,'
',original_request/inquiry/site,' ',original_request/inquiry/payer)"/>
 <xsl:key name="payers" match="payer" use="."/>
 
 <xsl:template match="log">
  <xsl:apply-templates
select="transaction[generate-id()=generate-id(key('G1',original_request/
inquiry/@date))]" mode="G1"/>
 </xsl:template>
 
 <xsl:template match="transaction" mode="G1">
  <!-- do whatever with the date, here i'm just displaying the value -->
  <xsl:value-of select="original_request/inquiry/@date"/>
  <xsl:text>&#10;</xsl:text>
  <!-- here i'm applying level2 group -->
  <xsl:apply-templates
select="key('G1',original_request/inquiry/@date)[generate-id()=generate-
id(key('G2',concat(original_request/inquiry/@date,'
',original_request/inquiry/site)))]" mode="G2"/>
  <xsl:text>Total: </xsl:text>
  <!-- the Total is the count of the current group -->
  <xsl:value-of
select="count(key('G1',original_request/inquiry/@date))"/>
  <xsl:text>&#10;</xsl:text>
  <xsl:text>&#10;</xsl:text>
 </xsl:template>
 
 <xsl:template match="transaction" mode="G2">
  <xsl:text>Site: </xsl:text>
  <xsl:value-of select="original_request/inquiry/site"/>
  <xsl:text> transactions: </xsl:text>
  <!-- counting transactions of the current group -->
  <xsl:value-of
select="count(key('G2',concat(original_request/inquiry/@date,'
',original_request/inquiry/site)))"/>
  <xsl:text>&#10;</xsl:text>
  <!--
  I can't apply as before because you want all payers displayed,
  so i use the payers key to group by unique payers and pass the current
date and site as parameters.
  -->
  <xsl:apply-templates
select="../transaction/original_request/inquiry/payer[generate-id()=gene
rate-id(key('payers',.))]" mode="G3">
   <xsl:with-param name="date" select="original_request/inquiry/@date"/>
   <xsl:with-param name="site" select="original_request/inquiry/site"/>
  </xsl:apply-templates>
 </xsl:template>
 
 <xsl:template match="payer" mode="G3">
  <xsl:param name="date"/>
  <xsl:param name="site"/>
  <xsl:text>  Payer: </xsl:text>
  <xsl:value-of select="."/>
  <xsl:text> transactions: </xsl:text>
  <!-- use the parameters date and site in the G3 key to get the correct
values for the count -->
  <xsl:value-of select="count(key('G3',concat($date,' ',$site,'
',.)))"/>
  <xsl:text>&#10;</xsl:text>
 </xsl:template>

Hope this helps you.




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


Current Thread