<for> Example
You must define the counter variable and the beginning and final values for the counter. The counter limits can be integers or XPath 2.0 expressions that evaluate to integers. The loop increment is always one.
Note: | The counter variable in <for> is implicitly declared and has a local scope of the <for> command only. As with other programming languages, you can use the counter variable name in expressions used in statements within the body of <for>. |
One common use of <for> loops is to repeatedly append content to a variable, as shown in the following example:
<variables>
<variable name="reviewers" type="document"/>
<variable name="compositeResult" type="document"/>
</variables>
...
<for variable="$i" startcountervalue="1"
finalcountervalue="count($reviewers//reviewer)">
<display message="reviewer is " variable="$reviewers//reviewer[$i]/name"/>
<appendresult outputvariable="$compositeResult">
<res:ratingBy>
<res:name>{string($reviewers//reviewer[$i]/name)}</res:name>
<res:rating>{string($reviewers//reviewer[$i]/rating)}</res:rating>
</res:ratingBy>
</appendResult>
</for>