Last reviewed: 3/23/2024 12:15:24 PM

<foreach>

A <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.

Syntax

<foreach array="expression"
     item="var" />

Attributes

array

An ECMAScript expression that must evaluate to an ECMAScript array.

item

The variable that stores each array item upon each iteration of the loop.

Children

<assign>, <audio>, <clear>, <data>, <disconnect>, <enumerate>, <exit>, <foreach>, <goto>, <if>, <log>, <prompt>, <reprompt>, <return>, <script>, <submit>, <throw>, <value>, and <var>.

Parents

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

Variables

none

Example

The following example illustrates using example elements.

<?xml version="1.0" encoding="UTF-8"?>
<vxml xmlns="http://www.w3.org/2001/vxml"
  version="2.1"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.w3.org/2001/vxml 
  http://www.w3.org/TR/2007/REC-voicexml21-20070619/vxml.xsd">

  <script src="movies.js"/>

  <form id="pick_movie">

    <!-- 
    GetMovieList returns an array of objects
      with properties audio and tts.
      The size of the array is undetermined until runtime.
    -->  
    <var name="prompts" expr="GetMovieList()"/>

    <field name="movie">
      <grammar type="application/srgs+xml" src="movie_names.grxml"/>

      <prompt>Say the name of the movie you want.</prompt>

      <prompt count="2">
        <audio src="prelist.wav">When you hear the name of the movie you want, just say it.</audio>
        <foreach item="thePrompt" array="prompts">
          <audio expr="thePrompt.audio"><value expr="thePrompt.tts"/></audio>
          <break time="300ms"/>
        </foreach>
      </prompt>

      <noinput>
        I'm sorry. I didn't hear you.
        <reprompt/>
      </noinput>

      <nomatch>
        I'm sorry. I didn't get that.
        <reprompt/>
      </nomatch>

    </field>
  </form>
</vxml>

Version Information

Supported in: VoiceXML 2.1.