- Xsl Key Function
- Key Generator
- Xml Xsl Examples
- Xsl Generate-id
- Xsl Generate Id
- Xslt Generate-id Key Example
Definition and Usage. The xsl:key element is a top-level element which declares a named key that can be used in the style sheet with the key function.
Key Basics
XSLT keys provide a simple and efficient way of looking up values based on other values. First, we will take a look at the syntax of keys and then we will look at how they are used in practice.
<xsl:key/>
Keys are created with the <xsl:key> tag, which must be a child of <xsl:stylesheet/>. It has three required attributes shown in the table below.
Attribute | Description |
---|---|
name | Name of the key. |
match | Pattern to match. |
use | The part of the matched node that will serve as the look-up index. |
The key() Function
The key() function is used to look up the node or nodes specified by the key. It takes two arguments: the name of the key and the index.
Xsl Key Function
The example below shows how to create a key and then look up a value in the key.
Code Sample:
Code Sample:
Key Generator
In the example above, the key is created with this line:
Xml Xsl Examples
Xsl Generate-id
Imagine that it creates a lookup table that looks something like this:
Xsl Generate Id
Index | Node-set |
---|---|
Paul | Paul McCartney beatle node |
John | John Lennon beatle node |
George | George beatle node |
Ringo | Ringo beatle node |
The key()
function is used to look up the node or nodes specified by the key. It takes two arguments: the name of the key and the index.
Xslt Generate-id Key Example
As $Firstname
is set to 'John' in the xsl:param
, this line above returns 'http://www.johnlennon.com'. As you can see, the key()
function returns nodes in the XML document. We can use XPath to drill down further into these nodes. In the example above, we drill down to the link
attribute of the beatle
node returned by key()
.