VoiceXML 2.1 Development GuideHome  |  Frameset Home


<var>  element

The var element is used to declare a VoiceXML variable within the scope specified by its parent element. Additionally, variables declared with the var element are accessible from any JavaScript which resides within the script element, providing that they are within the same scope.


usage
<var expr="CDATA" name="NMTOKEN">


attributes
exprData Type: CDATADefault: Optional
The expr attribute denotes the initial ECMAScript expression to be evaluated and assigned to the var name when first invoked.
nameData Type: NMTOKENDefault: Required
The name attribute represents the ECMAScript variable name. Valid characters for this value can be letters, numbers, underscores, and dollar signs, but cannot contain any other characters. In addition, the variable name must start with either a dollar sign or a letter, else an error.semantic will be thrown.



shadow variables
none


parents
<block> <catch> <error> <filled> <form> <help> <if> <nomatch> <vxml>


children
none


code samples
<Var name-expr>
<?xml version="1.0" encoding="UTF-8" ?>

<vxml version = "2.1">

<meta name="author" content="Matthew Henry"/>
<meta name="copyright" content="2005 voxeo corporation"/>
<meta name="maintainer" content="YOUR_EMAIL@HERE.COM"/>

<var name="myVar" expr="'foo'"/>

<form id="F1">

<block>
  <prompt>
  Our variable name is currently <value expr="myVar"/>.
  </prompt>

  <assign name="myVar" expr="'bar'"/>

  <prompt>
  However, now that we have assigned a new value to this variable,
  its value now equates to <value expr="myVar"/>.
  </prompt>
</block>
   
</form>

</vxml>

<Hollywood.grammar>
;GSL2.0
ACTOR
[
  (david faustino)    { <MyFavoriteActor "david faustino"> }
  (jean stapleton)    { <MyFavoriteActor "jean stapleton">  } 
  (tina yothers)      { <MyFavoriteActor "tina yothers"> }
]

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

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

<meta name="author" content="Matthew Henry"/>
<meta name="copyright" content="2005 voxeo corporation"/>
<meta name="maintainer" content="YOUR_EMAIL@HERE.COM"/>


<!-- note the self-referencing application root -->

<form id="MainMenu">
    <field name="GMyFavoriteActor">
      Please say your favorite Actor's name
      <grammar src="Hollywood.grammar#ACTOR" type="text/gsl"/>

      <filled namelist="GMyFavoriteActor">
        <assign name="application.GMyFavoriteActors" expr="GMyFavoriteActor"/>
        <goto next="Main2.vxml"/>
      </filled>
    </field>
  </form>
</vxml>


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

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

<!--CHANGE THE APPLICATION VALUE
TO REFLECT THE FILENAME OF THE FIRST DOCUMENT -->


<meta name="author" content="Matthew Henry"/>
<meta name="copyright" content="2005 voxeo corporation"/>
<meta name="maintainer" content="YOUR_EMAIL@HERE.COM"/>


  <form id="F1">
    <block>
    <prompt>
      My favorite actor is <value expr="application.GMyFavoriteActors"/>
    </prompt>
    </block>
  </form>
</vxml>




additional links
W3C 2.0 Specification
W3C 2.0 Specification


  ANNOTATIONS: EXISTING POSTS
BoknesMonster
9/14/2004 6:28 AM (EDT)
Hi
<vxml> can also be a parent of <var>.
phalcom
1/30/2006 1:50 PM (EST)
Hello

How i can transver variables from one VXML doc to an Other VXML doc?
mikethompson
1/30/2006 1:59 PM (EST)
Hey there,

The <submit> element is exactly what you are looking for.  It allows you to attach a space-delimited list of variables to send to the destination document.  You can find out all you need to know about it here: http://www.voicexmlguide.com/submit.htm

Regards,
Mike Thompson
Voxeo Extreme Support
mikethompson
3/20/2006 11:41 AM (EST)
Hello Bryan,

In order to get moving toward a solution there's some more information that we need from you.  We will need to see full debugger logs to take a deeper look at what is going wrong.  If you would, please call your application with the Real-time Debugger window open.  Then use the "Support" tab at the top of that window to email us the session logs.  We will gladly take a look for you...

Cheers,
Mike Thompson
Voxeo Extreme Support
mikethompson
3/20/2006 4:02 PM (EST)
Hi Bryan,

Voxeo offers free community support through our ticketing system.  By posting here, it is clear that you already have a community account.  As such, there is no problem with asking us for help :).  However, we are still going to need debugger logs before we can move forward.

Cheers,
Mike Thompson
Voxeo Extreme Support
bsaliba
3/21/2006 6:17 AM (EST)
Thanks Mr. Thompson,

So, how do I proceed to send the logs?

Rgds,
Bryan
bsaliba
3/21/2006 8:08 AM (EST)
On further tbst, I think the possible cause might be that on loading a subdialog, its re-initialising a new root context and hence re-initialising my var loggedin to false.

How can I prevent this?
Michael.Book
3/21/2006 10:33 AM (EST)
Hey Brian,

