Home > Articles > Web Services > XML

Introduction to Voice XML Part 4: Grammars, Scope, and Event Handlers

Building robust voice applications means striking a balance between the needs of both experienced and naïve users. Experienced users often want to jump ahead to selections they already know about; novice users often need all the help they can get. Like most contemporary programming languages, Voice XML offers a structured application framework that supports events, handlers, and scoping rules that enable developers to fine-tune how a voice application responds in different contexts. Frank Coyle tells you why putting it all together will go a long way toward keeping all your users happy.
Like this article? We recommend

Welcome back! The previous article in this series looked at how grammars can be used with forms to build simple voice applications. To summarize our explorations so far, you’ve learned the following:

  • Forms and menus are the basic building blocks of a Voice XML application.
  • Grammars can be associated with the fields that make up a form.
  • Input that matches a grammar is used to populate form variables.
  • The <nomatch> element can be used to define and override an application-specific response to input that does not match a grammar.
  • The <noinput> element can be used to define and override an application-specific response to lack of input.

Understanding these basics enables you to build useful and functional voice applications. However, there are times when you need to go beyond these basics to deliver more complex, robust, and responsive voice applications.

For example, you may want to provide general as well as context-specific help messages or allow experienced users to interrupt dialogs and jump to specific options without being prompted. To accomplish these things, we need to look at several techniques that will enhance the user experience.

Barging in with bargein

By default, Voice XML applications allow a user to barge in and interrupt a dialog with input that will advance them past one or more prompts. This ability to barge in is controlled by the bargein property of an application, which is great for experienced users who can move rapidly through prompts to get the information they want, bypassing the tedium of long messages and complex key sequences.

However, developers can selectively disable bargein—to ensure that a user listens to a complete informative message or advertisement. Additionally, bargein can be disabled to prevent spurious background conversation from triggering an active grammar.

Listing 1 illustrates how to disable bargein (line 4) and force the user to listen to an advertisement. Since bargein is turned off within a form in Listing 1, other dialogs that may appear within the document will not have bargein turned off. Turning bargein off at the document level will turn it off for all dialogs in the document.

Listing 1 Disabling bargein for a form

1 <vxml version="2.1">
2  <form id="main">

3  <!--inside the form we disable bargein for this dialog -->
4  <property name="bargein" value="false"/>

5  <!-- play an ad -->
6  <block>
7   <audio src="http://www.zorko.com/ads/paynow.wav"/>
8  </block> 

9 <!-- dialog -->
10 <field name="city">
11  <prompt>What city are you calling from?</prompt>
12  <grammar src="city.grammar"/>
13  </field>
14  </form>
15 </vxml>

When bargein is allowed, a user can bail completely out of one dialog and move rapidly to a completely different dialog. But to accomplish this, we need to have multiple grammars active so we can take action if any spoken or dtmf input matches.

To write applications that allow these kinds of jumps across dialogs, Voice XML supports scoping rules similar to those found in programming languages. In Voice XML, grammars can be positioned at the field, form, or document level. Additionally, we can react to input across a whole collection of documents by organizing multiple documents as an application.

Let’s first look at a simple example of how to accomplish this within a single document and then look at multiple documents.

Listing 2 illustrates grammar scope with a document that contains multiple dialogs. The example code makes use of the Voice XML link element, which enables us to define a target dialog and a grammar that defines the input that will trigger the dialog. The structure of a link element is the following, where next specifies the dialog to transition to:

 <link next="#mydialog">
   <grammar mode="voice">
    <!--grammar goes here -->

In Listing 2, the link element appears as a child of the vxml element, giving it document scope. This means that within any menu or form contained in the document, a match against one of the link grammars will pass control to the target dialog.

This example also contains two link elements: one that will give us a quick transition to the baseball menu and another that will take us to the form with id="mets".

In Line 13, the link element includes a grammar for the single word baseball. Note that our example contains two link elements: one for transitioning to the baseball dialog (line 10) and another to transition to the mets dialog (line 18).

Also note that the link on line 18 contains two grammars: one voice and the other dtmf. Thus, if the user says "mets" or "New York mets" or presses 9 at any time, they will be transitioned to the form with id="mets".

Listing 2 Voice XML document with link grammars

1  <?xml version="1.0" encoding="UTF-8"?>
2  <!DOCTYPE vxml SYSTEM "http://www.w3.org/TR/voicexml20/vxml.dtd">
4  <vxml version = "2.0" xmlns=’http://www.w3.org/2001/vxml’
5     xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance’
6     xsi:schemaLocation=’http://www.w3.org/2001/vxml 
7     http://www.w3.org/TR/voicexml20/vxml.xsd’>
10   <link next="#baseball">
11    <grammar mode="voice">
12     <rule id="linkbaseball" scope="public">
13      baseball
14     </rule>
15    </grammar>   
16   </link>
18   <link next="#mets">
19     <grammar>
20      <rule id="linkmets" scope="public">
21       <one-of>
22        <item>mets</item>
23        <item>new york mets</item>
24       </one-of>
25      </rule>
26     </grammar> 
28     <grammar mode="dtmf" >
29      <rule id="linkmets2" scope="public">9</rule>
30     </grammar>
32   </link>
35   <menu id="mainmenu" dtmf="true">
36    <prompt>
37     Welcome to the info hotline. If you know the category you want, 
38     you may say it at any time.
39     <enumerate>
40      For <value expr="_prompt"/>, press <value expr="_dtmf"/> 
41     </enumerate>
42    </prompt>
45    <choice next="#sports">sports</choice> 
46    <choice next="#weather">weather</choice> 
47   </menu> 
49  <menu id="sports">
50   <property name="inputmodes" value="dtmf"/>
51   <prompt>
52   For baseball press 1, For football press 2, For soccer
53   press 3.
54   </prompt>
55   <choice dtmf="1" next="#baseball"/>
56   <choice dtmf="2" next="#football"/>
57   <choice dtmf="3" next="#soccer"/>
58  </menu>
61  <form id="weather">9
62   <block> You have reached the weather line. Whether it’s cold,
63   or whether it’s hot, we’re going to have weather,
64   whether or not.
65   </block> 
66  </form>
68  <menu id="baseball">
69   <property name="inputmodes" value="dtmf"/>
70   <prompt>
71   For yankees press 1, For mets press 2, For giants press 3.
72   </prompt>
73   <choice dtmf="1" next="#yankees"/>
74   <choice dtmf="2" next="#mets"/>
75   <choice dtmf="3" next="#giants"/>
76  </menu>
78  <form id="soccer">
79   <block> you have reached the soccer hotline.
80   </block>
81  </form>
83  <form id="football">
84   <block> you have reached the football hotline.
85   </block> 
86  </form>
88  <form id="mets">
89   <block> The mets are contenders for the world series. 
90   </block> 
91  </form> 
93  <form id="yankees">
94   <block> George Stein Brenner rules his roost.
95   </block> 
96  </form> 
98  <form id="giants">
99   <block> Barry Bonds has now hot more home runs than Hank Aaron.
100  </block> 
101 </form> 
103 </vxml>

In the Listing 2 example, all the menus and forms are contained in the same document, so our positioning of the link element (with document scope) ensures that the link grammar is operational across all dialogs.

However, if our menus and forms were set up as separate documents, the shortcut document link grammar would no longer be available once we transitioned to another document.

To get around this problem so that grammars, variables, and properties remain visible across a range of separate documents, Voice XML supports the concept of application.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020