Re: XSLT 2.0 Idea: third argument for key()

Subject: Re: XSLT 2.0 Idea: third argument for key()
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 04 Sep 2000 18:26:25 -0400
At 00/09/04 19:56 +0100, Jeni Tennison wrote:
In brief: I'd like to suggest having a third argument for key(), a node
set, such that the key only retrieves nodes with the relevant key value
that are within this node set.

When the node set is subset based entirely on hierarchy, I do this today using generate-id() based on the premise that generate-id() produces a name token (that never includes a space).


Here's an example of the second area in which this would be useful,
restricting nodes to a known node set: say I had a document with a massive
dataset like:

<athletes>
  <country id="GRB">
    <athlete event="100 metres">...</athlete>
    <athlete event="200 metres">...</athlete>
    <!-- another 400 athletes -->
  </country>
  <country id="ROI">
    <athlete event="100 metres">...</athlete>
    <athlete event="800 metres">...</athlete>
    <!-- another 400 athletes -->
  </country>
  ...
</athletes>

I want to put the details of the athletes in a table, with columns being
countries and rows being events, so I use a key to index on the event, and
access the data for the table using:

  key('athletes', @event)[generate-id(parent::country) =
                          generate-id(current()/parent::country)]

or something similar. It would be a lot cleaner to do:

key('athletes', @event, parent::country/child::athlete)

What I would do in this case is:


...
<!ENTITY lookup-athletes
         "concat( generate-id(parent::country), ' ', @event )>
...
<xsl:key name="athletes" match="athlete"
         use="&lookup-athletes;"/>
...
  key('athletes', &lookup-athletes;)
...

Any thoughts? Objections? Implementation issues?

Below are fragments from the solution to one of the exercises in my two-day hands-on course where students are required to group hockey team statistics based on the number of games played within either their division or their conference (two separate tables).


The Muenchian Method of grouping uses *all* members of the source node tree which isn't *directly* applicable since the grouping has to be subset by the hierarchy. What I do is calculate the key table value to include the generated id of the ancestral point in the source node tree. The space delimiter ensures no possible ambiguity in the concatenated value (provided all values are calculated the same way ... which I do using a general entity to ensure I don't screw up my typing).

...
<!DOCTYPE xsl:stylesheet [
<!--use entities to keep typing down and promote consistency-->
<!ENTITY lookup-games-division
"concat( generate-id(ancestor::division), ' ', g)">
<!ENTITY lookup-games-conference
"concat( generate-id(ancestor::conference), ' ', g)">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">


<xsl:key name="games-division" match="team"
use="&lookup-games-division;"/> <!--games played within division-->
<xsl:key name="games-conference" match="team"
use="&lookup-games-conference;"/><!--games played within conference-->


...
<xsl:for-each select="division">
<h3><xsl:value-of select="name"/> Division</h3>
<xsl:for-each select=".//team
[generate-id(.)=generate-id(key('games-division',
&lookup-games-division;)[1])]">
...
<xsl:for-each
select="key('games-division',&lookup-games-division;)">
...
<h2><xsl:value-of select="name"/> Conference</h2>
<xsl:for-each select=".//team
[generate-id(.)=generate-id(key('games-conference',
&lookup-games-conference;)[1] &lookup-games-conference;)[1])]">
...
<xsl:for-each
select="key('games-conference',&lookup-games-conference;)">
...


The above works quite smoothly and is the basis of how I teach students to group subsets of the source node tree (after teaching the Muenchian Method of grouping all found members in the source node tree).

I hope this helps.

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


-- 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. Book: Practical Transformation Using XSLT and XPath ISBN1-894049-05-5 Article: What is XSLT? http://www.xml.com/pub/2000/08/holman Next public instructor-led training: 2000-09-19/20,2000-10-03/05, - 2000-10-09/10,2000-10-19,2000-11-06/07,2000-11-12, - 2000-12-03/04,2001-01-27


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



Current Thread