English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

<x:forEach> tag

JSP Standard Tag Library

The <x:forEach> tag is used to iterate over the nodes of an XML document.

syntax format

<x:forEach
   var="<string>"
   select="<string>"
   begin="<int>"
   end="<int>"
   step="<int>"
   varStatus="<string>">

attribute

The <x:forEach> tag has the following attributes:

attributedescriptionwhether necessarydefault value
select the XPath expression to be calculated is
var variable used to store the current item
begin start index of the iterator
end end index of the iterator
step iteration step
varStatus representing the state stored by the iterator variable

示例演示

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
  <title>JSTL x:forEach Tag</title>
</head>
<body>
<h2>Books Info:</h2>
<c:set var="xmltext">
  <books>
    <book>
      <name>Padam History</name>
      <author>ZARA</author>
      <price>100</price>
    </book>
    <book>
      <name>Great Mistry</name>
      <author>NUHA</author>
      <price>2000</price>
    </book>
  </books>
</c:set>
<x:parse xml="${xmltext}" var="output"/>
<ul>
<x:forEach select="$output/books/book/name" var="item">
   <li>Book Name: <x:out select="$item" /></li>
</x:forEach>
</ul>
</body>
</html>

The running result is as follows:

BOOKS INFO:
Book Name: Padam History
Book Name: Great Mistry

JSP Standard Tag Library