This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: grouping (was: if or template?)
G. Ken Holman writes:
> <!--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>
</xsl:text>
> <!--select only those for the grouping-->
> <xsl:for-each select="//name[surname=current()/surname]">
> <xsl:sort select="given"/> <!--sorted within grouping-->
you can speed this up again, I believe, by using the key again
<xsl:for-each
select="//name[generate-id(.)=
generate-id(key('surnames',surname)[1])]">
<xsl:variable name="surname" select="surname"/>
<xsl:for-each select="key('surnames',$surname)">
...
that is to say, for every unique surname, consult the key again to
get the list of people with that surname. that way, you do not have to
navigate the tree again at all, since the list of relevant nodes is
already known.
Sebastian
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list