XSLT <xsl:if> Element

The XSLT <xsl:if> element is used to specify a conditional test against the content of the XML file.

<xsl:if test="expression">
  ...some output if the expression is true...

Parameter explanation

test: It specifies a condition in xml data to test.

XSLT <xsl:if> Element Example

Let's take an example to create a table of element with its attribute "id" and its child <firstname>, <lastname>, <nickname>, and <salary> by iterating over each employee. Let's take a condition which takes salary to be greater than 15000 and then prints the employee's details.


<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?> 
   <employee id = "001">
   <employee id = "024"> 
   <employee id = "056"> 


<?xml version = "1.0" encoding = "UTF-8"?> 
<xsl:stylesheet version = "1.0" 
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
   <xsl:template match = "/"> 
            <h2>Employee </h2> 
            <table border = "1"> 
               <tr bgcolor = "pink"> 
                  <th>First Name</th> 
                  <th>Last Name</th> 
                  <th>Nick Name</th> 
               <xsl:for-each select = "class/employee"> 
                  <xsl:if test = "salary > 30000"> 
                        <td><xsl:value-of select = "@id"/></td> 
                        <td><xsl:value-of select = "firstname"/></td> 
                        <td><xsl:value-of select = "lastname"/></td>
                        <td><xsl:value-of select = "nickname"/></td>> 
                        <td><xsl:value-of select = "salary"/></td>


XSLT Xsl if element 1
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +