XSLT if

XSLT <xsl:if> Element

We can put a conditional test against the content of the XML file, using the <xsl:if> element.

The <xsl:if> Element:

In the XSL document, we can add an <xsl:if> element, to put a conditional if test against the content of the XML file.

Syntax:


 ...Code if the expression is true...

Where to use the <xsl:if> Element:

In the XSL file, we need to add the <xsl:if> element inside the <xsl:for-each> element, to add a conditional test. The expression to be evaluated is included in the value of the required test attribute.

Example:

Books.xml:




  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
    2005
    300.00
  

  
    Sociology 1
    Author Name
    2015
    250.00
  

  
    Current Affairs
    Author Name
    2002
    500.00
  

  
    Science Book
    Author 1
    Author 2
    Author 3
    2019
    150.00
  


XSLT Code:




  
  
    

List of Books

Title Price Year

Output:

Explanation:

In the above example, only the title and price elements of the books with the year value higher than 2005 will be displayed.