Updated April 1, 2023
Introduction to XSLT if
XSLT if statement is defined as a conditional pattern used to process a true statement and considered a simple Statement. The statement provides a simple test to use concerning the XML file. If a certain condition is met, the respective code is executed. It takes a test attribute to return a Boolean value through the XPath expression. It doesn’t accept an else-if pattern.
Syntax:
The syntax declaration of <xsl: if> is given as:
<xsl:if test="expression">
<// THEN section is executed here>
</xsl:if>
Here the first line explains the test where the expression is evaluated, the test is required to check the source data or if the pattern is executed. This should result in a true part.
How does XSLT if Statement Works?
The working steps of the if statement is very easy. So, the pattern has one input and a single output. The element information where the statement falls is given here. They are xsl:attribute, xsl:call-template, xsl:variable, xsl:for -each respectively. If our code doesn’t need any element or attribute, then we can just use the if-then structure for the process.
<Thesis>
<xsl:if test="Publisher/Guide= 'Diana Cyril'">
<xsl:value-of select="'Paper Selected'"/>
</xsl:if>
<xsl:if test="contains(Publisher/Guide,'James')">
<xsl:value-of select="'A best Paper Ever'"/>
</xsl:if>
</Thesis>
The code above defines a true statement. We can insert the value of an Xpath expression through a value-of statement, which in turn results in a true condition. The value-of element has a select attribute that has this construct. If XSLT needs else part, we can use <xsl: choose> with their code generation block.
To check whether a given string is NULL or NOT, the following format is instantiated, and the test case is:
<xsl:if
test="not(element
name)">
</xsl:if>
The <if> condition which tests the given multiple variables to ‘0’ and the logic here is:
<xsl:if test="not((number($VAR_gross_bill) = 0)
and (number ($VAR_work per hour) = 0)
and (number($VAR_work_midnite) = 0)
and (number ($VAR_other_extra allowances) = 0))">
The test condition is evaluated to be true when all the given variables are non-zero. Different content is given only if a given condition is met.
Examples of XSLT if
Given below are the examples of XSLT if:
Here we do things in XML and XSLT versions for comparison evaluation.
Example #1
For our example, we are using the ebook concept as an input XML.
Code:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<ebook>
<dept>
<bkname>Python-Basic Learning</bkname>
<author>Guido Van</author>
<country>German</country>
<euro>9.90</euro>
<pubyear>2001</pubyear>
</dept>
<dept>
<bkname>XPath and XSLT</bkname>
<author>Peter Doug</author>
<country>Italy</country>
<euro>22.90</euro>
<pubyear>1965</pubyear>
</dept>
<dept>
<bkname>JEE Language</bkname>
<author>Evangeline Thomas</author>
<country>German</country>
<euro>20.90</euro>
<pubyear>1995</pubyear>
</dept>
<dept>
<bkname>Shell Script Language</bkname>
<author>lousis Brain</author>
<country>Denmark</country>
<euro>15.90</euro>
<pubyear>1995</pubyear>
</dept>
</ebook>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My E-book Collection</h2>
<table border="2">
<tr bgcolor="yellow">
<th>bkname</th>
<th>author</th>
<th>euro</th>
</tr>
<xsl:for-each select="ebook/dept">
<xsl:if test="euro>15">
<tr>
<td><xsl:value-of select="bkname"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="euro"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Explanation:
- Here we are interested in getting only the attributes bkname and author and displaying them in a well-formatted table by taking the condition with the euro element where it meets the criteria euro> 15 in the XPath expression.
- To achieve this, we have used for-each to loop the elements.
Output:
Example #2
If statement with the letters.
Code:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<fish_class>
<type name="jawless">
<bycatch>
<name>Carp</name>
<Place>Maldives</Place>
<fattyfish>15 percent</fattyfish>
<Smallbone> yes </Smallbone>
</bycatch>
</type>
<type name="ediblefish">
<bycatch>
<name>Blue Fin Tuna</name>
<Place>United States</Place>
<fattyfish> 5 percent</fattyfish>
<Smallbone>yes</Smallbone>
</bycatch>
</type>
<type name="Lean Fish">
<bycatch>
<name>chinook salmon</name>
<Place>Alaska</Place>
<fattyfish>10 percent</fattyfish>
<Smallbone>no</Smallbone>
</bycatch>
</type>
</fish_class>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="type">
<xsl:if test="starts-with(@name, 'j')">
Here Is the match: <xsl:value-of select="./@name"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Explanation:
- Here is the code of how XSL transforms the XML code for a test condition. If a test condition is true, it processes further instructions. So here the condition is a name should start with the letter ‘J.
- To determine this, a value-of select is assigned and checks with the type; if it is found, the name is written on the output as shown below.
Output:
Example #3
Evaluating with min and max value bound.
Code:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Dmartstore>
<shop category="Gadgets">
<Model lang="en">GT4352</Model>
<Company>Samsung</Company>
<year>2020</year>
<ownat>120.00</ownat>
</shop>
<shop category="Gadgets">
<Model lang="en">GH5648</Model>
<Company>Redmi</Company>
<year>2018</year>
<ownat>110.00</ownat>
</shop>
<shop category="Gadgets">
<Model lang="en">AMT234</Model>
<Company>Apple</Company>
<year>2019</year>
<ownat>130.00</ownat>
</shop>
</Dmartstore>
XSL:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.java2s.com"
xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
version="1.0">
<xsl:output method="xml"/>
<xsl:template match="*">
<xsl:element name="{name(.)}">
<xsl:for-each select="@*">
<xsl:if test="(name(.) != 'minOccurs') and (name(.) != 'maxOccurs')">
<xsl:attribute name="{name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Explanation:
- Here if the attribute value ‘name’ meets the minimum and maximum range, then the output that it yield is given as.
Output:
Example #4
Testing NIL value.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<data_service>
<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<InBound>2006-10-15T01:05:00.000</InBound>
<S_id>5220004 </S_id>
<message>Client Frames</message>
<Acknowledgment>NSent</Acknowledgment>
</data>
</data_service>
<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">>
<InBound xsi:nil=”true”></InBound>
<S_id>5220004 </S_id>
<message>Client Frames</message>
<Acknowledgment>NSent</Acknowledgment>
</data>
XSL:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.edu.com"
xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
version="1.0">
<xsl:output encoding="UTF-8" indent="yes" method="xml"/>
<xsl:template match="/">
<xsl:element name="File">
<xsl:for-each select="/data_service/data">
<xsl:if test="count(.) > 0">
<xsl:element name="data_service">
<xsl:if test="not(InBound/@xsi:nil=true())">
<xsl:element name="InBound">
<xsl:value-of select="InBound"/>
</xsl:element>
</xsl:if>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Explanation:
- The above code outputs a NIL statement after evaluation.
Example #5
Adding a text to the exiting element on the if-Pattern.
Code:
XML:
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="baaa.xsl" ?>
<delivery>
<product domestic="yes">Franch Express</product>
<product>Electronics</product>
<product domestic="yes">BigBaskets grocery</product>
<product>Libia, Ltd</product>
<product domestic="yes">Peter England, Inc.</product>
</delivery>
XSL Fragment:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<html><body>
<xsl:apply-templates/>
</body></html>
</xsl:template>
<xsl:template match="product">
<p/>
<xsl:if test="@domestic">domestic clearance </xsl:if>
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
Explanation:
- Along with the if test constructs, the text is added in the condition, and the result is given.
Output:
Example #6
Test condition on colors.
Code:
XML:
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="ifcolor.xsl" ?>
<educba_web>
<Courses>Data Mining</Courses>
<Courses>Data Analyst</Courses>
<Courses>Python programming</Courses>
<Courses>J2EE</Courses>
<Courses> R Programming</Courses>
<Courses>REST</Courses>
<Courses>AWS</Courses>
</educba_web>
XSLT:
<?xml version='1.0'?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<html>
<body>
<table border="2" cellpadding="1.8" cellspacing="0" width="60%">
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Courses">
<tr>
<xsl:if test="position() mod 2 = 0">
<xsl:attribute name="bgcolor">Red</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</tr>
</xsl:template>
</xsl:stylesheet>
Explanation:
- The above templates test the if statement with the position and formats the output in a table format.
Output:
Conclusion
Though if-statement is considered to be less elegant, it is beneficial well in testing a single condition. Therefore, in this article, we have seen how if-then logic works with specific implementation. If the test conditions aren’t true, what happens then, programmers shifted to if-else constructs.
Recommended Articles
This is a guide to XSLT if. Here we discuss the introduction, how XSLT if statement works? and examples for better understanding. You may also have a look at the following articles to learn more –