Share via


grammar Element

  Microsoft Speech Technologies Homepage

Specifies input grammar resources.

<grammar name="gramName" src="src#ruleid" type="type" lang="lang" xmlns="nameSpace"/>

– or –

<grammar>
   inline grammar
</grammar>

The grammar element consists of the following members:

Contents Description
Inline grammar content Specifies inline grammar content in the format defined by the World Wide Web Consortium (W3C) Speech Recognition Grammar Specification (SRGS) format.
Attributes Description
name A string that uniquely identifies the grammar element within a listen or dtmf element. Optional.
src Specifies the Uniform Resource Identifiers (URI) of an external grammar. Optional.
type Specifies the MIME type that corresponds to the grammar format. Optional.
xmlns Declares a namespace for the grammar element and identifies the schema of the grammar format. This applies only to inline grammars. Optional
lang Specifies the language to which the grammar applies. Optional

Remarks

The grammar element is a child of either the listen element or the dtmf element. The grammar element can contain an inline grammar, that is, a grammar that is expressed between the opening and closing tags, or it can use the src attribute to point to a file containing a grammar definition. Users must specify at least one grammar for speech recognition and it can be either inline or referenced.

When referring to an external grammar file, it is possible to refer directly to a rule below the root rule using a standard syntax. Assume that the following external grammar file (airport.grxml) is being used.

<grammar version="1.0" tag-format="semantics-ms/1.0" xmlns="http://www.w3.org/2001/06/grammar" root="Airports" lang="en-US">
  <rule id="Airports">
    <one-of>
      <item>Heathrow</item>
      <item>Gatwick</item>
      <item>San Diego</item>
    </one-of>
    <one-of>
      <item repeat="0-1">
          <ruleref uri="#FlightDay" />
      </item>
    </one-of>
  </rule>
  <rule id="FlightDay">
    on
    <one-of>
      <item>Monday</item>
      <item>Wednesday</item>
      <item>Friday</item>
    </one-of>
  </rule>
</grammar>

There are two rules within this grammar. Use the following syntax to refer directly to the FlightDay rule.

<grammar src="airports.grxml#FlightDay">

When a listen element specifies multiple grammar elements, each grammar element is considered in a separate namespace for the purpose of grammar compilation. All the grammars of a listen element are active unless they are explicitly deactivated, or if the internal content changes the grammar to inactive.

A grammar used inline should declare a root. For a W3C SRGS grammar used by reference, the src attribute can include the rulename fragment.

  • If the src does not refer to a public rule of the grammar, the reference is in error.
  • If the rulename fragment is omitted, the root rule of the referenced grammar is implied.
  • If the referenced grammar has no root, the src reference is in error.

It is valid to have more than one reference to the same external grammar if each grammar refers to a different public rulename of that grammar (by the rulename fragment).

Example

The following code provides an example of an inline grammar. Note the use of rule fragments within the grammar.

<salt:grammar name="cities">
  <grammar version="1.0" tag-format="semantics-ms/1.0" lang="en-US"
  xmlns="http://www.w3.org/2001/06/grammar" root="root">
    <rule id="root">
      <ruleref uri="#cities"/>
    </rule>
    <rule id="cities">
      <one-of>
        <item>Seattle</item> 
        <item>Boston</item> 
        <item>Houston</item> 
        <item>New York</item> 
      </one-of>
    </rule>
  </grammar>
</salt:grammar>

See Also

bind Element |  dtmf Element |  listen Element