[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Sum one attribute while grouping by another
DB,
If you can't use XSLT 2.0, you would be best off in 1.0 using the so-called Muenchian grouping technique to collect your groups. It would allow the collection of nodes that you need. It is also more efficient and will perform better over a large data set than the method you are using. (Actually, at present you aren't grouping at all, as is indicated by your dependency on the items being sorted by type -- you are only using comparison logic to recognize the start and end of groups that are already implicit in the order. A grouping algorithm would be able to work with groups that were not sorted in the input.) The essence of Muenchian grouping is in the use of key-based retrieval to collect all the elements belonging to a particular group. For example, if you had <xsl:key name="items-by-type" match="item" use="@type"/> you would then be able to collect all the 'fruit' items using "key('items-by-type','fruit')" and all the items of type $x using "key('items-by-type',$x)". Not only is this useful for grouping, but it's helpful for aggregating, as you can then say "sum(key('items-by-type','fruit')/@rotten)" to add together the @rotten attributes on the 'fruit' items. Muenchian grouping is well documented on line. Besides key-based retrieval, the one subtlety it depends on is an idiom to recognize a single representative member of each group (commonly the first one). Usually, for this you'll see something like generate-id(.)=generate-id(key('items-by-type',@type)[1] which will be true only for every first item of its type. You need such a de-duplication technique to avoid repeating groups. If this plus your further research leaves you confused, feel free to ask for more detail or clarification. Cheers, Wendell At 02:06 PM 7/24/2008, you wrote: How do I sum one attribute while grouping by another? ====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|