xsl-list
[Top] [All Lists]

RE: simple question regarding xsl:key and value calculation

2003-08-25 07:22:47
On Mon, 25 Aug 2003, Michael Kay wrote:

Consider

<book id="1">
  <author>John</author>
  <author>Jane</author>
  <author>Mary</author>
</book>
<book id="2">
  <author>John</author>
</book>
<book id="3">
  <author>John</author>
  <author>Jane</author>
</book>

<xsl:key name="k" match="book" use="author"/>

For book id=1, the value of the use attribute is a node-set containing
three author elements. Each element contributes one value to the key.
The value that it contributes is the string-value of the author element.
So the three key values for book id=1 are "John", "Jane", and "Mary".

So the value of key('k', 'Jane') is the set containing book id=1 and
book id=3.

Is that clearer?

i had assumed the above.  what seemed confusing to me was the reference
to "the" key, which threw me until i realized that, when you write that
"each element contributes one value to the key", what that meant was that
each element contributed another (N,V) pair to the key.

it was just the reference to "the key" and "that node" that confused me
into thinking that, somehow, a node-set generated one and only one value
for the expression.

just to be pedantic, what do you mean when you refer to "the key" in 
that first bullet point on p. 243?  if by a "key", you really mean
the set of (N,V) pairs that satisfy the matching, then to write
"The value of the key" is a bit confusing, since it implies that a key
has a *unique* value.  which i assume is not what you mean.

just embarrassing myself once again,
rday

p.s.  given that one can reorder the attributes of a <xsl:key> element,
i'm curious as to how most people define their keys.  rather than the 
common ordering of name/match/use, i find it *far* more intuitive to
define keys with names and orderings like:

<xsl:key name="books-by-author" use="author/name" match="book">

this allows me to read this as that key maps "author/name" -> "book".
or books, as the case may be.

and using that name allows me to write

  key('books-by-author', 'Agatha Christie')

which seems more intuitive.  but that's all just personal preference.
back to work.


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



<Prev in Thread] Current Thread [Next in Thread>