The rule element defines a grammar rule. A rule element contains text or XML elements that define what speakers can say, and the order in which they can say it. The rule element associates a valid rule definition with a rule name and sets the scope of the rule definition. A valid rule element must contain at least one piece of recognizable text or one rule reference. A valid grammar must contain at least one rule element.
Syntax
XML | Copy Code |
---|---|
<rule id = "string" scope = (public | private) sapi:dynamic = (true | false) > </rule> |
Attributes
Attribute | Description |
---|---|
id | Required. Specifies the name that identifies the rule. The rule name must be unique within the containing grammar or rule namespace. The same rule name can be used in multiple grammars. The rule name is a case-sensitive character sequence that is a valid XML name, does not contain the characters ".", ":", or "-", and is not the name of a special rule (NULL, VOID, or GARBAGE). |
scope | Optional. Specifies whether the rule can be used only within its containing grammar or referenced by another grammar. If specified, this attribute must have one of the following values:
|
sapi:dynamic | Optional. Specifies whether the rule can be modified dynamically. Values are true or false. If specified, the containing grammar element must have the following attribute-value pair: xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions" |
Remarks
A rule element can contain unmarked text or any of the ruleref, item, one-of, token, or tag elements. A rule element cannot be empty or contain only white space; however, it can contain empty elements, thus making it empty in grammar logic.
A rule can be identified as the root rule of the grammar that contains it by using the root attribute on the grammar element. The root rule is the default rule used by a ruleref element when that reference specifies only a grammar, and not a specific rule name within that grammar.
Examples
The following example demonstrates that the publicRulerule can be referenced by a rule contained in an external grammar.
XML | Copy Code |
---|---|
<?xml version="1.0" encoding="utf-8"?> <grammar version="1.0" xml:lang="en-US" mode="voice" root="publicRule" xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0"> <rule id="publicRule" scope="public"> <item> This content is accessible by an external grammar. </item> </rule> </grammar> |
The following example demonstrates private rules. Although the rule named referenceYes is scoped as private, it can be referenced by an external grammar because it is the root rule of its containing grammar. The rule named referenceNo cannot be referenced by an external grammar.
XML | Copy Code |
---|---|
<?xml version="1.0" encoding="utf-8"?> <grammar version="1.0" xml:lang="en-US" mode="voice" root="referenceYes" xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0"> <rule id="referenceYes" scope="private"> <one-of> <item> Yes </item> <item> Yep </item> </one-of> </rule> <rule id="referenceNo" scope="private"> <one-of> <item> No </item> <item> Nope </item> </one-of> </rule> </grammar> |
The following example demonstrates how to reference rules using ruleref elements. The first ruleref element in the "CustomerInfo.grxml" grammar points to the rule named food in the grammar "RestaurantInfo.grxml". This is a valid rule reference even though the rule named food is scoped as private, because it is declared as the root rule of the "RestaurantInfo.grxml" grammar.
The second ruleref element in "CustomerInfo.grxml" references the public rule named location in "RestaurantInfo.grxml".
XML | Copy Code |
---|---|
<!-- Grammar file "customerInfo.grxml" --> <?xml version="1.0" encoding="utf-8"?> <grammar version="1.0" xml:lang="en-US" mode="voice" root="customers" xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0"> <rule id="customers"> <item repeat="0-1"> I would like </item> <ruleref uri="RestaurantInfo.grxml"/> <item repeat="0-1">in</item> <ruleref uri="restaurantInfo.grxml#location"/> </rule> </grammar> |
XML | Copy Code |
---|---|
<!-- Grammar file "RestaurantInfo.grxml" --> <?xml version="1.0" encoding="utf-8"?> <grammar version="1.0" xml:lang="en-US" mode="voice" root="food" xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0"> <rule id="food" scope="private"> <one-of> <item> Italian </item> <item> Thai </item> </one-of> </rule> <rule id="location" scope="public"> <one-of> <item> Bellevue </item> <item> Redmond </item> <item> Seattle </item> </one-of> </rule> </grammar> |