This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: grouping (was: if or template?)


(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@CraneSoftwrights.com
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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]