VoiceVoiceXML Developer Series: A Tour Through VoiceXML, Part III

VoiceXML Developer Series: A Tour Through VoiceXML, Part III

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Last time in VoiceXML Developer, we focused on creating a voice user interfaces
with menus. In this edition of the VoiceXML Developer, we will introduce
new VoiceXML elements and learn how to get user input with form fields,
so buckle your seat belt, ’cause here we go!

Create metadata with <meta>

Like HTML, we can create metadata for VoiceXML documents by setting
values with the <meta> element. This information may be used by search
engines or other applications that access information such as author, copyright,
and date.

<vxml version="1.0">
  <meta content="Jonathan Eisenzopf" 
name="author" />
  <meta name="maintainer" 
content="eisen@ferrumgroup.com" />
  <meta content="Copyright 2001, 
VoiceXML Planet" name="copyright" />
  <meta content="VoiceXML, 
tutorial, eisen, developer" name="
keywords" />

Set/access local variables with <var>, <value>, and <assign>

VoiceXML uses the <var> element for declaring
variables in VoiceXML applications. To create a new variable for my email
address, we would use the statement below:

<var name="email">

We can define an initial value for the variable in the expr
attribute. The example below initializes my name, address, and phone
number with default values (that can be changed later):

<vxml version="1.0">
  <var name="name" expr="'Jonathan Eisenzopf'" />
  <var name="address" expr="'555 Hard Wood Lane'" />
  <var name="phone" expr="'703-555-1212'" />

You can retreive the value of a variable by specifying the name in the
expr attribute of the <value> element:

<vxml version="1.0">
  <form id="form1">
    <block>my name is 
      <value expr="name" />I live at
      <value expr="address" />My phone number is 
      <value expr="phone" />

If you were to run the example above, you would here:

My name is Jonathan Eisenzopf I live 
at five five five hard wood lane my phone number 
is seven zero three five five five one two one two.

To set the value of a variable after it has been declared, name the variable
in the name attribute of the <assign> element and
specify the new value inside the expr attribute. For example, to change
my phone number to 800-555-1212, I would use the assignment below:

<assign name="phone" expr="800-555-1212">

Set the application attribute of <vxml> to access global variables

We may want to make some variables globally accessible to a number of
VoiceXML documents that make up a VoiceXML application. But first, let’s introduce
the concept of a multi-document application. The diagram below depicts a VoiceXML
application whose root is main.vxml.

Application Root

There are two other documents
in the application as well; login.vxml and account_info.vxml.
Both of these documents are able to access and manipulate the variables in
main.vxml by setting the application attribute of the
vxml element to main.vxml. Let’s take a look at the
source of two of these files to see how we create and access global variables.


This VoiceXML file is the root document, thus all global variables will
be initialized and stored here. Below, you’ll notice that main.vxml contains
only one statement, which is the variable named valid_pin.
There’s no need to include any other elements in this for our example here.

<vxml version="1.0">
  <var name="valid_pin" expr="2516" />


The first thing you should notice about login.vxml is that
the application attribute of the vxml element is
main.vxml. This makes all of the variables and grammars available
within login.vxml. To access a variable in the root document,
pre-pend the application. keyword to the variable
name contained in the root document:

<vxml version="1.0" application="main.vxml">
  <form id="pin">
    <block name="block1">The valid PIN code is 
      <value expr="application.valid_pin" />

Now we can call on the value of the valid_pin variable
in main.vxml by referring to it with a <value> element.
When this VoiceXML document is executed, the output will

The valid PIN code is two five one six.

Use the <field> element to gather input from the user

VoiceXML forms are very similar to HTML forms except that VoiceXML
uses the <field> element whereas HTML uses the <input>
element (there are other field types as well which we’ll cover later).
Each VoiceXML form contains one or more <field> elements. Each field
must have an associated grammar or built-in type defined in the type
attribute of the <field> element. For example, to get a user’s credit
card number, we can use the digit type for the field and set the
expected length to 16 by adding ?length=16 after specifying
digit as our built-in grammar type.

<vxml version="1.0">
    <field name="ccnum" type="digits?length=16">
      <prompt bargein="true">Please say your sixteen 
	  digit credit card number now.</prompt>
    <block name="block1">You said
      <value expr="ccnum" />

The example above will ask for the users credit card number and wait for
a response. If the user says or enters (via the keypad) exactly 16 digits,
the computer will repeat the value that was entered and exit. If the user did
not enter or say 16 digits, the system will either reprompt the user or exit
the program depending on your platform. The value of the input is converted
to text and stored in the ccnum variable, which can be
referenced as described earlier with the <value> element.

Alternatively, we could have left off the length parameter as part of the
value for the field type attribute,
and the system would match any number of digits:

<field name="ccnum" 

Or, we can specify a minimum and/or maximum number of digits to match:

<field name="ccnum" type="digits?minlength=16;

Use built-in field grammars to match speech and DTMF input

There are seven built-in data types that VoiceXML
interpreters can recognize. These are:

  • number
  • date
  • time
  • currency
  • phone
  • digits
  • boolean

All of the built-in types can accept DTMF tones and/or spoken text. We will discuss
each built-in type with an example for each.


