XSLT <xsl:chave>
❮ Referência Completa do Elemento XSLT
Definição e uso
O elemento <xsl:key> é um elemento de nível superior que declara uma chave nomeada que pode ser usada na folha de estilo com a função key().
Nota: Uma chave não precisa ser única!
Sintaxe
<xsl:key
name="name"
match="pattern"
use="expression"/>
Atributos
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 |
Exemplo 1
Suponha que você tenha um arquivo XML chamado "persons.xml":
<persons>
<person name="Tarzan" id="050676"/>
<person name="Donald" id="070754"/>
<person name="Dolly" id="231256"/>
</persons>
Você pode definir uma chave em um arquivo XSL como este:
<xsl:key name="preg" match="person" use="@id"/>
Para encontrar a pessoa com id="050676", escreva (no arquivo 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>
❮ Referência Completa do Elemento XSLT