VoiceXML 2.1 Development GuideHome  |  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
vanguill
11/11/2008 10:21 AM (EST)
Hi,

Thanks in advance for any advice you can offer.

I have a vxml file, seaslugs.vxml which is functioning as my document root.  In seaslugs.vxml root file, I declare a variable with application scope (appSlugColor). 

I gather a value for appSlugColor variable from a user inside of a slugColor field of a form getSlugInfo form within the root document.  Inside of the <filled> tag corresponding to the slugColor field, I assign  the value gathered from the user to the application variable appSlugColor, using <assign name="application.appColor" expr="slugColor"/>.  (I've also tried this from within script tags) Immediately following that assignment, I submit a couple of values (slugColor and slugShell) to a cgi script, which looks up the slug color in a MySQL database and returns a list of slugs of that color. After retrieving the slugs from the database, the cgi writes out a new vxml file (in which seaslugs.vxml is declared as the application)  which tells the user what slugs were retrieved and then then redirects the user back to another form (called goodByeSSSS) within the original application root, seaslugs.vxml, using a <goto> tag.  This all works, so what's my problem?

Well, it is that the value of the appSlugColor variable is lost somewhere after the post and before getting back into the form at #goodbyeSSSS.

Is what I am attempting to do completely wrong, or is this a syntax or scope question?

I've include the seaslugs.vxml source below.  This is a learning exercise for me, so any advice will be appreciated.

Thanks,
Linda
----------
<?xml version="1.0"?>
<vxml version="2.1" xmlns="http://www.w3.org/2001/vxml">

<property name="universals" value="help"/>

<var name="myAppName" expr="'Sea Slug Survey System'" />
<var name="appSlugColor" />


<link next="#welcomeSSSS">
<grammar type="application/x-nuance-gsl" mode="voice">[main back begin]</grammar>
</link>
<link next="#goodbyeSSSS">
<grammar type="application/x-nuance-gsl" mode="voice">[exit quit bye goodbye]</grammar>
</link>


<form id="welcomeSSSS">
  <block>
      <audio src="Welcome.wav"/>
      <prompt> the <value expr="application.myAppName"/>!</prompt>
    </block>
 

  <field name="needHelp">
<prompt>Do you require help identifying your slug?</prompt>
<grammar type="application/x-nuance-gsl" mode="voice">
<![CDATA[
;Match one of the enclosed terms
[
;Each line is recognized as a separate term
;Terms are separated by a space
yes
no
;Match either yes yep si or oui
;Square brackets are the same as OR
[yes yep wee see]
;Parentheses require all of the enclosed terms to be matched
(yes please)
(no thanks) 
]
]]>
</grammar>

<!-- The user was silent, restart the field -->
<noinput>
I did not hear you, please speak up.
<reprompt/>
</noinput>
 

<!-- The user said something that was not defined in our grammar -->
<nomatch>
I do not understand those words.  Please try again.
<reprompt/>
</nomatch>

<!-- The user requested help -->
<help> Please say, yes please, or, no thanks. </help>

  </field>
  <filled>
<if cond="needHelp == 'yes'">
  <prompt>I'm happy to help!</prompt>
  <goto next="#slugIdentHelp"/>
<elseif cond="needHelp == 'yes please'"/>
  <prompt>You need polite help.</prompt>
  <goto next="#slugIdentHelp"/>
<elseif cond="needHelp == 'no'"/>
  <prompt>You don't think you need help, but you will.</prompt>
  <goto next="#getSlugInfo"/>
<elseif cond="needHelp == 'no thanks'"/>
  <goto next="#getSlugInfo"/>
<else/>
  <prompt>
You don't even know whether you need help. Let me tell you a few things. The caller i d number is <value expr="session.callerid"/>
      The called i d of this application is
            <say-as interpret-as="telephone">
            <value expr="session.callerid"/>
            </say-as>
  </prompt>
  <goto next="#welcomeSSSS"/>
</if>
  </filled>
</form>


<form id="slugIdentHelp">
  <block><prompt>Okay.  Let's walk through a few questions.</prompt></block>

  <field name="slugShell" slot="sShell">
<prompt>
  Does your slug have a shell?
</prompt>
<grammar type="application/x-nuance-gsl" mode="voice">
<![CDATA[[
[yes no]
]]]>
</grammar>
<filled>
  <if cond="slugShell == 'yes'">
  <prompt>I'm sorry, I am not yet able to classify slugs
with shells.  This feature will be available in
January of 2009.  Returning to main menu.
  </prompt>
  <goto next="#welcomeSSSS"/>
    <elseif cond="slugShell == 'no'"/>
<prompt>  Okay then. </prompt>
  </if>
</filled>
  </field>


<field name="slugColor" slot="sColor">
<prompt>
  What color is your slug?
</prompt>
<grammar type="application/x-nuance-gsl" mode="voice">
<![CDATA[[
  (?[light dark] [red]) { <sColor "red">  }
  [orange] { <sColor "orange">  }
  (*[light lemon] [yellow]) { <sColor "yellow"> }
  [green] { <sColor "green"> }
  [purple violet] { <sColor "purple"> }
  [pink fuscia] { <sColor "pink">  }
  [brown] { <sColor "brown"> }
  [white] { <sColor "white"> }
  (+[light dark baby royal] [blue]) { <sColor "blue"> }
  [black] { <sColor "black">  }
]]]>
</grammar>
<nomatch>
I do not know that color. Please say: red, orange, yellow,
green,light blue, dark blue, purple, violet, pink, brown,
white, or black.
  <reprompt/>
</nomatch>
  </field>


<filled>
            <script>
      application.appSlugColor = 'fyoosha';
            </script>
  <prompt>
        I just assigned <value expr="application.appSlugColor"/> too appSlugColor. 
  Checking database for
<value expr="slugColor$.utterance"/>
slugs with index term
<value expr="slugColor$.interpretation.sColor"/>.
  </prompt>


          <submit next="http://www.linguistland.net/~lcvg/cgi-bin/checkColors.cgi"
                  namelist="slugShell slugColor"  method = "get" />
  <goto next="#goodbyeSSSS"/>
</filled>



</form>



  <form id="getSlugInfo">
<block><prompt>Great.</prompt></block>
<field name="slugColor">
  <prompt>
    What is the order of your slug?
  </prompt>
  <grammar type="application/x-nuance-gsl" mode="voice">
<![CDATA[[
headshield
sapsucking
seahare
sidegill
nudibranch
noodibrank
]]]>
  </grammar>

</field>
  </form>

  <form id="goodbyeSSSS">
    <block>
<prompt>
  Thank you for caring about <value expr="application.appSlugColor"/>. sea slugs.  Goodbye.
</prompt>
    </block>
</form>
</vxml>
 

login
  Variable Syntax  |  TOC  |  Session and Application Variables  

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