XSLT <xsl:key>
❮ Kompletní reference prvků XSLT
Definice a použití
Prvek <xsl:key> je prvek nejvyšší úrovně, který deklaruje pojmenovaný klíč, který lze použít v šabloně stylů s funkcí key().
Poznámka: Klíč nemusí být jedinečný!
Syntax
<xsl:key
name="name"
match="pattern"
use="expression"/>
Atributy
Attribute | Value | Description |
---|---|---|
name | name | Required. Specifies the name of the key |
match | pattern | Required. Defines the nodes to which the key will be applied |
use | expression | Required. The value of the key for each of the nodes |
Příklad 1
Předpokládejme, že máte soubor XML s názvem „persons.xml“:
<persons>
<person name="Tarzan" id="050676"/>
<person name="Donald" id="070754"/>
<person name="Dolly" id="231256"/>
</persons>
Klíč v souboru XSL můžete definovat takto:
<xsl:key name="preg" match="person" use="@id"/>
Chcete-li najít osobu s id="050676", napište (do souboru XSL):
<?xml version="1.0" encoding="UTF-8"?>
<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>
❮ Kompletní reference prvků XSLT