RE: [xsl] counting and grouping issue

Subject: RE: [xsl] counting and grouping issue
From: "Bjorndahl, Brad" <brad.bjorndahl@xxxxxxxxxxxxxxxx>
Date: Tue, 8 May 2007 17:03:02 -0400
Hi,

A key is applied to the entire document, so when you
"count(key('itemsByAuthor', .))", you are counting _all_ products from a
specific author.

You need a more specific 'use' value. You can concatenate two values,
such as use="concat(../@name,Attributes/author)" to capture the
Category. Then, in the template, "count(key('itemsByAuthor',
concat(ancestor::Category/@name,.) ))". Something like that, I'm not
certain of the syntax. You can put a lot of different stuff into key
'use' values.

A few more things.

I don't think that this:
  <xsl:for-each
select="Product/Attributes/author[not(.=preceding::Product/Attributes/au
thor)]">
is doing what you expect. It seems to be trying to skip duplicates, but
your expected output is not skipping.

Also, once you go into:
  <xsl:for-each select="key('itemsByAuthor', .)">
your context changes to Product elements (because of the key 'match'
attribute), but you already went to some trouble to go down to the
author elements. It's not wrong based on your test data, but I'm sure
there is a simpler way.

Using XSLT 2.0 is the best way to go for this kind of problem.

Brad


-----Original Message-----
From: Charles Ohana [mailto:charles.ohana@xxxxxxxxxxxxxx]
Sent: May 8, 2007 2:19 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] counting and grouping issue

Hello,
I'm having an issue while counting and aggregating nodes that have
similar subnodes value within the same category.
In other words I'm trying to do something like a group by (in sql) I
must be doing something wrong somewhere. Please see my xml, stylesheet
and output to see what I'am trying to accomplish.

Thank you all 4 ur help.

OUTPUT
ONE : 4 : Gary Numan : Are Friends Electric ONE : 4 : Gary Numan : We
Are Glass ONE : 4 : Gary Numan : I Die You Die TWO : 4 : Gary Numan :
Unknown TWO : 1 : Bob MArley : Jammin

EXPECTED OUTPUT
ONE : 3 : Gary Numan : Are Friends Electric ONE : 3 : Gary Numan : We
Are Glass ONE : 3 : Gary Numan : I Die You Die TWO : 1 : Gary Numan :
Unknown TWO : 1 : Bob MArley : Jammin

XML
<?xml version="1.0" encoding="ISO-8859-1" ?> <ApplicationCatalogue>
   <Category name="category 1" description="ONE">
      <Product code="TT000002630">
         <Attributes>
            <title>Are Friends Electric</title>
            <author>Gary Numan</author>
         </Attributes>
      </Product>
      <Product code="TT000002631">
         <Attributes>
            <title>We Are Glass</title>
            <author>Gary Numan</author>
         </Attributes>
      </Product>
      <Product code="TT000002632">
         <Attributes>
            <title>I Die You Die</title>
            <author>Gary Numan</author>
         </Attributes>
      </Product>
   </Category>

   <Category name="category 2" description="TWO">
      <Product code="TT000002632">
         <Attributes>
            <title>Unknown</title>
            <author>Gary Numan</author>
         </Attributes>
      </Product>

      <Product code="TT000002669">
         <Attributes>
            <title>Jammin</title>
            <author>Bob Marley</author>
         </Attributes>
      </Product>
   </Category>
</ApplicationCatalogue>


XSL
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:output method="text" indent="no"/>
      <xsl:key name="itemsByAuthor" match="Product"
use="Attributes/author"/>
      <xsl:template match="/ApplicationCatalogue">
         <xsl:for-each select="Category">
         <xsl:for-each
select="Product/Attributes/author[not(.=preceding::Product/Attributes/au
thor)]">
             <xsl:variable name="cnt"><xsl:value-of
select="count(key('itemsByAuthor', .))" /></xsl:variable>
                 <xsl:for-each select="key('itemsByAuthor', .)">
                     <xsl:value-of select="../@description" />
                     <xsl:text> : </xsl:text>
                     <xsl:value-of select="$cnt" />
                     <xsl:text> : </xsl:text>
                     <xsl:value-of select="Attributes/author" />
                     <xsl:text> : </xsl:text>
                     <xsl:value-of select="Attributes/title"/>
                     <xsl:text>&#10;</xsl:text>
                 </xsl:for-each>
         </xsl:for-each>
         </xsl:for-each>
      </xsl:template>
</xsl:transform>

Current Thread