XML Grammars
The XML grammar defined as part of SGRS is more verbose than GSL, but has the advantage that it is XML )which means we can use XML parsing tools to help with syntax checking). In the following examples we revisit our name-extension voice lookup service.
Example 4: XML Format
The main difference between the GSL and the SGRS XML grammar is how we map what the user says to the semantic meaning our program will use. For example, in GSL, the mapping between what a user says and the assignment to a variable value (i.e., worker) can be expressed in one line:
[(roger rabbit) rabbit] {<worker "9 2 8 8">}
In SGRS XML, the matched item in a grammar sets the value of the grammar’s field variable. One common pattern in SGRS XML is to used the filled element to do some additional processing based on the field variable value.
This is illustrated in Listing 4.
Listing 4 An XML grammar for telephone extension lookup
1 <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’> 2 <form id="getRequest"> 3 <var name="phone" expr="’unknown’"/> 4 <field name="worker" > 5 <prompt>Whose extension would you like?.</prompt> 6 <grammar type="application/srgs+xml" mode="voice" root="order"> 7 <rule id="order"> 8 <one-of> 9 <item>bob kirin</item> 10 <item>roger whitney</item> 11 <item>wilma rudolph</item> 12 <item>customer service</item> 13 </one-of> 14 </rule> 15 </grammar> 16 <noinput>Hey, come on, speak up or get off the line.</noinput> 17 <nomatch>Nobody with that name around here.</nomatch> 18 <filled> 19 <if cond="worker == ’wilma rudolph’"> 20 <assign name="phone" expr="’8121’"/> 21 <elseif cond="worker == ’bob kirin’"/> 22 <assign name="phone" expr="’8306’"/> 23 <elseif cond="worker == ’roger whitney’"/> 24 <assign name="phone" expr="’9234’"/> 25 <elseif cond="worker == ’customer service’"/> 26 <assign name="phone" expr="’6234’"/> 27 </if> 28 </filled> 29 </field> 30 <block> 31 the number for <value expr="worker"/>. 32 is <value expr="phone"/>. good bye. 33 </block> 34 </form> 35 </vxml>
Some notes on Listing 4:
- Line 3: Declaration of the variable phone that will eventually hold the value of the extension we are seeking.
- Line 6: We specify the grammar type as "application/sgrs+xml".
- Lines 7–14: The XML grammar rule that looks for one of the names to be spoken. When one of the names matches spoken input, the value of the field variable worker is set to that name string. Since the grammar is satisfied, control is passed to the filled element to complete our processing.
- Lines 18–28: In the filled element we have access to the worker variable and use if..else to assign the extension to our phone variable.
- Lines 30–33: After exiting the form, we announce the extension of worker by accessing our Voice XML variables worker and phone.
(c)Example 5: SGRS XML with Multiple Rules
The downside of Example 4 is that there must be a match against the complete name. To provide options within options, a common technique is to define additional rules as a kind of subgrammar and reference them from within other rules. Example 5 illustrates how this can be done.
Listing 5 shows a grammar with two rules: a main rule for our top-level options and a secondary rule to show how to provide alternatives for customer service.
The problem we run into, however, is that when one of the alternatives for customer service is specified, we need to return a semantic meaning (i.e., return a value out of the rule so we can take action based on the fact that the user wants customer service). To accomplish this, we enclose the following (not very attractive) tag element within our item elements:
<tag> <![CDATA[ <worker "customer service"> ]]> </tag>
This has the effect of setting the value of the worker variable from within our custoservice rule.
Listing 5 A multirule SGRS XML grammar
1 <vxml version="2.0" xmlns=’http://www.w3.org/2001/vxml’> 2 <form id="getRequest"> 3 <var name="phone" expr="’unknown’"/> 4 <field name="worker" > 5 <prompt>Whose extension would you like?.</prompt> 6 <grammar type="application/srgs+xml" mode="voice" root="order"> 7 <rule id="order"> 8 <one-of> 9 <item>bob kirin</item> 10 <item>roger whitney</item> 11 <item>wilma rudolph</item> 12 <item> <ruleref uri="#custoservice"/> </item> 13 </one-of> 14 </rule> 15 <rule id="custoservice"> 16 <one-of> 17 <item>customer service 18 <tag> <![CDATA[ <worker "customer service"> ]]> </tag> 19 </item> 20 <item>service department 21 <tag> <![CDATA[ <worker "customer service"> ]]> </tag> 22 </item> 23 <item>service 24 <tag> <![CDATA[ <worker "customer service"> ]]> </tag> 25 </item> 26 <item>help 27 <tag> <![CDATA[ <worker "customer service"> ]]> </tag> 28 </item> 29 </one-of> 30 </rule> 31 </grammar> 32 <filled> 33 <if cond="worker == ’wilma rudolph’"> 34 <assign name="phone" expr="’8121’"/> 35 <elseif cond="worker == ’bob kirin’"/> 36 <assign name="phone" expr="’8306’"/> 37 <elseif cond="worker == ’roger whitney’"/> 38 <assign name="phone" expr="’9234’"/> 39 <elseif cond="worker == ’customer service’"/> 40 <assign name="phone" expr="’6234’"/> 41 </if> 42 </filled> 43 </field> 44 <block> 45 the number for <value expr="worker"/>. 46 is <value expr="phone"/>. 47 </block> 48 </form> 49 </vxml>
Some things to note about Listing 5:
- Line 6: Refers to the root rule of our grammar: root="order".
- Line 7: The root rule of the grammar with id="order".
- Line 12: Within the one-of selection, we refer to the custoservice rule. If that rule is satisfied, the root rule will also be satisfied, and control will pass to the filled element of the field.
- Line 15: Begins the custoservice rule, which also contains a one-of element. When any of these items are spoken, the associated tag element is visited, which sets our semantic meaning for the form’s worker variable. SGRS XML also allows other variables to be set here and made available to the form.
- Line 32: The filled element that is executed when the grammar is satisfied.
- Lines 39–40: We use the value of the worker variable to set the value of the phone variable.