VoiceXML 2.1 Development Guide Home  |  Frameset Home

  Variable Syntax  |  TOC  |  Session and Application Variables  

Variable Scoping

Declared variables will inherit the scope of their parent element by default, unless explicitly being assigned a scope, (see below). Thus, variables declared at the <form> level will be given dialog scope, while variables declared at the <vxml> level will be given document scope. Variables declared at the <vxml> level within the application root document will inherit application scope.

Note that when using subdialogs, that it is best practice to declare any application variables as a local,  form-scoped variable rather than passing an application variable to the subdialog file via the <param> element. While both methods will, in fact, work, sending an application scoped variable to a subdialog will result in the logger message of ‘application is not defined'.

Application Scoped Variables

Application scoped variables are declared in the application root document, and assigned 'application' scope within a leaf document. These variables are considered active in any child document that references the App root. Application variables follow the naming convention of ‘application.VarName'.

Document Scoped Variables

These variables are declared and initialized as children of the vxml element within a non-root document. Document scoped variables are considered active throughout all forms within the current document. The naming syntax of ‘document.VarName' is considered good coding practice, but is not wholly necessary, as variables declared in this manner assume this value inherently.

Dialog Scoped Variables

A dialog scoped variable is considered on which is declared within a form, and will only be considered active throughout the form in which it is declared. For instance, a variable declared in the first <form> of a document will not be available within the second <form>, unless it is passed along via a <submit>, or if it is explicitly assigned a higher scope while within the <form>.

Anonymous Scoped Variables

Variables of anonymous scope are variables which are declared within  form items, such as within a <block> or within a <field>. They will only be considered active when the application visits and executes these form items.

Scope Prefixes

Note that in the example provided, we can also use the scope prefixes to clearly define which scope the variable should be using when accessed. This is useful when a script contains a single variable name which changes values depending on its location in the code.

          <value expr="application.MyVar"/>
          <value expr="document.MyVar"/>
          <value expr="dialog.MyVar"/>


Page1.vxml -- (document/dialog example)

<?xml version="1.0" encoding="UTF-8"?>

<vxml version="2.0">

<meta name="maintianer" content="yourEmail@here.com"/>
  <var name="DocumentVar" expr="'document'"/>

  <!--  IT IS IMPORTANT TO REMEMBER *NOT* TO ASSIGN ANY VARIABLES
        WITH AN 'application' PREFIX WITHIN THE ROOT DOCUMENT ITSELF,
        ELSE AN ERROR.SEMANTIC WILL BE THROWN:

      <var name="application.ApplicationVar" expr="'application'"/>

  -->


      <var name="ApplicationVar" expr="'application'"/>


  <form id="F1">
    <var name="DialogVar" expr="'dialog'"/>

    <block>
      <prompt>
        here we have  a <value expr="DialogVar"/> scoped variable.
      </prompt>

      <goto next="#F2"/>
    </block>
  </form>

  <form id="F2">
    <block>
      <var name="AnonVar" expr="'anonymous'"/>
      <prompt>
          Our anonymous scoped variable is available here,
          <value expr="AnonVar"/>, but our dialog variable will equate
          to undefined. However, our <value expr="DocumentVar"/>
          scoped variable is still considered active.
        </prompt>

    <!--
    IF WE OUTPUT THIS VARIABLE WITHIN THIS SCOPE,
    WE WILL GET AN ERROR.SEMANTIC, AS THIS VAR
    IS NO LONGER AVAILABLE

    <log expr="DialogVar"/>
    -->


        <goto next="Page2.cfm"/>

    </block>
  </form>
</vxml>



Page2.vxml


<?xml version="1.0" encoding="UTF-8"?>

<vxml version="2.0" application="Page1.cfm">

<meta name="maintianer" content="yourEmail@here.com"/>

  <form id="F1">
    <block>
      <prompt>
        and here our <value expr="application.ApplicationVar"/> scoped
        variable is available throughout the entire application. But our anonymous
          scoped variable  and our dialog scoped variable are no longer available.
      </prompt>

    <!--
    IF WE OUTPUT THESE VARIABLES WITHIN THIS SCOPE,
    WE WILL GET AN ERROR.SEMANTIC, AS THESE VARS
    ARE NO LONGER AVAILABLE

    <log expr="Anon"/>
    <log expr="DialogVar"/>

    -->

    <log expr="DocumentVar"/>

    </block>
  </form>
</vxml>




  ANNOTATIONS: EXISTING POSTS
jongmagno
4/5/2006 5:16 PM (EDT)
how do you assign a value to a variable from a user input?
mikethompson
4/5/2006 5:24 PM (EDT)
Hi Jong,

You can assign values to user input simply by using slot values within your grammars.  I could hit you with some example code but I see a perfect example located in our GSL documentation.  You can find that here:  http://docs.voxeo.com/voicexml/2.0/gslmultislot.htm

This will give you an in depth look at Multi-slot grammars.

Hope this helps,
Mike Thompson
Voxeo Extreme Support
appalachiangypsy
7/29/2006 10:38 PM (EDT)
This tutorial is awesome.  I found the info I needed.  Thanks.
bsmith
7/31/2006 8:00 PM (EDT)
Thank you for the feedback!  We always appreciate what our customers have to say.

Thanks again,

Ben Smith
Voxeo Corporation
SSA_MarkPMiller
9/12/2006 12:24 PM (EDT)
Simple typo in example code:

The second document references a non-existant root application document. The root doc is named "Page1.vxml" and the second page references 'application="Page1.cfm"'

Also, the goto in the first doc references a .cfm file when it should be a .vxml file as named in the code.

Mark

mikethompson
9/12/2006 1:32 PM (EDT)
Mark,

Good catch! :)

This is defintely a mix up.  I imagine Matt was using .cfm for no-caching headers when he was building the application for this tutorial.  I'll certainly let him know of this slip up here and have him make any changes needed.

Best,
Mike Thompson
Voxeo Corporation
sarika
3/23/2007 1:22 AM (EDT)
Hello,
How can i access vxml declared variable in php code or how can i access php variable in voicexml code within the same document?
raghu.ram
9/21/2007 6:04 AM (EDT)
looking answer for thr above question.....
-rag

login

  Variable Syntax  |  TOC  |  Session and Application Variables  

© 2003-2008 Voxeo Corporation  |  Voxeo IVR  |  VoiceXML & CCXML IVR Developer Site