JSTL XML x:forEach Tag

The <x: forEach> tag is used for looping a list of elements in an xml document.

Syntax of <x: forEach> Tag

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

Attributes of <x: forEach> Tag


<%@ 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:forEach select="$output/fruits/fruit/name" var="item" begin="1" end="2">

<tr><td>Fruit Name:</td> <td><x:out select="$item" /></td></tr>




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:forEach select=”$output/fruits/fruit/name” var=”item” begin=”1″ end=”2″> tag is used to looping a list of element by using some attributes.

Steps for ExecutionBefore 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

OutputWhen the execution process is completed successfully we will get the following output :
x forEach tag output

selectTo evaluate the particular object it specifies xml Xpath expression.
varIt specifies the variable name where current item has to be set.
varStatusSpecifies the variable name and tells the status of loop
beginSpecifies the particular value from where the user want to start in the list.
endSpecifies the particular value where the user wants to end the list.
stepSpecifies the increment of the loop.

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!