| VoiceXML 2.1 Development Guide | Home | Frameset Home |
<foreach> element, which allows the developer an inherent browser method for looping through items in an array, and outputting them via TTS, or via <audio>. This element must specify both the 'array', as well as the 'item' attributes, else an error.semantic will be thrown.| array | Data Type: (ECMAScript Expression) | Default: Required |
The 'array' attribute defines the name of the array referenced by the <foreach> element,; failure to specify this attribute will result in an error.semantic event being thrown. | ||
| item | Data Type: NMTOKEN | Default: Required |
The 'item' attribute denotes the name of the variable that stores a particular array value as it is being iterated within the loop. If this variable is not already defined, then it will be declared upon invocation of the <foreach> element. | ||
| <?xml version="1.0" encoding="UTF-8"?> <vxml version = "2.1"> <meta name="author" content="Matthew Henry"/> <meta name="copyright" content="2005 voxeo corporation"/> <meta name="maintainer" content="YOUR_EMAIL@HERE.COM"/> <form id="F1"> <block> <prompt> welcome to the gaming news phone network. </prompt> <script> var arrayGames = ["duke nukem forever", "half life 2", "doom 3", "shadow warrior 2", "track and field: grecian edition" ]; </script> <prompt> The most anticipated games for this year are <break/> </prompt> <foreach item="games" array="arrayGames"> <prompt><value expr="games"/><break/></prompt> </foreach> <prompt> But of course, we all know that Duke Nukem Forever is never, ever, ever, going to be released, so gamers may as well pin their hopes on Miss Pac Man 3, or Command and Conquer: French Assault.</prompt> </block> </form> </vxml> |
| ANNOTATIONS: EXISTING POSTS |
mkoe
|
|
| Hi,
How can I use <subdialog> in a <foreach>? It seems the vxml doesn't allow to execute subdialog within <foreach>. I put my sample as the following. Thanks for your help. <form name="main"> <foreach item = "theCurrentSAVAccout" array = "SAVAccoutList"> <subdialog name="result" src="#SayAccountInformation"> <param name="currentMonth" expr="theCurrentSAVAccout.currentMonth" /> <param name="previousYear" expr="theCurrentSAVAccout.previousYear" /> <param name="isJanToMay" expr="theCurrentSAVAccout.isJanToMay" /> <filled /> </subdialog> </foreach> </form> <form name="theCurrentSAVAccout"> <block> </block> </form> When I run the above code in IBM VoiceToolkit, I am getting error --------------------------------------------------------- INFO: Call is connected. ERROR - Cause: VXML Document error: and form the system error log. [7/4/06 11:20:21:250 EDT] 254cc570 VXML E 3006509539553984@127.0.0.1 TRAS0014I: The following exception was logged com.ibm.voice.server.vc.VContainerException: VContainer::dialogRun at com.ibm.voice.server.vc.core.VContainer.dialogRun(VContainer.java:389) at com.ibm.voice.server.vxp.VXPRuntime$RunWorker.runWork(VXPRuntime.java:747) at com.ibm.voice.server.vxp.VXPWorker.run(VXPWorker.java:84) at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:912) Caused by: com.ibm.vxi.intp.BrowserProcException: VXIContext::interpret: at com.ibm.vxi.intp.VXIContext.interpret(VXIContext.java:512) at com.ibm.vxi.intp.VoiceBrowser.run(VoiceBrowser.java:570) at com.ibm.voice.server.vc.core.VContainer.dialogRun(VContainer.java:383) ... 3 more Caused by: error.badfetch:com.ibm.vxi.intp.CatchEvent: file:/C:/myViews/snapshots/MKOE_VoiceXML/vxml/ITSWebProject/WebContent/main/vxml/interestEarned.vxml:VXML Document error: Line : 353 Column: 18 URI : null <SNIP> |
|
MattHenry
|
|
|
Hello there, I think that there is a bit of confusion that I should clear up. Checking the parent-child listing for theelements in question, <subdialog> is not a valid child of <foreach>, so this is not a valid option. You could however, code a foreach loop on the serverside, and output your subdialogs as needed. ~Matt |
|
dragondad
|
|
| Can I use the <foreach> recursively to support two dimensional array assignment.
For example: <foreach item="games" array="arrayGames"> <prompt><value expr="games"/><break/></prompt> <foreach item="scores" array="arrayScores"> <prompt><value expr="scores"/><break/></prompt> </foreach> </foreach> this looks like a valid script, but will the result as expected. |
|
MattHenry
|
|
|
Hi there, I haven't tested this personally, but I see no reason why this wouldn't work. If you want to give me a sample array that is specific to this topic, I can run a test and find out, though...let me know. ~Matt |
|
shawnaslam1
|
|
| My question is once we have assigned the array string to the VXML array now can we access the specific index of the VXML array as we can do in any language or in JSP.
It seems to me that when we use array in foreach loop it plays the array and didn't give us any control so is there any way in we each we can access the specific index of the array. your spontaneous action will be great for me. Regards, Shawn Aslam |
|
MattHenry
|
|
|
Hi Shawn, What you are looking to do in this case exceeds what the <foreach> element was designed to do. If you want finer-grained control over accessing individual index positions and their associated values, then you would likely be better off doing this by crafting an ECMAScript function to achieve this goal. Hope this helps, ~Matthew Henry |
| login |