XSLT’s Key Facility
Now that we’ve covered the id() function in great detail, we’ll move
on to XSLT’s key() function and the <xsl:key> element. Each <xsl:key> element effectively creates an
index of the document. You can then use that index to find all elements
that have a particular property. Once the key is created, we can use the
key() function to retrieve parts
of the document.
For example, 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. (Be aware that it can take a significant amount of memory to create a key, particularly for very large documents.) 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 facility works, and then
we’ll compare it to the id() function.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access