Scope in VoiceXML

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:

session
read only variables pertaining to an entire user session. These variables are declared by the platform and cannot be set within VoiceXML documents.
application
declared by the <var> element as children of the root applications <vxml> tag (declared directly under this tag and no other). They exist while the root document is loaded and can be accessed at any level within any document in the application.
document
declared as children of a supporting document’s <vxml> tag. They are initialized upon loading the supporting document and may be accessed only within that document.
dialog
declared as children of <form> or <menu>, these variables are accessible only within that dialog element and are initialized when the form is visited. If declared inside of executable content, initialization occurs when the content is executed. Form/field item variables initialize as the form item is collected (see Tutorials1 & 2).
(anonymous)
Each <block>, <filled>, and <catch>* element defines a new anonymous scope in which variables may be declared.

Declaring variables

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" />