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

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
xslt 2.0 generate id
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

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.