What is use of xsl:if element in XSLT

In this article we are taking about xsl:if element
  • 1982
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-->

</xsl:if>


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" ?>

<items>

  <item>Asp</item>

  <item>C#</item>

  <item>Java</item>

  <item>java script</item>

  <item>C</item>

  <item>C++</item>

  <item>Perl</item>

  <item>VB</item>

  <item>Sql</item>

  <item>Sql Statement</item>

  <item>Null</item>

</items>


XSLT File(ifblue.xsl)
 

<?xml version='1.0'?>

<xsl:stylesheet version="1.0"

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

  <xsl:template match="/">

    <html>

      <body>

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

          <xsl:apply-templates/>

        </table>

      </body>

    </html>

  </xsl:template>

  <xsl:template match="item">

    <tr>

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

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

      </xsl:if>

      <xsl:apply-templates/>

    </tr>

  </xsl:template>

</xsl:stylesheet>

 

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

© 2019 DotNetHeaven. All rights reserved.