What is use of xsl:if element in XSLT

In this article we are taking about xsl:if element
  • 2161
The <xsl:if >element is used to text conditions, if a condition is true then template is processed and if you want to achieve the functionality of an if-then-else statement then use the <xsl:choose> element with one <xsl:when> and one <xsl:otherwise> children. It is supported to IE 5.0 and FF 1.0.

Syntax of xsl:if element

<xsl:if  test = "some-expression">

  <!-- template-->


It has a attribute-

  • test

                 text expression Specifies the condition in the source data to test. If the value is true, the template is processed; if it is not, no action is taken.


Example of  xsl:if element prints  every next row is blue

XML File(abc.xml)

<?xml version='1.0'?>

<?xml-stylesheet type="text/xsl" href="ifblue.xsl" ?>





  <item>java script</item>






  <item>Sql Statement</item>



XSLT File(ifblue.xsl)

<?xml version='1.0'?>

<xsl:stylesheet version="1.0"

      xmlns:xsl="http://www.google.com/XSL/Transform" >

  <xsl:template match="/">



        <table border="1" cellpadding="2" cellspacing="0" width="40%">






  <xsl:template match="item">


      <xsl:if test="position() mod 2 = 0">

        <xsl:attribute name="bgcolor">blue</xsl:attribute>







Further Readings

You may also want to read these related articles :

Ask Your Question

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.