Loop Processing
DPML provides a single loop instruction, while.
while
The structure of the while
statement is:
<while>
<cond>
<instr />
</cond>
<seq>
<instr />
</seq>
</while>
When program flow enters the <while>
block the
<cond>
block is executed first.
If the execution of instructions in this block results in the
resource this:cond
being set to the value
true
then the instructions contained in the
<seq>
block are executed in order.
Next the <cond>
block is executed again
and the process continues until the resource
this:cond
is set to the value false.
As in any programming language, care should be taken not to create
an infinite loop!
This example DPML program illustrates the use of the while statement.
<idoc>
<!---->
<instr>
<type>copy</type>
<operand>
<inventory>
<item name="cheese" price="1.23" />
<item name="ham" price="1.50" />
<item name="brocolli" price="0.75" />
<item name="tiger roll" price="0.90" />
<item name="onion marmalade" price="3.45" />
</inventory>
</operand>
<target>var:inventory</target>
</instr>
<instr>
<type>copy</type>
<operand>
<basket />
</operand>
<target>var:basket</target>
</instr>
<while>
<cond>
<instr>
<type>xpatheval</type>
<operator>
<xpath>count(item)</xpath>
</operator>
<operand>var:inventory</operand>
<target>this:cond</target>
</instr>
</cond>
<seq>
<instr>
<type>copy</type>
<operand>var:inventory#xpointer(item[1])</operand>
<target>var:item</target>
</instr>
<if>
<cond>
<instr>
<type>xpatheval</type>
<operator>
<xpath>@price > '1.00'</xpath>
</operator>
<operand>var:item</operand>
<target>this:cond</target>
</instr>
</cond>
<then>
<instr>
<type>stm</type>
<operator>
<stm:group xmlns:stm="http://1060.org/stm">
<stm:append xpath="/basket">
<stm:param />
</stm:append>
</stm:group>
</operator>
<operand>var:basket</operand>
<param>var:item</param>
<target>var:basket</target>
</instr>
</then>
</if>
<instr>
<type>stm</type>
<operator>
<stm:group xmlns:stm="http://1060.org/stm">
<stm:delete xpath="item[1]" />
</stm:group>
</operator>
<operand>var:inventory</operand>
<target>var:inventory</target>
</instr>
</seq>
</while>
<instr>
<type>copy</type>
<operand>var:basket</operand>
<target>this:response</target>
</instr>
</idoc>