Skip to Main Content
Practical PostgreSQL
book

Practical PostgreSQL

by Joshua D. Drake, John C. Worsley
January 2002
Intermediate to advanced content levelIntermediate to advanced
640 pages
16h 39m
English
O'Reilly Media, Inc.
Content preview from Practical PostgreSQL

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 trailing ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PostgreSQL: Up and Running, 2nd Edition

PostgreSQL: Up and Running, 2nd Edition

Regina O. Obe, Leo S. Hsu
Learning PostgreSQL 11 - Third Edition

Learning PostgreSQL 11 - Third Edition

Christopher Travers, Andrey Volkov
Learn PostgreSQL

Learn PostgreSQL

Luca Ferrari, Enrico Pirozzi
PostgreSQL 13 Cookbook

PostgreSQL 13 Cookbook

Vallarapu Naga Avinash Kumar

Publisher Resources

ISBN: 9781449309770Supplemental ContentErrata Page