VoiceXML 2.1 Development Guide Home  |  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, i.e., 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'"/>



</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
mframpton
3/31/2009 9:35 AM (EDT)
I see that "A variable may NOT ... have a period '.' or dash '-' contained within them".
Unfortunately, I'm integrating with a system which relies on some variables of the format "Qn-n-nnnnnnnnn" to identify question numbers and I have no control over this format.
If I declare a var with a name like 'Q1-1-7268742', I get a gateway error. 
Do you see any workaround for this?
VoxeoDustin
3/31/2009 10:09 AM (EDT)
Hey,

You may be able to workaround this with a bit of server-side coding. You'd need to submit the data to a server-side script that would convert the variables to the syntax you need and send the request to your backend API from within the server-side.

Regards,
Dustin Hayre
Customer Support Engineer 2
Voxeo Support
mframpton
4/2/2009 5:47 AM (EDT)
Thanks Dustin, I now have that working OK. 
For anyone that's interested, code fragements are like this (I'm building up the html string to create the page):

html.append("<var name=\"QID\" expr=\"'" + qid.replace("-","_") + "'\"/>");
html.append("<submit next=\"redirect2.jsp\" namelist=\"QID ANS\"/>");

and then within redirect2.jsp
<% String qid = (String)request.getParameter("QID");
String mqid = qid.replace("_","-");
String ans = (String)request.getParameter("ANS"); %>
<jsp:forward page="theRealURL.jsp" >
    <jsp:param name="<%=mqid%>" value="<%=ans%>" />
</jsp:forward>
voxeoJeffK
4/2/2009 7:07 AM (EDT)
Hello,

Thank you for including that. We, and future developers thank you.

regards,
Jeff Kustermann
Voxeo Support
eman.elsawa
12/20/2009 10:31 AM (EST)
Hello

Hoow could i assign the variable a value that was taken from a field.I mean if the user speaks a number ,i need to assign this variable he number that was spoken
Regards
Eman
mikethompson
12/20/2009 4:45 PM (EST)
Hello Eman,

The field name (<field name="MyField">) actually contains the matched utterance result from the grammar.  However, if you want to access the slot value returned from the grammar, you'll need to access the interpretation variable like so:

lastresult$.interpretation.MyField

We have more information on how to do this in our documentation:

http://www.vxml.org/grxmlslots.htm

Hope this helps,
Mike Thompson
Voxeo Corporation
herocksit
2/9/2010 6:08 PM (EST)
What is the significance of '$' in "lastresult$"? Is it not possible to access without '$'?

jdyer
2/9/2010 8:05 PM (EST)
Hello,

  It's my understanding that you are asking about the significance of the $, and whether or not it is able to be omitted, is this correct?  If this is the case then I am afraid the answer is no, as this is the syntax for shadow variables (http://www.vxml.org/t_13.htm?search=shadow) in the VoiceXML markup.  Might we ask what you are trying to accomplish here, do you have a need to remove this character from your code?  Please do advise, as our team is most certainly standing by to be of service!

Regards,

John Dyer
Customer Engineer
Voxeo Support
Hixxey
3/12/2010 6:47 AM (EST)
Is it possible to make a variable global? So that when I go to another file such as a getPrices.JSP the varible will still have data within it when I return to the main.vxml.

Also, is it possible to store a recorded voice to a simple .mp3 or .wav?

Thanks,
Hixxey
voxeoJeffK
3/12/2010 8:14 AM (EST)
Hello,

If you specify a root document in a leaf file using the application attribute:

  <vxml version = "2.1" application="AppRoot.vxml">

all event handlers, variables, links, grammars, and scripts contained within the root document are considered active when each document loads. All variables declared within the root document are then considered to have application scope, and are available at any point in the application. To access or write to application scoped variables you will want to use the naming syntax of "application.VarName"

Concerning recording, you can record a user, utterance, or complete call. We have details available here:

  http://www.vxml.org/recording.htm

Please let us know if we can help with any other questions or clarifications.

Regards,
Jeff Kustermann
Voxeo Support
Hixxey
3/12/2010 9:17 AM (EST)
Thanks for that. I've now got the variable working and I will look over the recording section tomorrow.

I forgot to ask if it is possible and if so how can I write from a variable to a text file (test.txt)

Thanks,
Hixxey
VoxeoDante
3/12/2010 9:33 AM (EST)
Hello,

There is nothing in VoiceXML directly that will be able to write a variable to a file, but you should be able to do it with some serverside.  Here is a little PHP example that should be able to write the value of a variable called "name" that you pass to it into a text file called testFile.txt.

[code]
<?
$stringData = $_REQUEST["name"];
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);
?>
[/code]

I hope this helps.

Regards,
Dante Vitulano
Customer Support Engineer II
Voxeo Corporation
N0182209
3/19/2010 4:00 PM (EDT)
hi there,

i trying to call a jsp file from my main vxml program, i have the code below be once directed to the form the phone just goes quiet, i wondered if the code im using is correct, any help would be appreicate.

<form id="test">
<filled>
<submit next="salesec.jsp"/>
</filled>
</form>
jdyer
3/19/2010 4:22 PM (EDT)
Hello,

Is this sample code snippet complete, or just illustrative? If this is the actual offending code block then the issue is most likly that you have filled as a child of form, which is not valid.  You can wrap the submit in a block, which is a proper child of form.  If you are still having issues please submit logging from the log viewer, or application debugger so we can review the actual call flow.  The application debugger is the best diagnostic tool that we have for getting insight into applications that aren't behaving the way we want them to, but it takes a bit of experience to intuit the messages displayed.

Click on the "account" tab at the top right of evolution site, and select the "application debugger" prior to making test calls to capture the data. You can then select the "support" tab in the debugger to send us a problem report. Some additional links on this utility are provided below:

http://docs.voxeo.com/voicexml/2.0/loggerfeatures.htm
http://docs.voxeo.com/voicexml/2.0/mot_loggermessages.htm
http://docs.voxeo.com/voicexml/2.0/gettingsupport.htm

Hope this helps, and we are standing by for your update at this time.

Regards,

John Dyer
Customer Engineer
Voxeo Support
futurejp
5/3/2010 5:40 PM (EDT)
Hi, I have a question. I have an array variable and a counter variable. So if I want to get the value inside certain cell of an array, I can say in a prompt

<value expr="gameListAudio[qCounter]"/>

and it will speak the value, currently either the string "easy" or "hard".

I want to put this in a conditional and I feel like I've tried every possible way but nothing works. So I want to say something like:

<if cond="gameListAudio[qCounter] == 'easy'"/>
<audio src="audio/easy.wav"/>
<else/>
<audio src="audio/hard.wav"/>
...

Which doesn't work. No audio plays. The audio is there, because if I say

<audio src="audio/hard.wav"/>

outside of a conditional, it plays.

I've tried doing things like

<if cond="value expr='gameListAudio[qCounter]' == 'easy'"/>

to try to get at the *value* of that array cell, but nothing seems to work. Any ideas?

Also, here's another question. Say I just have a variable myVar and its value is either 'hard' or 'easy.' Is there a way for me to say something like:

<audio src="audio/" + myVar + ".wav"/>

? Because that would be very helpful. Thanks.

login
  E: VoiceXML Variables  |  TOC  |  Variable Scoping  

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