//XSLT Variable

XSLT Variable

XSLT Variable

Definition of XSLT Variable

XSLT variable is defined as special tags used to declare a local or global variable that we make use of to store any values. The declared variables are referenced within an Xpath expression. Once it is set we cannot overwrite or update the variables. The scope of the element is done by the element that contains it. While we are setting a variable it could be done as global and local variables. The top-level element declared in the file is named as a global variable and the local variables are assigned within the template section.


Following is the syntax declaration of the variable element.

select=” Xpathexpression”>// template section</xsl:variable>

Here the required attributes are name defines the variable name and through the select statement, we can define the value of the corresponding variable. If the template is assigned then the select attribute is avoided.

How do variables work in XSLT?

The variables used in other programming language is completely different from the one of XSLT and they are variable. Here the attribute variable is case-sensitive. This variable is immediately referenced with XPATH expression using the name of the variable with the $ symbol instead of a Specified node.<xsl:variable name=”animal” select=”‘cat'” />
<xsl:variable name=”animal” select= ‘”cat”’ />
Here the <xsl: variable> contains a name attribute with a literal string done with the quotes. And the dollar sign is not assigned here with the name. It is later referred to as the variable. To get the value or to output the above statement  we can do it as follows:

<xsl:value-of select=”$animal”/>

Mostly we often prefer to use <xsl: variable> in top-level to avoid recalculation while implementation. The other term called convenience variable is defined in layman terms means the variable is not voluntarily assigned they are declared based on the developers.

The following templates assign the variable which includes both text and parameter values. The output is generated by referring to the value of the variable.

<xsl:template match=”BoardBook”>
publish no. <xsl:number />
<xsl:copy-of select=”./Bookname”/>

A variable takes a lot of types of values like integer or declared within XPath type expressions.

With Integer

<xsl:variable name=”number books”
select=”13″ />

With String

<xsl:variable name=”bookname”
select=”‘Harry Potter'” />

With Input document

<xsl:variable name=”Booktitle”
select=”/Typr/Board/Page/Pno[1]” />
<xsl:variable name=”BookAuthor”
select=”/Typr/Board/Page//Bauthor[1]” />

For instance, let’s take a scenario like we need to output the book title of a respective Document in various places. We can do with the XSL variable and the book title is changed provided the changes done in one preferred location in the XSLT file. For example, we may need to use the value of current-time twenty times. Instead of passing a call to current-time(), we can call once and the corresponding value is stored in a variable.

<xsl:variable name=”” select=astro”max(//country/
<xsl:value-of select=”//place[count(descendant::scientist) = astro]/


Below example create a demo on two files  XSL and XML  with their elements and their child elements in the XML file and matches them with the XSL variable name. Let’s get started with the first demo.

Example #1 – Here is our XML file taking Book details

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<?xml-stylesheet type=”text/xsl” href=”test.xsl”?>
<PublishedBy>Andrew Davenport</PublishedBy>
<Bookname>Fairly Tales</Bookname>
<PublishedBy>Jack Marques</PublishedBy>

Following this file we have XSL style sheets with a template match by assigning a variable name to the values.


<?xml version=”1.0″ encoding=”utf-8″?>
<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” >
<xsl:output method=”html” indent=”yes”/>
<xsl:variable name=”new”>01</xsl:variable>
<xsl:template match=”/”>
<xsl:template match=”BoardBook”>
publish no. <xsl:number />
<xsl:copy-of select=”./Bookname”/>


Here an XML file is converted into a new XML file by assigning a variable name simultaneously  I have incremented the variable many times when need with the new variable.  The above code uses a global variable and could be accessed throughout the documents.XSLT supports three modes of Output methods XML, HTML and Text. Here I have used HTML to show. Well, we can see the output like this:



Example #2

XML file

<?xml version=”1.0″?>
<?xml-stylesheet type=”development/xml” href=”xxx.xslt”?>
<student born=”2006″ died=”2008″ id=”01″>
<profession>Big data engineer</profession>
<student born=”2004″ died=”2007″ id=”02″>
<profession>Go ibo</profession>

Xslt file

<?xml version=”1.0″?>
<xsl:stylesheet version=”1.0″
<xsl:variable name=”new”>
<tr bgcolor=”#9acd32″>
<xsl:template match=”/”>
<table border=”1″>
<xsl:copy-of select=”$new” />
<xsl:for-each select=”candidate/student”>
<td><xsl:value-of select=”fname”/></td>
<td><xsl:value-of select=”lname”/></td>


As a result of adding this stylesheet and applying this rule of variable assignments, the output is generated. The resulting Output is given as follows:


xml 2

Example #3

Xml file

<?xml version=”1.0″?>
<list xml:lang=”en”>
<Theme>Welcome to my channel </Theme>
<channel>BBC news</channel>
<channel>Fashion news</channel>
<channel xml:lang=”oo”>channel 4</channel>
<channel xml:lang=”pp-gb”>channel 5</channel>
<channel xml:lang=”dd”>channel 6</channel>
<channel xml:lang=”ll”>channel 7</channel>

XSL file

<?xml version=”1.0″?>
<xsl:stylesheet version=”1.0″
<xsl:output method=”text”/>
<xsl:variable name=”Toppest No” select=”54″/>
<xsl:variable name=”Toppest candidate” select=”‘red'”/>
<xsl:variable name=”absencevar”>
<xsl:when test=”count(//channel) &gt; 11″>
<xsl:text>Too lenghth</xsl:text>
<xsl:when test=”count(//channel) &gt; 4″>
<xsl:text>reasonable prize</xsl:text>
<xsl:text>fina; scheme</xsl:text>
<xsl:template match=”/”>
<xsl:text>Hello!  Your schedule time is</xsl:text>
<xsl:value-of select=”$Toppest No”/>
Your toppesst  color is </xsl:text>
<xsl:value-of select=”$Toppest candidate”/>

Here is a page

<xsl:value-of select=”$action var”/>
<xsl:variable name=”channel” select=”list/channel”/>
<xsl:call-template name=”manipulation”>
<xsl:with-param name=”channel” select=”$channel”/>
<xsl:template name=”processlist”>
<xsl:param name=”channel”/>
<xsl:variable name=”favoriteColor”>
<xsl:text>    (choose an option</xsl:text>
<xsl:value-of select=”$favoriteColor”/>
<xsl:for-each select=”$channel”>
<xsl:value-of select=”position()”/>
<xsl:text>.  </xsl:text>
<xsl:value-of select=”.”/>


The above code uses three variable names and all the values are been assigned.


xml 3


XSL variables are very useful in many circumstances.

XSLT variables have the advantage to store the values that we need to manipulate or statically define.
Variables help in avoiding long typing of XPath expression in case of complicated instructions.
XSL Being a Formatting language used for many XML Applications by providing elements and variable names with local or global declarations where they exclusively focus on formatting Objects.


XSLT is gaining much importance in business logic and produces a few best practices in achieving a good result in XSL. Therefore in this article, we have seen how variables are declared with the example.

Recommended Articles

This is a guide to XSLT Variable. Here we discuss the definition, syntax, How do variables work in XSLT? examples with code implementation. You may also have a look at the following articles to learn more –

Oracle XML
XML ampersand
XML Mapping
XML File 

The post XSLT Variable appeared first on EDUCBA.

Read more: educba.com