This is actually the expected behavior.  As per the W3C VoiceXML specification, subdialogs will create a new application context.  The only way to pass parameters to and from a subdialog is via <param>'s, the <subdialog> initiation 'namelist', and <return>.

The big question here is probably going to be:  Why are you using a subdialog?

There are very, very few cases where a subdialog is actually beneficial.  It's likely that a subdialog is not necessary here, and a simple <submit> will do...

Let us know if you have some compelling reason you would like to use a subdialog.  Or provide some code example and we can offer more targeted suggestion...


Cheers,

~ Michael
bsaliba
3/21/2006 12:11 PM (EST)
On second thoughts, I dont know if this makes sense, but is there a way to either have a variable, that cannot be re-initialised, or having a subdialog, which does cause loading of the application root document and re-initialise its variables.

I don't know, possibly declaring a variable, available to all documents within the application, which can be set as read only at some point. Or using a session variable, or cookie? can this be done? I am trying to think of something available throughout the application, which can be set once, but cannot be re-initialised.

Thanks for your help.
Bryan



bsaliba
3/21/2006 1:11 PM (EST)
I fully understand. Thanks for your time and help.

Unfortunately, that platform was the only available platform for a student at the time when I started my thesis.

I will try and amend my coding to exclude subdialogs.

On a final note, so there is no way I can set particular session variables or information in a cookie, which is not overwritten or re-initialised, or some form of read-only attribute to a variable?

Thanks
Bryan
MattHenry
3/23/2006 10:25 PM (EST)


Hiya Brian,

If setting a session var is required, and we can'tgather the correct information to debug your subdialog/application var issue, then I'm afraid that the only option that is really available is to use server-side session vars/cookies.

Best of luck!

~Matthew Henry
yasserhu
5/7/2006 1:37 PM (EDT)
Dear Sirs,

I would like to get some help with the var element. Suppose I have two fields field1 and field2 and on variable MyVar, and after recognizing the voice both fields would have a value, the question is how can I pass these to values and store them in the same varialbe such that

<assign name="MyVar" expr=??????"

Thanks for your help.

Kind regards

Yasser Hussein
Michael.Book
5/7/2006 4:21 PM (EDT)
Yasser,

My apologies, but I am not entirely sure I understand what you are asking for...

You ask how to "pass and store" the vars.  To where would you like to pass and store your variables?

- To pass variables back to your web-server, use the <submit> tag, and specify the needed vars in the 'namelist' attribute.

- To pass variables between VoiceXML documents within the same session, I would recommend using an application root document and declaring/assigning application scope vars.  For more information, see 'http://docs.voxeo.com/voicexml/2.0/varscoping.htm#start'.

I hope this helps, but if I have missed the mark on what you are asking, do let me know...


Cheers,

~ Michael
kim69
5/8/2006 8:21 AM (EDT)
I am trying to create a document scope variable. Can some please tell me how to do this please as I am struggling with doing this?

I am trying to get the variable to be assigned to the value of a field in one form then access the variable in another form. Any ideas?

Thanks Kim
mikethompson
5/8/2006 1:55 PM (EDT)
Hey Kim,

All you need to do is make sure you have the variable already declared at the root scope of the specific document.  Then, to assign that variable a value from within a form or field, you'll simply need to use document.myVar.  For example:

<assign name="document.myVar" expr="'foo'"/>

Hope this helps,
Mike Thompson
Voxeo Extreme Support
kim69
5/8/2006 5:23 PM (EDT)
sorry but i don't understand by what you mean about root scope of the document?

does this this mean:

<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">

<var name ="test"/> <-------------this bit root scope?

<form id="var1" scope="dialog">
   
<field name="field1">
        <prompt>
            Please state your name.
</prompt>
        <grammar>
******just taken this out now
</grammar>
    </field> 
      <filled>   
                <assign name="document.test" expr="field1"/>
      <submit next="http://www.domain.com/file.php" method = "post"/> <-----------does some server side processing before return to form gets where i want to output the document variable
          </filled>

  </form>

  <form id="gets" scope="dialog">
 
<field name="lecturerName">
          <prompt>
            <value expr="test"/> <--- this is where it keeps saying undefined!!!!!!!
            </prompt>
      </field>
</form>
 
MattHenry
5/8/2006 6:17 PM (EDT)

Hello Kim,

Ideally, it would be best to send application debugger logs to us in order to get an accurate answer to this inquiry, as the snippet that you provided has a large number of "blank" areas that leave quite a bit to guesswork.

In the interest of getting your app working, I have provided some sample code that illustrates the usage of document-scoped variables:


<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">

<var name ="test"/>

<form id="var1" scope="dialog">
   
<field name="field1" type="boolean">
        <prompt>
            Please say yes or no.
      </prompt>
    </field> 
      <filled>   
        <assign name="document.test" expr="field1"/>
        <goto next="#gets"/>
      </filled>
  </form>

  <form id="gets" scope="dialog">
 
        <block>
            <log expr="'*** test = ' + test"/>
            <log expr="'*** document.test = ' + document.test"/>       
      </block>
</form>

</vxml>



~Matthew Henry

login



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