Scope defines the order of precedence for tags used in VoiceXML depending on where they are placed within an application. Variables, links, grammars, and event handling all utilize scope. For example, variables have five distince levels of scope:
The <var> element is used to declare variables with the mandatory name attribute to specify the variable name. The variable may optionally be initialized with a given value through the use of the expr attribute. Here is an example of declaring an application wide variable:
<?xml version="1.0" ?>
<vxml version="2.0">
<var name="myvar" expr="'hi'" />
<form id="sayHi">
<block>
<prompt>
I just wanted to say
<value expr="myvar" />
</prompt>
</block>
</form>
</vxml>
The above piece of code simply plays the prompt, "I just wanted to say hi." with text-to-speech (TTS). A variable may be played by TTS through the use of the <value> element by setting its expr attribute equal to the name of the variable. This allows the generation of dynamic TTS prompts. Setting a variable value
To set a variable to a new value, use the <assign> element with the name and expr attributes. For example, if you wanted to set the variable "myvar" equal to ten, you would use the following code:
<assign name="myvar" expr="10" />