XSLT <xsl:key>
Element
Definition and Usage
The <xsl:key>
element is a top-level element, which can declare a named key (i.e., a name-value pair assigned to specified elements in an XML document). This key is used in the stylesheet via the key()
function, helping you efficiently access assigned elements in complex XML documents.
Note: Keys do not have to be unique!
Syntax
Attributes
Attribute | Value | Description |
---|---|---|
name | name | Required. Specifies the name of the key. |
match | pattern | Required. Defines which nodes the key is applied to. |
use | expression | Required. Specifies the expression to use as the key value for each node. |
Example 1
Suppose you have an XML file named "persons.xml":
You can define a key in the XSL file as follows:
To find the person with id="050676", use these codes (in the XSL file):