EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial XSLT if
Secondary Sidebar
Python Newspaper

Magic Number in C

Phishing and Pharming

Shell Scripting Interview Questions

Software Testing Interview Questions

What is JavaScript?

XSLT if

XSLT if

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

XSLT if 1

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:

XSLT if 2

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:

XSLT if 3

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"&gt;
<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"&gt;>
<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:

Adding a Text to the exiting Element on the If-Pattern

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:

&lt;?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:

Test Condition On Colors

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 –

  1. XML Schema
  2. XML Mapping
  3. XML ampersand
  4. XML XSD
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more