The number type will recognize spoken whole numbers like "One-Hundred
Twenty-Three" as well as numbers with decimal points such as
"Fifty-Five Point Nine". When entering a number via DTMF tones,
the * character represents the decimal point.

    <field name="age" type="number">
      <prompt bargein="true">How old are you?</prompt>


The boolean type returns either a true or false. Two parameters can be
passed with the boolean type that indicate which DTMF tones correspond
with true and false.

    <field name="likebooks" 
      <prompt bargein="true">
Have you read William Shatner's books?</prompt>

In the example above, 1 is true and 2 is false. If you evaluate the value of the result,
it will be either yes or no. Spoken input (for English) is yes for true and no
for false.


We’ve already seen this one, but below is an example for completeness:

    <field name="ccnum" type="digits?length=16">
      <prompt bargein="true">
Please say your credit card number.</prompt>

As before, you can pass a length parameter, which specifies the exact number of
digits to match or you can include a maxlength and/or minlength, which specify the
maximum and minimum number of digits to match respectively.


The date type will match the day, month, and year when spoken. For example,
"August first nineteen seventy four". When specifying a date with
the keypad, the user should enter yyyymmdd where yyyy is the year, mm is the
month, and dd is the day. If the year is not specified by the user, the resulting
string will contain ???? for the year. In the same manner, if the month or day
are not specified, the string value for the month or day will be ?? respectively.

    <field name="ccexpire" type="date">
      <prompt bargein="true">
Please say the expiration date for your 
      credit card.</prompt>


The currency type will recognize dollars and cents (or whatever currency
names are used in your locale. i.e. pounds and yen). For example,
"Fifty dollars, thirty seven cents". When entering a currency amount
via the keypad, use the * character for the decimal point.

    <field name="price" type="currency">
      <prompt bargein="true">What price would 
you like to play for these tickets?</prompt>


Time can be spoken in 12 or 24 hour formats. For example, "twelve thirty
PM" or "twenty three ten". Time can also be entered via a
keypad. The resulting string will contain four numbers plus a p, a, h, or ?
character. An a means AM, p means PM, h
means 24 hour time, and a ? means
the system was not able to determine the format.

    <field name="time" type="time">
      <prompt bargein="true">
What time before the show would you 
      like to pick up your tickets?</prompt>


The phone type can contain a number of digits and alternatively an extension.
For example, "eight-zero-zero-five-five-five-one-two-one-two extension
one-zero-one". When entering a phone number via the keypad, the * character
indicates that the numbers that follow are the extension to the number that
was entered previously. The resulting text will contain the phone number with
an x character separating the main number and extension if an extension was

    <field name="phone" type="phone">
      <prompt bargein="true">
What phone number can we reach 
      you at if the show gets cancelled?</prompt>

Example 2

Now let’s pull all the individual fields together into one form.
To try this example, call VoiceXML Planet at 510-315-6666; press
1 to listen to the demos, then press 2 to hear this example.

<vxml version="1.0">
  <form id="form1">
    <block name="block1">To purchase tickets 
for William Shatner's Tambourine Man world tour, 
do whatever the robotic man on the phone 
<field modal="false" hotword="false" name="age" type="number">
<prompt bargein="true">
How old are you?</prompt>
<field name="ticketnum" 
<prompt bargein="true">
How many tickets do you need?</prompt>
<field name="likebooks" 
<prompt bargein="true">
Have you read William Shatner's 
books or watched the made for television 
<field name="ccnum" 
<prompt bargein="true">
Please say your credit card number.</prompt>
<field name="ccexpire" type="date">
<prompt bargein="true">Please say 
the expiration date for your 
credit card.</prompt>
<field name="price" type="currency">
<prompt bargein="true">
What price would you like to play 
      for these tickets?</prompt>
<field name="time" type="time">
<prompt bargein="true">
What time before the show would 
      you like to pick up your tickets?</prompt>
<field name="phone" type="phone">
<prompt bargein="true">
What phone number can we reach 
      you at if the show gets cancelled?</prompt>
<block name="results">You are 
<value expr="age" />years old 
and want to purchase
<value expr="ticketnum" />tickets. 
When asked whether you've read William Shatner's 
books or seen the movie, you said
<value expr="likebooks" />. 
Your credit card number is 
<value expr="ccnum" />, which 
expires on 
<value expr="ccexpire" />. 
If William Shatner 
needs a friend, he can call 
<value expr="phone" />. Thank 
you for your order.


In this article, we’ve learned about creating metadata, manipulating
variables, and gathering form field input via built-in grammar types. We are
well on our way to being able to create full-fledged VoiceXML applications.
Join us next time for another edition of VoiceXML Developer as we continue
our tour through VoiceXML.

About Jonathan Eisenzopf

Jonathan is a member of the Ferrum Group, LLC based in Reston, Virginia
that specializes in Voice Web consulting and training. He has also written
articles for other online and print publications including WebReference.com
and WDVL.com. Feel free to send an email to eisen@ferrumgroup.com regarding
questions or comments about the VoiceXML Developer series, or for more
information about training and consulting services.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories