Open In App

XSLT <if> Element

Last Updated : 03 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

XSLT <if> Element is used to check the condition of the content of XML. The test is used to check the condition if it satisfies the condition then inside if is executed.

Syntax:

<xsl:if test=EXPRESSION>
  // Some output if the expression is true
</xsl:if>

Parameters:

  • test: It is the condition that will check on the content of XML.

Example 1: In this example, we will use the <xsl:if> tag to only show the CSE student in the table. Save Both files as mentioned and open xsl file on the Browser, it will show the output as shown below:

XML




<!--Test.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl "href="Rule.xsl" ?>
<student>
   <s>
      <name> Divyank Singh Sikarwar </name>
      <branch></branch>
      <age>18</age>
      <city> Agra </city>
   </s>
   <s>
      <name> Aniket Chauhan </name>
      <branch>CSE</branch>
      <age> 20</age>
      <city> Shahjahanpur </city>
   </s>
   <s>
      <name> Simran Agarwal</name>
      <branch>CSE</branch>
      <age> 23</age>
      <city> Buland Shar</city>
   </s>
   <s>
      <name> Abhay Chauhan</name>
      <branch>CSE</branch>
      <age> 17</age>
      <city> Shahjahanpur</city>
   </s>
   <s>
      <name> Himanshu Bhatia</name>
      <branch>IT</branch>
      <age> 25</age>
      <city> Indore</city>
   </s>
</student>


XML




<!-- Rule.xsl-->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
   <xsl:template match="/">
      <html>
         <body>
            <h1 align="center">Students' Basic Details</h1>
            <table border="3" align="center" >
               <tr>
                  <th>Details</th>
               </tr>
               <xsl:for-each select="student/s">
                  <tr>
                     <xsl:if test="branch='CSE'">
                        <td>
                           <xsl:apply-templates select="name"/>
                           <xsl:apply-templates select="branch"/>
                           <xsl:apply-templates select="age"/>
                        </td>
                     </xsl:if>
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
   <xsl:template match="name">
      Name:
      <span style="font-family:cursive;color:#ff0000">
         <xsl:value-of select="."/>
      </span>
      <br />
   </xsl:template>
   <xsl:template match="branch">
      Branch:
      <span style="font-family:serif;color:#0ff000">
         <xsl:value-of select="."/>
      </span>
      <br />
   </xsl:template>
   <xsl:template match="age">
      Age: 
      <span style="font-family:fantsy;color:#0000ff">
         <xsl:value-of select="."/>
      </span>
   </xsl:template>
</xsl:stylesheet>


Output:

if1

XSLT <if> Element

Example 2: In this example, we will show the details of Student Dhananjay with the help of if tag. Save Both files as mentioned and open xsl file on the Browser, it will show the output as shown below:

XML




<!--Test.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl "href="Rule.xsl" ?>
<student>
   <s>
      <name>Dhananjay</name>
      <branch>CSE</branch>
      <age>18</age>
   </s>
   <s>
      <name> Aniket </name>
      <branch> CSE</branch>
      <age> 20</age>
   </s>
   <s>
      <name>Dhananjay</name>
      <branch> CE</branch>
      <age> 23</age>
   </s>
   <s>
      <name>Dhananjay</name>
      <branch>XE</branch>
      <age> 17</age>
   </s>
   <s>
      <name> Himanshu</name>
      <branch> IT</branch>
      <age>25</age>
   </s>
   <s>
      <name>Dhananjay</name>
      <branch>AI</branch>
      <age>25</age>
   </s>
   <s>
      <name>Max</name>
      <branch> IT</branch>
      <age>25</age>
   </s>
   <s>
      <name>Sam</name>
      <branch>ECE</branch>
      <age>25</age>
   </s>
</student>


XML




<!--Rule.xsl-->
  
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
<xsl:template match="/">
<html>
<body>
<h1 align="center">Students' Basic Details</h1>
<table border="3" align="center" >
<tr>
    <th>Name</th>
    <th>Branch</th>
    <th>Age</th>
</tr>
    <xsl:for-each select="student/s">
<tr
        <xsl:if test="name='Dhananjay'">
    <td>
        <span style="color:#ff0000">
        <xsl:value-of select="name"/></span>
        </td>
    <td>
        <span style="color:#0000ff">
        <xsl:value-of select="branch"/></span>
</td>
          
        <td>
        <span style="color:#00ff00">
        <xsl:value-of select="age"/></span>
        </td>
        </xsl:if>
</tr>
    </xsl:for-each>
    </table>
  
</body>
</html>
</xsl:template>
</xsl:stylesheet>


Output:

if2

XSLT <if> Element



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads