VoiceXML変数はECMAScript変数と等価な関係にある。変数の命名規則はECMAScriptと同じである。ただしアンダスコア("_")で始まる名前は内部的に使用するために予約されている。
変数は<var>要素により宣言される。
<var name="home_phone"/> <var name="pi" expr="3.14159"/> <var name="city" expr="'Sacramento'"/>
フォーム項目により宣言することもできる。
<field name="num_tickets" type="number"> <prompt> チケットを何枚購入しますか? </prompt> </field>
明白な初期値を持たない変数はECMAScriptのundefined値に初期化される。変数は使用前に宣言されなければならない。
formにおいては、<var>により宣言される変数とフォーム項目によって宣言される変数は、formに入ったときに初期化される。初期化はドキュメント中の出現順序で行われることが保証される。よって以下の例は正しい:
<form id="test"> <var name="one" expr="1"/> <field name="two" expr="one+1" type="number"> </field> <var name="three" expr="two+1"> <field name="go_on" type="boolean"> <prompt> 「はい」か「いいえ」を言ってください。 </prompt> </field> <filled> <goto next="#tally"/> </filled> </form>
ユーザがこの<form>にアクセスすると、formの初期化は最初変数"one"を宣言し、その値を1にセットする。それからfield項目変数"two"を宣言し、それに2を与える。次に初期化ロジックは変数"three"を宣言し、数値3を与える。form処理アルゴリズムはその後メインループに入り、"go_on"フィールドの処理を始める。
変数は以下の有効範囲に対して宣言できる。
session | これらは読み出し専用の変数であり、ユーザセッション全体に関するものである。処理コンテキストにより宣言、セットされる。新しいセッション変数をVoiceXMLドキュメントで宣言することはできない。9.4.参照 |
application | アプリケーションルートドキュメント<vxml>の子要素である<var>要素によって宣言される。それらはアプリケーションルートドキュメントがロードされたときに初期化される。アプリケーションルートドキュメントがロードされている間これらの変数は存在し、ルートドキュメントおよびロード中のリーフ(葉)ドキュメントからアクセス可能である。 |
document | ドキュメント<vxml>の子要素である<var>要素で宣言される。ドキュメントがロードされたときに初期化される。ドキュメントがロードされている間存在しており、ドキュメント内からのみアクセス可能である。 |
dialog | 対話(<form>または<menu>)は、ユーザがその対話をアクセスしている間のみ有効な変数範囲を持ち、その対話の要素からアクセス可能である。対話における変数は<form>の子要素である<var>、実行可能内容(例:<block>内容またはcatch要素内容)に含まれる<var>要素、またはフォーム項目要素により宣言される。<form>要素の子要素<var>は、formが最初にアクセスされたときに初期化される。実行可能内容に含まれる<var>要素は、実行可能内容が実行されたときに初期化される。フォーム項目変数は、フォーム項目が収集されるときに初期化される。 |
(anonymous) | <block>、<filled>そしてcatch要素はそれぞれ変数範囲を匿名で定義し、その要素内で宣言された変数がこれに含まれる。 |
以下の図は変数範囲の階層を示している。
図中の白矢印は、それぞれの有効範囲そのものを名前として定義済みの変数を参照できることを示す。例えば、匿名、対話、そしてドキュメントの有効範囲において、ドキュメント有効範囲内の変数"X"は"document.X"として参照できる。
変数はcond、expr属性において参照できる。
<if cond="city == 'LA'"> <assign name="city" expr="'Los Angeles'"/> <elseif cond="city == 'Philly'"/> <assign name="city" expr="'Philadelphia'"/> <elseif cond="city == 'Constantinople'"/> <assign name="city" expr="'Istanbul'"/> </if> <assign name="var1" expr="var1 + 1"/> <if cond="i > 1"> <assign name="i" expr="i-1"/> </if>
condと、expr内で使われる表現の言語はECMAScriptそのものである。注:condの演算子">","<",">=","<=","&&"はXMLにおいてはエスケープされなければならない。(例:">","<")簡単のために、この文書の例ではXMLエスケープを用いていない。
変数の参照は、上述した変数範囲の階層に従って、内包している最も近い変数範囲とマッチする。変数範囲の名前を接頭辞に使うことで、変数参照を明確化したり不明瞭さを解決することができる。例:ドキュメント内で後に使用するために、フォーム項目変数の数値を保存する。
<assign name="document.ssn" expr="dialog.ssn"/>
もしアプリケーションルートドキュメントが変数xを持つならば、それは非ルートドキュメント内のapplication.x、そしてアプリケーションルートドキュメント内のapplication.xまたはdocument.xとして参照される。
session.telephone.ani | Automatic Number Identification。この変数は発信側の電話番号とともに電話着信を受信側に知らせるAutomatic Number Identificationサービスからの結果を提供する。この情報はサービスがサポートされている場合のみ提供され、さもなくばundefinedにされる。 |
session.telephone.dnis | Dialed Number Identification Service。この変数は発信者がかけた受信側の電話番号を指定するDialed Number Identification Serviceからの結果を提供する。この情報はサービスがサポートされている場合のみ提供され、さもなくばundefinedにされる。 |
session.telephone.iidigits | Information Indicator Digit。この変数は発信者の発信線(例:payphone,携帯電話,特別なオペレータハンドリング,prison)。Telecordiaは"Local Exchange Routing Guide"のそれぞれのvolumeの1章のII digitsの完全なリストを公開している。この情報はサービスがサポートされている場合のみ提供され、さもなくばundefinedにされる。 |
session.telephone.uui | User to User Information。この変数は発信者の共同加入線から設定を呼び出すISDNの一部としての補充情報を返す。この情報はサービスがサポートされている場合のみ提供され、さもなくばundefinedにされる。 |
訳注:VoiceXML 2.0の5.1.5 Standard Application Variablesを訳出して追加した.
Interpretations are sorted by confidence score, from highest to lowest. Interpretations with the same confidence score are further sorted according to the precedence relationship (see Section 3.1.4) among the grammars producing the interpretations. Different elements in application.lastresult$ will always differ in their utterance, interpretation, or both.
The number of application.lastresult$ elements is guaranteed to be greater than or equal to one and less than or equal to the system property "maxnbest". If no results have been generated by the system, then "application.lastresult$" shall be ECMAScript undefined.
Additionally, application.lastresult$ itself contains the properties confidence, utterance, inputmode, and interpretation corresponding to those of the 0th element in the ECMAScript array.
All of the shadow variables described above are set immediately after any recognition. In this context, a <nomatch> event counts as a recognition, and causes the value of "application.lastresult$" to be set, even though the existing values of field variables are not affected by a <nomatch>. In contrast, a <noinput> event does not change the value of "application.lastresult$". After the value of "application.lastresult$" is set, the value persists (unless it is modified by the application) until the browser enters the next waiting state, when it is set to undefined. Similarly, when an application root document is loaded, this variable is set to the value undefined. The variable application.lastresult$ and all of its components are writeable and can be modified by the application.
The following example shows how application.lastresult$ can be used in a field level <catch> to access a <link> grammar recognition result and transition to different dialog states depending on confidence:
<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/vxml http://www.w3.org/TR/voicexml20/vxml.xsd"> <link event="menulinkevent"> <grammar src="/grammars/linkgrammar.grxml" type="application/srgs+xml"/> </link> <form> <field> <prompt> Say something </prompt> <catch event="menulinkevent"> <if cond="application.lastresult$.confidence < 0.7"> <goto nextitem="confirmlinkdialog"/> <else/> <goto next="./main_menu.html"/> </if> </catch> </field> </form> </vxml>
The final example demonstrates how a script can be used to iterate over the array of results in application.lastresult$, where each element is represented by "application.lastresult$[i]":
<?xml version="1.0" encoding="UTF-8"?> <vxml version="2.0" xmlns="http://www.w3.org/2001/vxml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/vxml http://www.w3.org/TR/voicexml20/vxml.xsd"> <form> <field name="color"> <prompt> Say a color </prompt> <grammar type="application/srgs+xml" src="color.grxml" /> <filled> <var name="confident_count" expr="0"/> <script> <![CDATA[ // number of results var len = application.lastresult$.length; // iterate through array for (var i = 0; i < len; i++) { // check if DTMF if (application.lastresult$[i].confidence > .7) { confident_count++; } } ]]> </script> <if cond="confident_count > 1"> <goto next="#verify"/> </if> </filled> </field> </form> </vxml>