Bug 7844 - <x:forEach> Doesn't Iterate Properly
Summary: <x:forEach> Doesn't Iterate Properly
Alias: None
Product: Taglibs
Classification: Unclassified
Component: Standard Taglib (show other bugs)
Version: unspecified
Hardware: Other Windows XP
: P3 normal (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
Depends on:
Reported: 2002-04-08 15:20 UTC by David Geary
Modified: 2004-11-16 19:05 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description David Geary 2002-04-08 15:20:33 UTC
I have this code ...

<x:forEach select="$addressBook/*"> 

... which produces this output ...


... for this XML file:

      <phone type="work">716-873-9644</phone>
      <phone type="home">716-834-8772</phone>
      <phone type="work">716-873-9644</phone>
      <phone type="home">716-834-8772</phone>

The output, as far as I can tell, should be ...


... because there are 2 addresses in the file.
Comment 1 Shawn Bayern 2002-04-08 15:35:12 UTC
No, <x:forEach> is behaving properly.

I think you're misunderstanding the mechanism.  You're apparently assuming that 
the scoped variable "addressBook" refers somehow to a tree where the context
node is the root element.  Instead, the variable refers to the entire parsed
tree.  Thus, the single "ADDRESS" that's printing corresponds to the single 
document root element:  <addressBook> ... </addressBook>.  If you want to
loop over each <address> ... </address> element, you'll need to use an 
expression like


This might look odd to you at first, but I'd assert that's just because you 
used the same name for the scoped variable and the root element of your 

To learn more about the mechanism, instead of printing just "ADDRESS," print a 
property of the context node (e.g., the tag name).  The behavior of <x:forEach> 
is demonstrated nicely in JSTL's standard-examples application.  (*Please* 
consult it before submitting bug reports.)  :-)  Thanks!