The <foreach> element allows a VoiceXML application to iterate through an ECMAScript array and to execute the content contained within the <foreach> element for each item in the array.





An ECMAScript expression that must evaluate to an array; otherwise, an error.semantic event is thrown.


The variable that stores each 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.


Both “array” and “item” must be specified. Please refer to the VoiceXML 2.1 Draft for examples and detailed usage information for this tag.


<?xml version="1.0"?>
<vxml version="2.1">
var movies = new Array();
movies[0] = new Object();
movies[0].audio = 'godfather.wav';
movies[0].tts = 'The Godfather';
movies[1] = new Object();
movies[1].audio = 'highfidelity.wav';
movies[1].tts = 'High Fidelity';
movies[2] = new Object();
movies[2].audio = 'raiders.wav';
movies[2].tts = 'Raiders of The Lost Ark';
<field name="mov">
Please select one of the following movies.
<break time="500ms"/>
<foreach item="movie" array="movies">
<audio expr="movie.audio"><value expr="movie.tts"/></audio>
<break time="500ms"/>
<grammar mode="voice" type="application/srgs+xml" root="ROOT">
<rule id="ROOT">
<item>The Godfather</item>
<item>High Fidelity</item>
<item>Raiders of The Lost Ark</item>
You said <value expr="mov"/>.

The output of the above script would be:

Computer: Please select one of the following movies. Computer: The Godfather, High Fidelity, Raiders of The Lost Ark. Human: High Fidelity. Computer: You said High Fidelity.

Child Tags

<audio>, <assign>, <break>, <clear>, <data>, <disconnect>, <emphasis>, <enumerate>, <exit>, <foreach>, <goto>, <if>, <log>, <metadata>, <mark>, <paragraph>, <phoneme>, <prompt>, <prosody>, <reprompt>, <return>, <say-as>, <sentence>, <script>, <speak>, <sub>, <submit>, <throw>, <value>, <var>, <voice>

Parent Tags

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