This post helps you understand how to use the <x:forEach> tag in the JSTL XML tags library with code example.

You know, the <x:forEach> is the XML iteration tag. This tag is used to iterate over nodes in an XML document.

 

JSTL <x:forEach> Syntax:

<x:forEach

   var="<string>"

   select="<string>"

   begin="<int>"

   end="<int>"

   step="<int>"

   varStatus="<string>">

   ...

</x:forEach>

 

Attributes:

Name

Description

var

Name of the scoped variable which holds the current item in the iteration. This variable’s type depends on the XPath expression in the select attribute.

select

XPath expression to evaluate.

begin

Begin index of the iteration. Iteration begins at the value mentioned in this attribute value. First item has index of 0.

end

End index of the iteration. Iteration stops at the value mentioned in this attribute value (inclusive).

step

Iteration processes for the step value mentioned in this attribute.

varStatus

Name of the scoped variable which holds the loop status of the current iteration. This variable is of type javax.servlet.jsp.jstl.core.LoopTagStatus and has nested visibility.

 

 

JSTL <x:forEach> Example:

 

The following JSP example iterates over nodes in citizens XML document and displays node values in tabular format. 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
 "http://www.w3.org/TR/html4/loose.dtd">
<html> 
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>&lt;x:forEach&gt; Demo</title>
  </head>
  <body>
    <h1>&lt;x:forEach&gt; Demo</h1>
    <c:import 
      url="http://localhost:8080/JSTL-Tag-Reference/tag-types/xml/citizens.xml" 
      var="citizenXML"/>
    <x:parse var="doc" xml="${citizenXML}" />
    <table border="1">
    <x:forEach var="citizen" select="$doc/citizens/citizen">
        <tr>        
      <td><x:out select="ssn" /></td>
         <td><x:out select="firstname" /></td>
      <td><x:out select="lastname" /></td>
      <td><x:out select="role" /></td>
          <td><x:out select="salary"/></td>                        
        </tr>        
   </x:forEach>
  </table>
  </body>
</html>
 

XML Document (given for reference):

<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<citizens>
    <citizen>
        <ssn>Z345T</ssn>
        <firstname>Cheryl</firstname>
        <lastname>Johnson</lastname>
        <role>Manager</role>
        <salary>12000</salary>
    </citizen>
    <citizen>
        <ssn>Z446T</ssn>
        <firstname>John</firstname>
        <lastname>Smith</lastname>
        <role>Employee</role>
        <salary>1000</salary>
    </citizen>
    <citizen>
        <ssn>Z335T</ssn>
        <firstname>Justin</firstname>
        <lastname>Claire</lastname>
        <role>Senior Manager</role>
        <salary>14000</salary>
    </citizen>
    <citizen>
        <ssn>Z389T</ssn>
        <firstname>Clark</firstname>
        <lastname>Rick</lastname>
        <role>Employee</role>
        <salary>2000</salary>
    </citizen>
</citizens>
 

Output:



x-for-each

 

Recommended Usage of <x:forEach> tag:

The <x:forEach> tag is the XML counterpart of the core iteration tag. This tag is used to iterate over nodes and to display node values. This tag also has begin, end, step and varStatus attributes to control the iteration behavior.

 

Other JSTL XML Tags:

if  |  out  |  param  |  parse  |  set  |  transform  |  choose, when, otherwise


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#2abc2019-12-11 01:01
Strut1Sample

Login.jsp




action

org.apache.struts.action.ActionServlet


config
/WEB-INF/struts-config.xml

2



action
*.do
Quote
#1Quan Hoang2019-12-10 02:35
Need use xalan 2.7.2 and xml-apis 1.4.1 to work.
Thanks
Quote