Re: [xsl] Who can give me tech info about xsl:key and key()? (was: Lookup in a xml file with a key)

Subject: Re: [xsl] Who can give me tech info about xsl:key and key()? (was: Lookup in a xml file with a key)
From: tcn@xxxxxxxxxxxxx (Trevor Nash)
Date: Tue, 10 Jul 2001 14:28:57 GMT
>Hi xsl:key/key()-experts,
>I've seen more of these XSL's as shown below and I'm surprised that they 
>Could someone give me more technical info about how it works?
>What surprises me is that you declare an xsl:key at the top of the XSL, for 
>a second XML-file which is to be read by the document()-function. So is it 
>possible to use xsl:key for multiple XML's and how do you use the 
>key()-function then?

You do not declare a key 'for a particular file'.  A key declaration
could apply to *any* file processed by the stylesheet.  Think of it
like this: key('k', 'v') says use key definition 'k' to find all the
nodes in the same document as the context node which match the key's
'select' and have 'v' as the value of the 'use' expression.

Notice the phrase 'same document as the current context' -- this is
why you often see a for-each such as
>     <xsl:for-each select="document('lookup2.xml')">
>        ..."key('kLookup',$input)"
>     </xsl:for-each>
The value of the select can in fact be any old node as long as its
from the right document.

>And when is the key-table build, what is its scope and how must I imagine 
>such a table (if it is one)?
It is usually a hash table of some kind, and it is usually built the
first time you apply key() to a document.  Depends on the processor
you are using.

You can always concoct an XPath expression which will do exactly the
same as any given use of key().  So you can also think of it as a way
of defining your own XPath functions, with the added feature that the
XSLT processor will take the use of xsl:key as a hint to find a fast
way of executing it assuming it will be used lots of times.  A key can
be a lot easier to read than the equivalent XPath.

Trevor Nash
Traditional training & distance learning,
Consultancy by email

Melvaig Software Engineering Limited
voice:     +44 (0) 1445 771 271 
email:     tcn@xxxxxxxxxxxxx

 XSL-List info and archive:

Current Thread