# <foreach>

Description

(VoiceXML 2.1 feature) Iterates through the given ECMAScript array and repeats the enclosed content. Note that `<foreach>` will not create its own scope; all ECMAScript operation within the tag will be done in the parent's scope.

Supported as a VoiceXML 2.1 feature.

Syntax

    <foreach
        array="ECMAScript_Expression"
        item="ECMAScript_Variable">
      child elements
    </foreach>

Attributes

Attribute | Description
array | An ECMAScript expression to be evaluated and used as the array. Required.
item | An ECMAScript variable that will store the array item upon each iteration of the loop. A new variable will be declared if it is not already defined within the parent's scope. Required.

Attribute Notes

- If the `array` expression does not evaluate to an ECMAScript Array, an `error.semantic` will be thrown.

Parents

<block>, <catch>, <error>, <filled>, <foreach>, <help>, <if>, <noinput>, <nomatch>, <prompt>

Children

<assign>, <audio>, <clear>, <data>, <disconnect>, <enumerate>, <exit>, <foreach>, <goto>, <if>, <log>, <mark>, <prompt>, <reprompt>, <return>, <script>, <submit>, <throw>, <value>, <var>, Speech Markup (SSML), #PCDATA

Extensions

None.

Limitations/Restrictions

- If `<foreach>` is not used as a child of `<prompt>`, it cannot contain `<enumerate>`, `<mark>`, or Speech Markup (SSML).
- IIf `<foreach>` is used as a child of `<prompt>`, it can only contain:
  - `<audio>`
  - `<enumerate>`
  - `<foreach>`
  - `<mark>`
  - `<value>`
  - Speech Markup (SSML)
  - #PCDATA

Example

    <?xml version="1.0"?>
    <vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">
      <form>
        <block>
          <var name="numbers" expr="new Array(1, 2, 3, 4, 5)"/>
          <foreach array="numbers" item="i">
             The square of <value expr="i"/> is <value expr="i*i"/>.
          </foreach>
        </block>
      </form>
    </vxml>