[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: grouping (was: if or template?)

Subject: RE: grouping (was: if or template?)
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 12 May 2000 18:24:57 +0200
surname tracker
(I wrote this post before finding Steve's example just now from Wednesday in my archive; I'll still post it since it is what I did this evening for myself to help me understand what he has developed. I think this is quite neat!)

At 00/05/08 17:54 +0100, Kay Michael wrote:
> I want to then display the Tracker_ID if it is unique followed by all the
data for
> the specific tracker.

A FAQ: use something like
<xsl:for-each select="//tracker-id[not(.=preceding::tracker-id)]">

This is the traditional solution; Steve Muench has just told me about a
brilliant alternative using keys:

<xsl:key name="tid" use="tracker-id" select="."/>

<xsl:for-each
select="//tracker-id[generate-id(.)=generate-id(key('tid',.)[1])]">

I hope Steve will forgive me for announcing this discovery before he does,
I'm quite excited by it because it gives much better performance.

This is a truly kewl idea ... but ... isn't the syntax above incorrect?


Wouldn't the declaration of the key be as follows?

<xsl:key name="tid" match="tracker-id" use="."/>

I apologize in advance if this sounds pedantic, but readers might get confused about which attribute is the matching attribute (match=) and which is the selection (use=) since the cited example had it backwards and incorrectly named.

I have an example below.

I hope this helps.

................... Ken


T:\ftemp>type tests.xml <?xml version="1.0"?> <names> <name><given>Julie</given><surname>Holman</surname></name> <name><given>Margaret</given><surname>Mahoney</surname></name> <name><given>Ted</given><surname>Holman</surname></name> <name><given>John</given><surname>Mahoney</surname></name> <name><given>Kathryn</given><surname>Holman</surname></name> <name><given>Ken</given><surname>Holman</surname></name> </names> T:\ftemp>type tests.xsl <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="text"/>

            <!--prepare to examine all names valued by surname-->
<xsl:key name="surnames" match="name" use="surname"/>

<xsl:template match="/">                         <!--root rule-->
            <!--select only those name elements whose unique
                generated id is equal to the generated id of the
                first of the key members with the same surname-->
  <xsl:for-each
        select="//name[generate-id(.)=
                       generate-id(key('surnames',surname)[1])]">
    <xsl:value-of select="surname"/>     <!--show the grouping-->
    <xsl:text>&#xd;&#xa;</xsl:text>
                        <!--select only those for the grouping-->
    <xsl:for-each select="//name[surname=current()/surname]">
      <xsl:sort select="given"/>    <!--sorted within grouping-->
      <xsl:text>   </xsl:text>
      <xsl:value-of select="given"/>   <!--member distinctions-->
      <xsl:text>&#xd;&#xa;</xsl:text>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>saxon tests.xml tests.xsl
Holman
   Julie
   Kathryn
   Ken
   Ted
Mahoney
   John
   Margaret


-- G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995) Web site: XSL/XML/DSSSL/SGML services, training, libraries, products. Practical Transformation Using XSLT and XPath ISBN 1-894049-04-7 Next instructor-led training: 2000-05-11/12,2000-05-15, - 2000-06-12,2000-06-13,2001-01-27


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



Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.