VoiceXML Variable Scoping
August 13th, 2008 by voxeojeremyr
Here in Support we get a fair amount of tickets asking about variables. Namely, if a variable is created in a form can it be accessed in the field of that form or in a different form? What I am talking about is called variable scoping. In VXML there are several different variable scopes. You might have heard of global and local variables with other programming languages. VXML, while not a programming language, is similar in how it approaches variables and how they can be accessed.
First lets get a proper definition for variable scope. The scope of a variable defines when and where the variable exists and may be accessed. When the VXML browser exits the scope where the variable is defined, the variable is out-of-scope and cannot be accessed. When a variable is in scope, it can be accessed by both VoiceXML and ECMAScript.
Here is a table of the different scope levels of variables in VXML:
| Scopes | Declared In | Initialized In | Where accessible | Becomes Unavailable |
|---|---|---|---|---|
| Session | By the VXML browser | At the beginning of a session | Anytime during the session | At the end of the session |
| Application | In the root document | When VXML browser loads the root document | Throughout the application | When the VXML browser leaves the root document |
| document | In the <vxml> document | When the VXML browser loads the document | Within the <vxml> element | When VXML browser leaves the document |
| dialog | In the <field> element of a <form> or <menu> element | When the VXML browser loads the element | Within the element | When the VXML browser leaves the element |
| anonymous | In a <block>, <filled> or <catch> element | When the VXML browser begins to interpret the element | Within the element | When the VXML browser leaves the element |
Here is an example of document scoped variable. As you can see it is accessible throughout the current VXML document.
<?xml version=”1.0″?> <vxml version=”2.0″>
<var name = “counter” expr = “0″/>
<form id=”F_1″> <!– additional VXML code –> <filled> <assign name=”counter” expr = “counter + 1″/> </filled> </form>
<form id=”F_2″> <!– additional VXML code –> <filled> <assign name=”counter” expr = “counter + 2″/> </filled> </form>
</vxml>
Here is an example of a dialog scoped document. In form “F_1″ the value of “counter” after the assign <element> would be 1. In form “F_2″ the value after the <assign> value would be 2.
<?xml version=”1.0″?> <vxml version=”2.0″>
<form id=”F_1″> <!– additional VXML code –> <filled> <var name = “counter” expr = “0″/> <assign name=”counter” expr = “counter + 1″/> </filled> </form>
<form id=”F_2″> <!– additional VXML code –> <filled> <var name = “counter” expr = “0″/> <assign name=”counter” expr = “counter + 2″/> </filled> </form>
</vxml>
It is also good to know that variable references will match the closest applicable scope. You can prefix a reference with a scope name, such as application.counter, for clarity or to resolve ambiguity.
We hope this clears up a little confusion about VXML variables and where they are defined and when they can accessed.
Jeremy Richmond VXML Certified Developer
Related posts:
- Accessing Web Services From VoiceXML
- VoiceXML for web developers: Hello World
- Code Walk: Listening to Identi.ca (OSCON 2008 Demo #2: VoiceXML)
- Demystifying VoiceXML Subdialogs
- Certified Tech Tip: Alpha-Numeric voice recognition grammars – part two
If you found this post interesting or helpful, please consider either subscribing via RSS, becoming a fan on Facebook, or following us on Twitter.
RSS Feed





August 13th, 2008 at 3:39 pm
[...] we had a wee bit of fun with HTML tables… Jeremy, a member of our support team, published a post on our Voxeo Developer’s Corner blog that had an HTML <table> in it. Now it turned out that this was the first time in the 10 [...]
October 12th, 2008 at 9:33 pm
[...] | user-saved public links | iLinkShare 2 votesVoiceXML Variable Scoping>> saved by jakedeherrera 2 days ago1 votesRegulatory T cells – between benefical and damaging [...]