VoiceXML 2.1 Development GuideHome  |  Frameset Home

  E: VoiceXML Variables  |  TOC  |  Variable Scoping  

Variables


VoiceXML variables follow the standard ECMAscript rules, and may contain any characters with the exception of the following circumstances:

A variable may NOT begin with an underscore '_', or end with the dollar sign '$' characters. In addition, we may not have a period ‘.' or dash '-' contained within them, ie, the following examples will cause an error.semantic to be thrown:

            <var name="_myVar"/>
            <var name="myVar$"/>
            <var name="my.Var"/>
            <var name="my-Var"/>


Note that declaring a user variable via the <var> element is equivalent to declaring a variable using client-side scripting within a block of ECMAScript. As such, the two declarations below accomplish entirely the same goal:


<block>
<var name="myVar" expr="'some value'"/>

<script>
  var myVar = 'some value';
</script>

</block>



Be aware that variables used for TTS or <log> statements must be defined as string literals to function properly:

            <var name="myVar" expr=" ' Eat Canned Ham ' "/>
            <log expr="myVar"/>



Variable Declaration

Note that in older, non-compliant VoiceXML browsers, we could get away with assigning a variable a value without explicitly declaring it first:

<form id="F1">
  <block>
    <assign name="myVar" expr="'four twenty'"/>
    <prompt> hey, its <value expr="myVar"/> </prompt>
  </block>


But proper coding ethic dictates that any variable must be declared before assignation can take place, which must take place when executing VoiceXML application content on the Prophecy platform, else an error.badfetch will be thrown:

<form id="F1">
  <block>
  <var name="myVar"/>
    <assign name="myVar" expr="'four twenty'"/>
    <prompt> hey, its <value expr="myVar"/> </prompt>
  </block>


Making a habit out of explicitly declaring all variables will result in code which is much more easily ported across multiple platforms.




  ANNOTATIONS: EXISTING POSTS
pipn
9/14/2006 12:39 PM (EDT)
How can I append data to a variable?

As an example, in C# it would be something like:

Var1 = "Hello";
Var1 = Var1 + " World!";

Results: "Hello World!"

I am trying to do something similar with variables. I have two forms with condition logic. Form1 saves a value to a variable called UpdateText. Then I need Form2 to append a value to the already existing data in the variable UpdateText.

--Form1
<assign name="UpdateText" expr="Hello">

--Form2
<assign name="UpdateText" expr=UpdateText + " World!">

I know that is the wrong syntax but how would I accomplish this?

Thanks
Michael.Book
9/14/2006 3:00 PM (EDT)
Hello All,

The 'expr' attribute value is an ECMAScript expression, so you would use something like:
__________________

--Form1
<assign name="UpdateText" expr="'Hello'">

--Form2
<assign name="UpdateText" expr="UpdateText + ' World!'">
__________________

The single quotes here denote a string value.  Without single quotes references an existing variable.

I hope this helps...


Have Fun,

~ Michael

pipn
9/14/2006 5:23 PM (EDT)
Thanks Michael that fixed it. Another problem I ran in to is trying to pass a variable as a grammar.

As an example, asking the user for their access word to continue. Before asking them I would query a database to get their access word and pass it to a variable. I tried putting the variable in the grammar check but it keeps failing.

<grammar type="text/gsl">
  [(<value expr="AccessWord"/>)]
</grammar>
MattHenry
9/14/2006 7:27 PM (EDT)


Hi there,

To be clear, using VXML variable syntax inside of a grammar is entirely disallowed. I might suggest that you review our tutorial on dynamic grammars to get an idea on how you can isert DB values dynamically into a grammar file:

http://docs.voxeo.com/voicexml/2.0/t_16.htm

~Matt

login
  E: VoiceXML Variables  |  TOC  |  Variable Scoping  

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