JSTL XML x:if Tag

The <x: if> tag evaluates an XPath expression. If the result is true then the body of the tag is evaluated, otherwise not. It is used to check the conditional value.

Syntax of <x: if> Tag

<x: if attributes> body content </x: if>

Attributes of <x: if> Tag

selectTo evaluate the particular object it specifies xml Xpath expression.
varIt specifies the variable name which stores conditional result.
scopeThe Scope into which the variable attribute has to be set.


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

  <title>  x:if Tags</title>

<h2>Fruits Information:</h2>

<c:set var="fruitss">







<x:parse xml="${fruitss}" var="output"/>

<x:if select="$output/fruits/fruit/price < 100">
   Fruits prices are very low


Details of the Code

  • <c:set var=”fruitss”> tag is used to set the variable name which we want to display in the output.
  • <x:parse xml=”${fruitss}” var=”output”/> tag is used to parse the xml content and the result is been stored in specified variable.
  • <x:if select=”$output/fruits/fruit/price tag is used to test the condition.

Steps for Execution

Before executing the xml programs we should add jar files in eclipse namely:

  • Save this file as example.jsp in your eclipse IDE.
  • Now select this jsp file, right mouse click and select Run as ->Run on server


When the execution process is completed successfully we will get the following output :
x if tag output


Previous Tutorial :  JSTL XML x:forEach Tag :: Next Tutorial : JSTL XML x:set Tag

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!