Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XSLT <xsl:variable> elemens


May 28, 2021 XSL T


Table of contents


XSLT and lt;xsl:variable

The XSLT and xsl:variable elements can be used to declare variables, as detailed in this section.


XSLT <xsl:variable> elemens Complete XSLT element reference manual

Definitions and usages

The element is used to declare local or global variables.

Note: If declared as a top-level element, it is a global variable. If you declare a variable within a template, it is a local variable.

Note: Once you set the value of a variable, you cannot change or modify it!

Tip: You can add values to variables by the contents of the element or through the select property!


Grammar

<xsl:variable
name="name"
select="expression">

<!-- Content:template -->

</xsl:variable>

Property

Attributes value describe
name name Required.The name of the specified variable.
select expression Optional.Define the value of the variable.

Instance 1

If you set the select property, the element cannot contain anything. I f the select property contains a text string, you must quote the string. The following two examples are the variable "color" assigned "red":

<xsl:variable name="color" select="'red'" />

<xsl:variable name="color" select='"red"' />

Instance 2

If the element contains only the name property and has no content, the value of the variable is an empty string:

<xsl:variable name="j" />

Instance 3

The following example assigns a variable "header" to the contents of the element by the contents of the element:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="header">
<tr>
<th>Element</th>
<th>Description</th>
</tr>
</xsl:variable>

<xsl:template match="/">
<html>
<body>
<table>
<xsl:copy-of select="$header" />
<xsl:for-each select="reference/record">
<tr>
<xsl:if category="XML">
<td><xsl:value-of select="element"/></td>
<td><xsl:value-of select="description"/></td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
<br />
<table>
<xsl:copy-of select="$header" />
<xsl:for-each select="table/record">
<tr>
<xsl:if category="XSL">
<td><xsl:value-of select="element"/></td>
<td><xsl:value-of select="description"/></td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

XSLT <xsl:variable> elemens Complete XSLT element reference manual