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.
Get XSLT, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.