xsl:variable

<xsl:variable>
Syntax:
<xsl:variable name=”qname” > </xsl:variable>
Or: 

<xsl:variable name=”qname” select=”expression” />

The xsl:variable element is used to declare a local or global variable and to give that variable a name and a value. The value can be assigned by either the content of the xsl:variable element or by the select attribute, but not by both. Each variable declaration requires a separate xsl:variable element. Global variables are declared in the top level of the style sheet (as children of the xsl:stylesheet or xsl:transform elements). Local variables are declared within the template body.
Note that once you set a variable’s value, there is no provision in the XSLT language to change or modify that value. This is in sharp contrast to most other computer languages which allow you to repeatedly reassign variable values.
The xsl:param element can also be used to declare parameters. The only real difference between a variable and a parameter is how the value is assigned.
Like all XSLT elements, the xsl:variable element must be closed (well-formed). If the select attribute is present, then this element is self-closing. If the select attribute is not present, then this element is not self-closing and the separate closing element is mandatory.
name=”qname”
The mandatory name attribute is the qname of the expression. A qname is a qualified name that is composed of an optional namespace prefix, a colon which is only present if there is a prefix, and a mandatory XML name (for example, xsl:zipcode or zipcode). Note the following rules concerning when two different variables can have the same name. (The same rules apply to the name attribute of the xsl:param element.)
• A name can be repeated if one of the names is in an imported stylesheet and therefore has a lower import precedence. Under these circumstances, the higher import precedence
name will always have precedence.

• Two different variables can have the same name if they can never occur within the same scope. Therefore no ambiguity can occur (which would be an error).

• A local and global variable can have the same name. However, when the local variable is in scope, the global variable cannot be accessed.

If the variable element contains no content and a select attribute has been not assigned, then the named variable is set to be the empty string.

select=”expression”
The optional select attribute is an expression that defines the variable. If the select attribute is present, then the xsl:variable element cannot contain any content and is self-closing. If an expression is given, the data type must be Boolean, node-set, number or string. If it is a literal string, the string must be enclosed within opening and closing quotes and in turn, that value must be enclosed again in opening and closing quotes. For example:
<xsl:variable name=”emp_name” select=” ‘ fname ‘ ” />

Or

<xsl:variable name=”emp_name” select=’ ” fname ” ‘ />

If the name attribute is assigned, a select attribute is not assigned, and there is no content, then the named variable is set to be the empty string.
Source code of xsl_variable.xsl:
<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE xsl:stylesheet[

	<!ENTITY nbsp " ">

 ]>

<xsl:stylesheet version="1.0" xmlns:xsl=

"http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" 

encoding="iso-8859-1" />



<!-- XSL Variable Declaration-->

<xsl:variable name="tbl">

 	<tr>

		<td><b>Name</b></td>



		<td><b>Address</b></td>



		<td><b>Department</b></td>



		<td><b>Designation</b></td>



		<td><b>Age</b></td>

		

		

	</tr>

 </xsl:variable>



 <xsl:attribute-set name="tblAttrib">

 <xsl:attribute name="border">

 2

 </xsl:attribute>



 <xsl:attribute name="cellspacing">



 3

 </xsl:attribute>



 <xsl:attribute name="bordercolor">



"#3344dd"



 </xsl:attribute>



 </xsl:attribute-set>

<xsl:template match="/">

<ebizml>

<head>

<title>XSLT xsl:choose Example</title>

</head>

<body>



<span style="width 600px;border: 2px outset blue solidl;display:block">





</span><br /> 

<table xsl:use-attribute-sets="tblAttrib">

<xsl:copy-of select="$tbl" />



<xsl:for-each select="ebiz/employee_details">



<xsl:sort data-type="number" order="descending" select="age" /> 

 	<tr>

		<td><xsl:value-of select="fname" /


> <xsl:value-of select="lname" /></td>



		<td><xsl:value-of select="address" /></td>



		<xsl:choose>

			<xsl:when test="department-sub ='Java'">



		<td style="background-color:pink"> 


<xsl:value-of select="department" /><xsl:text>


[</xsl:text><xsl:value-of select="department-sub" />


<xsl:text>]</xsl:text></td>

		</xsl:when

		<xsl:when test="department ='TECHNICAL'">

		<td style="background-color:#
FFCC99;color:white;font-weight:bold">

			

			<xsl:value-of select="department" />

			 

			<xsl:text>[</xsl:text><xsl:value-of se

lect="department-sub" /><xsl:text>]</xsl:text></td>



		</xsl:when>

		<xsl:otherwise>

			<td><xsl:value-of select="department" /> </td>

		</xsl:otherwise>

		</xsl:choose>

		<xsl:choose>

			

			<xsl:when test="department='TECHNICAL'">

<td style="background-color:
#FFCC99;color:white;font-weight:bold">

		 	<xsl:value-of select="designation" />

		</td>

		</xsl:when>



	<xsl:otherwise>

			 

		<td style="font-weight:bold">

			<xsl:value-of select="designation" />

		</td>



		</xsl:otherwise>



	</xsl:choose>



	<td>

		<xsl:value-of select="age" />



	</td>



	</tr>



 </xsl:for-each>



</table>



</body>



</ebizml>



</xsl:template>



</xsl:stylesheet>
Scroll to Top