Loop Iteration
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 end
or endbefore
attribute. Each attribute should be given a numeric value.
The 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
specified by end
. Alternatively, if the endbefore
attribute is defined, the loop will stop one iteration earlier. Using end
and endbefore
is respectively equivalent to using the <=
and <
operators in a programming language such as PHP
or C.
While iterating, a special LXP object called for
maintains a value
called 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 />
The <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 ...
Get Practical PostgreSQL now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.