Now that we’ve covered the
id() function in great detail, we’ll move on to XSLT’s
key() function. Each
key() function effectively creates an index of the document. You can then use that index to find all elements that have a particular property. Conceptually,
key() works like a database index. If you have a database of (U.S. postal) addresses, you might want to index that database by the people’s last names, by the states in which they live, by their Zip Codes, etc. Each index takes a certain amount of time to build, but it saves processing time later. If you want to find all the people who live in the state of Idaho, you can use the index to find all those people directly; you don’t have to search the entire database.
We’ll discuss the details of how the
key() function works, then we’ll compare it to the
You define a
key() function with the
<xsl:key name="language-index" match="defn" use="@language"/>
The key has three elements:
name, used to refer to this particular key. When you want to find parts of your XML document, use the
name to indicate the key you want to use.
match attribute containing an XPath expression. This specifies what part of the document you want to index. The previous example created an index on all of the
<defn> elements. When we call the
key() function, it will return a
<defn> element. Note: according to Section 12.2 of the XSLT specification, the value of the ...