<x:forEach> Tag

JSP 标准标签库JSP Standard Tag Library

The <x:forEach> tag is used to loop through nodes of an XML document.

Syntax Format

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

Attributes

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

Attribute Description Required Default Value
select The XPath expression to be evaluated Yes None
var The variable used to store the current item no None
begin The start index of the iterator no None
end The end index of the iterator no None
step The step size of the iteration no None
varStatus The variable representing the state stored by the iterator no None


Example Demonstration

<%@ 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 标签</title>
</head>
<body>
<h3>Books Info:</h3>

<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 class="list">
<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 标准标签库JSP Standard Tag Library

Other Extensions