The Velocity foreach element allows you to do looping in Velocity.

The Velocity foreach element can be used to select every XML element of a specified node-set:  See the Apache Velocity Foreach section for details

The  $velocityCount variable can be used to access the current loop counter varialbe for display or you can use the method $foreach.count.

You can also use the #break directive to exit the loop early given that a particular condition is met.


<h2>My CD Collection</h2>
<table style="border: 3px solid #450084">
<tr style="background:#ccc999">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
</tr>
##LOOP
#set($cds = $_XPathTool.selectNodes($contentRoot, '//catalog/cd'))
#foreach($cd in $cds)
#set ($artist = $cd.getChild('artist').value)
#set ($title = $cd.getChild('title').value)
<tr>
<td>$title</td>
<td>$artist</td>
</tr>
#end
##END LOOP
</table>



My CD Collection

Title Artist
Empire Burlesque Bob Dylan
Hide your heart Bonnie Tyler
Greatest Hits Dolly Parton
Still got the blues Gary Moore
Eros Eros Ramazzotti
One night only Bee Gees
Sylvias Mother Dr.Hook
Maggie May Rod Stewart
Romanza Andrea Bocelli
When a man loves a woman Percy Sledge
Black angel Savage Rose
1999 Grammy Nominees Many
For the good times Kenny Rogers
Big Willie style Will Smith
Tupelo Honey Van Morrison
Soulsville Jorn Hoel
The very best of Cat Stevens
Stop Sam Brown
Bridge of Spies T`Pau
Private Dancer Tina Turner
Midt om natten Kim Larsen
Pavarotti Gala Concert Luciano Pavarotti
The dock of the bay Otis Redding
Picture book Simply Red
Red The Communards
Unchain my heart Joe Cocker


Back to Top