XSLT key() Function

Definition and Usage

The key() function returns a node set from the document by using the index number specified by the <xsl:key> element.

The key() function retrieves a node set that matches the key name and key value specified in the <xsl:key> statement (zero or more nodes). When the XSLT style sheet is initially processed, the key is stored internally to simplify access. Keys can simplify access to nodes in an XML document, but may not be faster than using XPath to retrieve the same nodes.

See the <xsl:key> element.

Syntax

node-set key(string, object)

Parameter

Parameter Description
string Required. Specifies the name of the xsl:key element.
object Required. The string to be searched.

Example

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:key name="cdlist" match="cd" use="title" />
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="key('cdlist', 'Empire Burlesque')">
  <p>
  Title: <xsl:value-of select="title" />
  <br />
  Artist: <xsl:value-of select="artist" />
  <br />
  Price: <xsl:value-of select="price" />
  </p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

View XSL File,View XSL File,View Results.