If you have an LXP region that you wish to iterate more than once, the
<for> tag exists for this purpose. It requires at least a
start attribute, and either an
endbefore attribute. Each attribute should be given a numeric value.
start attribute defines a whole integer value to begin the loop
iteration with. That value initializes an iteration count, which will be incremented by 1 for
each iteration of the loop. If the
end attribute is defined, the loop will
stop iterating after the iteration count has looped through the number
end. Alternatively, if the
attribute is defined, the loop will stop one iteration earlier. Using
endbefore is respectively equivalent to using the
< operators in a programming language such as PHP
While iterating, a special LXP object called
for maintains a value
count, which stores the value of the current loop’s iteration count.
Example 13-19 demonstrates a simple for loop that
will iterate from 1 to 5.
Example 13-19. A simple <for> loop
<lxp> <for start="1" end="5"> Iterating loop: <putvar name="for.count" /><br /> </for> </lxp>
Here is the output from this loop, when processed by LXP:
Iterating loop: 1<br /> Iterating loop: 2<br /> Iterating loop: 3<br /> Iterating loop: 4<br /> Iterating loop: 5<br />
<for> loop iterator can be invaluable when dealing with arrays of values that you need to return by using LXP. As mentioned earlier in this chapter, if a variable is defined with trailing ...