Defining a Key with <xsl:key>
You define a key()
function with the <xsl:key>
element:
<xsl:key name="supplier-by-country" match="supplier" use="@country"/> <xsl:key name="part-by-supplier" match="part" use="@supplier"/>
The key has three attributes:
nameThis attribute is used to refer to this particular key. When you want to find parts of your XML document, use the
nameto indicate the key you want to use.matchContaining an XPath expression, this attribute specifies what part of the document you want to index. In our sample here, we’ve created two keys: one for retrieving
<supplier>s and one for retrieving<part>s.useContaining another XPath expression, this attribute is interpreted in the context of the
matchattribute. In other words, the first<xsl:key>element here, namedsupplier-by-country, creates an index of all the<supplier>elements, and uses thecountryattribute to retrieve them. The second<xsl:key>element, namedpart-by-supplier, creates an index of all the<part>elements and uses thesupplierattribute to find them.
[2.0] XSLT 2.0 adds a fourth attribute, collation. This allows us to specify a set of rules for how
values are compared. To cite a frequent example from the specs, the
German word for street can be spelled
Strasse or Straße. Using a
German collation for the key function causes those to words to be the
same, despite the fact that they are clearly different strings.
Note
The XSLT 1.0 specification specifically states that the match and use attributes can’t ...
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