<X: forEach> tag
Next: <x: choose>, < x: when>, <x: otherwise> tag
<X: forEach> tag is used to loop through the nodes of an XML document.
Syntax
<x:forEach var="<string>" select="<string>" begin="<int>" end="<int>" step="<int>" varStatus="<string>">
Attributes
<X: forEach> tag has the following attributes:
Attributes | description | If necessary | Defaults |
---|---|---|---|
select | XPath expression to be calculated | Yes | no |
var | Variable is used to store the current project | no | no |
begin | Start index iterator | no | no |
end | End index iterator | no | no |
step | Iteration step | no | no |
varStatus | Status of variables stored on behalf of iterator | no | no |
Examples Demo
<%@ 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>
Results are as follows:
BOOKS INFO: Book Name: Padam History Book Name: Great Mistry
Next: <x: choose>, < x: when>, <x: otherwise> tag