Updated April 18, 2023
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.
Syntax:
Following is the syntax declaration of the variable element.
<xsl:variable
name="vname"
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">
<PublisherName>
publish no. <xsl:number />
<xsl:copy-of select="./Bookname"/>
</PublisherName>
</xsl:template>
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/
count(descendant::scientist))"/>
<xsl:value-of select="//place[count(descendant::scientist) = astro]/
@xml:no"/>
Examples
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"?>
<BookType>
<BoardBook>
<Bookname>Iglepiggle</Bookname>
<PublishedBy>Andrew Davenport</PublishedBy>
<country>USA</country>
<ISBN>158-85-63</ISBN>
<price>18.90</price>
<year>2009</year>
</BoardBook>
<BoardBook>
<Bookname>Fairly Tales</Bookname>
<PublishedBy>Jack Marques</PublishedBy>
<country>China</country>
<ISBN>185-45-33</ISBN>
<price>20.90</price>
<year>2010</year>
</BoardBook>
</BookType>
Following this file we have XSL style sheets with a template match by assigning a variable name to the values.
xsl
<?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="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="BoardBook">
<PublisherName>
publish no. <xsl:number />
<xsl:copy-of select="./Bookname"/>
</PublisherName>
</xsl:template>
</xsl:stylesheet>
Explanation
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:
Output:
Example #2
XML file
<?xml version="1.0"?>
<?xml-stylesheet type="development/xml" href="xxx.xslt"?>
<candidate>
<student born="2006" died="2008" id="01">
<sname>
<fname>Anand</fname>
<lname>Bobby</lname>
</sname>
<profession>Analytics</profession>
<profession>Big data engineer</profession>
<profession>Chef</profession>
</student>
<student born="2004" died="2007" id="02">
<sname>
<fname>D</fname>
<middle_name>E</middle_name>
<lname>F</lname>
</sname>
<profession>Go ibo</profession>
<passtime>Hubernate</passtime>
</student>
</candidate>
Xslt file
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="new">
<tr bgcolor="#9acd32">
<th>fname</th>
<th>lname</th>
</tr>
</xsl:variable>
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:copy-of select="$new" />
<xsl:for-each select="candidate/student">
<tr>
<td><xsl:value-of select="fname"/></td>
<td><xsl:value-of select="lname"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Explanation
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:
Output:
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>Goddess</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>
</list>
XSL file
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="Toppest No" select="54"/>
<xsl:variable name="Toppest candidate" select="'red'"/>
<xsl:variable name="absencevar">
<xsl:choose>
<xsl:when test="count(//channel) > 11">
<xsl:text>Too lenghth</xsl:text>
</xsl:when>
<xsl:when test="count(//channel) > 4">
<xsl:text>reasonable prize</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>fina; scheme</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:template match="/">
<xsl:text>Hello! Your schedule time is</xsl:text>
<xsl:value-of select="$Toppest No"/>
<xsl:text>.
Your toppesst color is </xsl:text>
<xsl:value-of select="$Toppest candidate"/>
<xsl:text>.
Here is a page
</xsl:text>
<xsl:value-of select="$action var"/>
<xsl:text>:
</xsl:text>
<xsl:variable name="channel" select="list/channel"/>
<xsl:call-template name="manipulation">
<xsl:with-param name="channel" select="$channel"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="processlist">
<xsl:param name="channel"/>
<xsl:variable name="favoriteColor">
<xsl:text>chartreuse</xsl:text>
</xsl:variable>
<xsl:text> (choose an option</xsl:text>
<xsl:value-of select="$favoriteColor"/>
<xsl:text>.)
</xsl:text>
<xsl:for-each select="$channel">
<xsl:value-of select="position()"/>
<xsl:text>. </xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Explanation:
The above code uses three variable names and all the values are been assigned.
Output:
Advantages
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.
Conclusion
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 –