Latest web development tutorials

XSLT <xsl: key> element

XSLT Elements Reference Manual Complete XSLT Element Reference Manual

Definition and Usage

The <xsl: key> element is the top element (top-level element), it can declare a named key (that is specified in the XML document elements are assigned the name and value pairs). The key by key () function in the style sheet used to help you effectively allocate access elements in a complex XML documents.

Note: The key does not have to be unique!


grammar

<xsl:key
name="name"
match="pattern"
use="expression"/>

Attributes

属性 描述
name name 必需。规定键的名称。
match pattern 必需。定义该键被应用到哪个节点。
use expression 必需。指定要作为每个节点的键的值使用的表达式。

Example 1

Suppose you have named "persons.xml" XML file:

<persons>
<person name="Tarzan" id="050676"/>
<person name="Donald" id="070754"/>
<person name="Dolly" id="231256"/>
</persons>

You can define a key in the XSL file, as follows:

<xsl:key name="preg" match="person" use="@id"/>

To find id = "050676" in person, please use the code (in the XSL file):

<?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="preg" match="person" use="@id"/>

<xsl:template match="/">
<html>
<body>
<xsl:for-each select="key('preg','050676')">
<p>
Id: <xsl:value-of select="@id"/><br />
Name: <xsl:value-of select="@name"/>
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>

</xsl:stylesheet>


XSLT Elements Reference Manual Complete XSLT Element Reference Manual