foreach

foreach allows simple loops based on an XPath expression.

Syntax
<foreach> ::= "foreach" <quote> <xpath expression> <quote> "{" <sort>? <block elements>+ "}"
Options
xpath An XPath expression defining the list of loop conditions (required).
Elements

Although there are no required elements within the block, it is meaningless to have a proc that is null and does not return any values (string characters).

Examples

For example consider the following.

foreach "*" { choose { when "name() = 'aw:include'" { variable chunkRef "@ref" call process-code { with chunkNode "//aw:chunk[@name = $chunkRef]" with name "$chunkRef" } } otherwise { value disable-output-escaping "." } } }

would compile to

<for-each select="*"> <choose> <when test="name() = 'aw:include'"> <variable name="chunkRef" select="@ref"/> <call-template name="process-code"> <with-param name="chunkNode" select="//aw:chunk[@name = $chunkRef]"/> <with-param name="name" select="$chunkRef"/> </call-template> </when> <otherwise> <value-of select="." disable-output-escaping="yes"/> </otherwise> </choose> </for-each>
Errors

Forgetting the XPath expression.

foreach { choose { when "name() = 'aw:include'" { variable chunkRef "@ref" call process-code { with chunkNode "//aw:chunk[@name = $chunkRef]" with name "$chunkRef" } } otherwise { value disable-output-escaping "." } } }

will give the error

**** (130) Missing expression after "foreach" in line 5 Insert expression.

Similarly forgetting the open block bracket as well:

stylesheet { version "1.0" proc process-code { foreach choose { when "'aw:include'" { variable chunkRef "@ref" call process-code { with chunkNode "//aw:chunk[@name = $chunkRef]" with name "$chunkRef" } } otherwise { value disable-output-escaping "." } } } } }

reports

**** (130) Missing expression after "foreach" in line 5 Insert expression. **** (106) Unexpected symbol "choose" in line 6 Check spelling, did you mean start of block bracket?

Note that the sort statement has to be the first in the block.

stylesheet { version "1.0" proc process-code { foreach "*" { choose { when "'aw:include'" { variable chunkRef "@ref" call process-code { with chunkNode "//aw:chunk[@name = $chunkRef]" with name "$chunkRef" } } otherwise { value disable-output-escaping "." } } sort ascending } } }

reports

**** (141) Sort in "foreach:*" in line 18 must be at start of block. Check order.
Copyright 2024 Hugh Field-Richards. All Rights Reserved.