XSLT <xsl:variável>
❮ Referência Completa do Elemento XSLT
Definição e uso
O elemento <xsl:variable> é usado para declarar uma variável local ou global.
Nota: A variável é global se for declarada como um elemento de nível superior e local se for declarada em um modelo.
Nota: Depois de definir o valor de uma variável, você não pode alterar ou modificar esse valor!
Dica: Você pode adicionar um valor a uma variável pelo conteúdo do elemento <xsl:variable> OU pelo atributo select!
Sintaxe
<xsl:variable
name="name"
select="expression">
<!-- Content:template -->
</xsl:variable>
Atributos
Attribute | Value | Description |
---|---|---|
name | name | Required. Specifies the name of the variable |
select | expression | Optional. Defines the value of the variable |
Exemplo 1
Se o atributo select estiver presente, o elemento <xsl:variable> não pode conter nenhum conteúdo. Se o atributo select contiver uma string literal, a string deverá estar entre aspas. Os dois exemplos a seguir atribuem o valor "red" à variável "color":
<xsl:variable name="color" select="'red'" />
<xsl:variable name="color" select='"red"' />
Exemplo 2
Se o elemento <xsl:variable> contiver apenas um atributo name e não houver conteúdo, o valor da variável será uma string vazia:
<xsl:variable name="j" />
Exemplo 3
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable
name="header">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
</xsl:variable>
<xsl:template
match="/">
<html>
<body>
<table border="1">
<xsl:copy-of select="$header" />
<xsl:for-each
select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
❮ Referência Completa do Elemento XSLT