VoiceXML2.0规范 |
|
第二章 Dialog的结构 | [第一章 ][前一章 ][后一章 ] |
|
第二节 Menus | 第一节 第三节 第四节 第五节 |
|
menu在语法上是一个form的另一种形式,该form包含了单个的匿名的field。menu提示选择一个选项,并根据该选项跳转到相应的dialog。就像一个正规的form一样, menu也有它自己的语法作用域,例如当用户在执行另一个dialog时,它的语法也可以是激活的。下面的例子中,menu给用户提供了3个选项: |
|
<?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"> <menu> <prompt> Welcome home. Say one of: <enumerate/> </prompt> <choice next="http://www.sports.example.com/vxml/start.vxml"> Sports </choice> <choice next="http://www.weather.example.com/intro.vxml"> Weather </choice> <choice next="http://www.stargazer.example.com/voice/astronews.vxml"> Stargazer astrophysics news </choice> <noinput>Please say one of <enumerate/></noinput> </menu> </vxml> |
|
下面是该dialog可能的流程: |
|
C: Welcome home. Say one of: sports; weather; Stargazer astrophysics news. H: Astrology. C: I did not understand what you said. (a platform-specific default message.) C: Welcome home. Say one of: sports; weather; Stargazer astrophysics news. H: sports. C: (proceeds to http://www.sports.example.com/vxml/start.vxml) |
|
一、MENU |
|
<menu>元素可以标识一个菜单,并确定它的语法的作用域。Menu的属性如下: |
id | Menu的标识符,它允许dialog通过<goto>或<submit>跳转到该menu。 | scope | Menu的语法作用域。如果它的值为“dialog”,只有当用户跳转到该menu,它的语法才是激活的。如果它的值为“document”,它的语法在整个文档内(或者,如果该menu在应用根文档里,在该应用中,所有加载的文档内)都是激活的。 | dtmf | 当它的值为“true”,前九个choice如果没有明确的指定dtmf属性,则它们会得到隐含的dtmf属性,如“1”、“2”等。剩下的那些没有明确指定dtmf属性的choice不会得到DTMF值(因此它们不能通过DTMF按键来匹配)。此时,如果该menu有choice指定了它自己的DTMF序列,该序列包含除了“*”、“#”或“0”以外的字符,平台要抛出error.badfetch 事件。该属性默认值为“false”。 | accept | 当它的值为“exact”(该值为默认值),则该menu中的choice元素的文本定义的是要识别的精确的短语。如果它的值为“approximate”,则该menu中的choice元素的文本定义的是要识别的近似的短语(如2.2.5节中描述的)。<choice>的accept属性的优先权高于menu的该属性。 | |
表7:<menu>元素的属性 |
|
二、CHOICE |
|
<choice>元素有下面几个作用: |
|
1、它可以指定一个语音语法,用<grammar>元素定义或由程序自动产生,详细描述见2.2.5节; |
|
2、它可以指定一个DTMF语法,详见2.2.3节; |
|
3、它的内容可以作为<enumerate>的提示语,详见2.2.4节; |
|
4、当某个选项被选择的话,它要么指定了要抛出的事件,要么指定了要跳转的URI。 |
|
<choice>的属性有: |
dtmf | 该<choice>元素的DTMF序列。它的作用等同于一个简单的DTMF语法,或者应用于该序列识别的DTMF property(6.3.3节)。它和DTMF不同的是,它的空格是不起作用的:dtmf=“123#”和 dtmf=“1 2 3 #”是等同的。 | accept | 该<choice>元素的“accept”属性的优先权高于所在的<menu>元素的“accept”属性。当该属性的值为“exact”(默认),<choice>元素的文本定义了要识别的精确的短语;当该属性值为“approximate”,<choice>元素的文本定义的是要识别的近似的短语(详见2.2.5节)。 | next | 要跳转的下一个对话或文档。 | expr | 一个表达式,它的值为要跳转的URI,而不指定“next”属性。 | event | 指定一个要抛出的事件,而不指定“next”属性。 | eventexpr | 一个ECMAScript表达式,它的值为要抛出的事件名。 | message | 一个信息串,它提供了关于要抛出的事件的上下文。在<catch>元素的作用域内用变量“_message”可以得到该信息串,详见5.2.2。 | messageexpr | 一个 ECMAScript表达式 ,它的值为一个信息串。 | fetchaudio | 见6.1节。它默认为<property>的fetchaudio的值。 | fetchhint | 见6.1节。它默认为<property>的documentfetchhint的值。 | fetchtimeout | 见6.1节。它默认为<property>的fetchtimeout的值。 | maxage | 见6.1节。它默认为<property>的documentmaxage的值。 | maxstale | 见6.1节。它默认为<property>的documentmaxstale的值。 | |
表8:<choice>元素的属性 |
|
必须正确地指定属性“next”、“expr”、“event”和“eventexpr”中的一个,否则会抛出error.badfetch事件。也可以正确地指定属性“message”或“messageexpr”中的一个,否则会抛出error.badfetch事件。 |
|
如果在<choice>中指定了一个<grammar>元素,则该外部语法用于替换自动产生的语法。这样开发者就可以准确地控制<choice>元素的语法,例如: |
|
<?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"> <menu> <choice next="http://www.sports.example.com/vxml/start.vxml"> <grammar src="sports.grxml" type="application/srgs+xml"/> Sports </choice> <choice next="http://www.weather.example.com/intro.vxml"> <grammar src="weather.grxml" type="application/srgs+xml"/> Weather </choice> <choice next="http://www.stargazer.example.com/voice/astronews.vxml"> <grammar src="astronews.grxml" type="application/srgs+xml"/> Stargazer astrophysics </choice> </menu> </vxml> |
|
三、DTMF in Menus |
|
menu可以只有语音语法,也可以只有DTMF语法,也可以在<menu>中包含一个<property>元素同时指定DTMF和语音语法。下面的例子中的menu只有DTMF语法,并且使用<choice>元素的dtmf属性给每个<choice>元素显式的指定了DTMF序列: |
|
<?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"> <menu> <property name="inputmodes" value="dtmf"/> <prompt> For sports press 1, For weather press 2, For Stargazer astrophysics press 3. </prompt> <choice dtmf="1" next="http://www.sports.example.com/vxml/start.vxml"/> <choice dtmf="2" next="http://www.weather.example.com/intro.vxml"/> <choice dtmf="3" next="http://www.stargazer.example.com/astronews.vxml"/> </menu> </vxml> |
|
另外,也可以把<menu>元素的dtmf属性置为true,自动的为前九个没有指定DTMF序列的<choice>元素指定一个DTMF序列,第一个为“1”,等等: |
|
<?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"> <menu dtmf="true"> <property name="inputmodes" value="dtmf"/> <prompt> For sports press 1, For weather press 2, For Stargazer astrophysics press 3. </prompt> <choice next="http://www.sports.example.com/vxml/start.vxml"/> <choice next="http://www.weather.example.com/intro.vxml"/> <choice dtmf="0" next="#operator"/> <choice next="http://www.stargazer.example.com/voice/astronews.vxml"/> </menu> </vxml> |
|
四、ENUMERATE |
|
<enumerate>元素用于为用户自动产生<choice>元素的描述,它指定了一个模板,该模板根据<choice>元素在<menu>元素中的顺序,依次应用于每个<choice>。如果<enumerate>中没有内容,则使用默认的模板,列出所有的<choice>。该默认的模板取决于解释器环境。如果<enumerate>中有内容,这些内容就是模板的样式,它可以引用两个专有变量,“_prompt”和“_dtmf”,前者表示<choice>元素的提示语,后者表示<choice>元素的DTMF序列。如下面的例子: |
|
<?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"> <menu dtmf="true"> <prompt> Welcome home. <enumerate> For <value expr="_prompt"/>, press <value expr="_dtmf"/>. </enumerate> </prompt> <choice next="http://www.sports.example.com/vxml/start.vxml"> sports </choice> <choice next="http://www.weather.example.com/intro.vxml"> weather </choice> <choice next="http://www.stargazer.example.com/voice/astronews.vxml"> Stargazer astrophysics news </choice> </menu> </vxml> |
|
该menu的提示语为: |
|
C: Welcome home. For sports, press 1. For weather, press 2. For Stargazer astrophysics news, press 3. |
|
<enumerate>元素可用于<menu>元素中的<promppt>和<catch>元素,也可以用于包含有<option>元素的<field>元素。如果<enumerate>用于其他的地方,平台会抛出error.semantic事件(例如<enumerate>元素中包含有<enumerate>元素)。 |
|
五、Grammar Generations |
|
每个选项的文本(choice phrase)都指定了一组要监听的词或短语。一个选项的文本由直接或间接包含在<menu>元素中的<choice>元素中的元素的PCDATA组成,或由<field>元素中的<option>元素的PCDATA组成。 |
|
如果accept的属性值为“exact”,则用户必须匹配选项文本中的整个短语,且顺序也要相同。 |
|
如果accept的属性值为“approximate”,则用户可以匹配选项文本中的短语的子短语。例如,对于响应选项文本“Stargazer astrophysics news”,用户可以说“Stargazer”, “astrophysics”, “Stargazer news”, “astrophysics news”等。产生的语法是依赖语语言和平台的。 |
|
如下面的例子,在不同的选项中分别使用“exact”和“approximate”。 |
|
<?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"> <menu accept="approximate"> <choice next="http://www.stargazer.example.com/voice/astronews.vxml"> Stargazer Astrophysics News </choice> <choice accept="exact" next="http://www.physicsweekly.com/voice/example.vxml"> Physics Weekly </choice> <choice accept="exact" next="http://www.particlephysics.com/voice/example.vxml"> Particle Physics Update </choice> <choice next="http://www.astronomytoday.com/voice/example.vxml"> Astronomy Today </choice> </menu> </vxml> |
|
由于第一个选项的accept属性值为“approximate”,因此要匹配这个选项,用户可以只说出子短语;例如“Stargazer”或“Astrophysics News”。然而,由于第二个和第三个选项的accept属性值为“exact”,因此只有说出整个短语“Physics Weekly”和“Particle Physics Update”才能匹配语法。 |
|
在下面的例子中,在<choice>元素中使用了PCDATA: |
|
<choice accept="exact" next="http://www.stargazer.example.com/voice/astronews.vxml"> <audio src="http://www.stargazer.example.com/space.wav"> Stargazer <emphasis>astrophysics</emphasis> news </audio> </choice> |
|
该<choice>元素的选项文本由音频文件播放,如果该音频文件不能被播放,则会以语音合成的方式播放“Stargazer Astrophysics News”。该<choice>元素的语法的精确匹配短语“Stargazer astrophysics news”来自<choice>元素的PCDATA。 |
|
六、Interpretation Model |
|
<menu>和只包含一个简单的<field>的<form>的工作机制类似。Menu的事件处理相应的对应于<field>的事件处理。<menu>的语法相应的对应于<form>的语法。和在<form>中一样,<menu>中的语法匹配也会更新数组application.lastresult$。这些变量在5.1.5节中由详细描述。 |
|
一旦进入<menu>,它的语法就被自动生成并激活,然后播放提示语。当用户的输入匹配了一个<choice>控制该<choice>的next, expr, event或eventexpr属性值跳转。这四个属性中,同时只可以指定一个。如果指定了一个event属性,而该事件的事件处理没有让解释器退出或把控制跳转到其他dialog,则FIA会将该<menu>的anonymous field清零,再次执行该<menu>。 |
|