Template Design

Our design here is to create a named template that will take some arguments, and then act as a for loop processor. If you think about a traditional for loop, it has several properties:

One or more initialization statements

These statements are processed before the for loop begins. Typically the initialization statements refer to an index variable that is used to determine whether the loop should continue.

An increment statement

This statement specifies how the index variable should be updated after each pass through the loop.

A boolean expression

If the expression is true, the loop continues; if it is ever false, the loop exits.

Let’s take a sample from the world of Java and C++:

for (int i=0; i<length; i++)

In this scintillating example, the initialization statement is i=0, the index variable (the variable whose value determines whether we’re done or not) is i, the boolean expression we use to test whether the loop should continue is i<length, and the increment statement is i++.

For our purposes here, we’re going to make several simplifying assumptions. (Feel free, dear reader, to make the example as complicated as you wish.) Here are the shortcuts we’ll take:

  • Rather than use an initialization statement, we’ll require the caller to set the value of the local variable i when it invokes our for loop processor. This value is passed as a parameter, so it can be calculated by an XPath expression.

  • Rather than specify an increment statement such as i++, we’ll require the caller to ...

Get XSLT, 2nd Edition 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.