Advantages of the key() Function
Now that we’ve taken the key() function through its paces, you
can see that it has several advantages:
The
key()function is defined in a stylesheet. That means I can define any number of relationships between parts of an XML document at any time. If I need to define a new relationship tomorrow, I don’t have to change my XML documents.Any number of
key()functions can be defined for a given element. In our parts list example, we could definekey()functions for the values of thevendor-id,part-id, andcomponent-idattributes. We could also createkey()functions based on the text of various elements or their children. If we usedIDs instead of thekey()function, we would be limited to a single index based on the value of the single attribute of theIDdatatype.To sum up the advantages for this point, an element can have more than one
key()defined against it, and that key doesn’t have to be based on an attribute. The key can be based on the element’s text, the text of child elements, or other constructs.Any number of elements can match a given value. Taking another look at our example, when we use the
key()function to find all the parts from a particular country, thekey()function returns a node-set that can have any number of nodes. If we use anIDinstead, legally there can be only one element that matches a given country.The value we use to look up elements in the key function isn’t constrained to be an XML name. If we use the
IDdatatype, ...
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