Export (0) Print
Expand All

if

Performs conditional logic.

<if cond="ECMAScript_Expression">
   <!-- executable content -->
<elseif cond="ECMAScript_Expression"/>
   <!-- executable content -->
<elseif cond="ECMAScript_Expression"/>
   <!-- executable content -->
<else/>
   <!-- executable content -->
</if>

cond

Required. An ECMAScript expression that evaluates to true or false.

An if element may contain additional elseif elements and a single, final else element. Each conditional statement is evaluated in document source order until the expression associated with a cond attribute evaluates to true. When that occurs, the content that follows that conditional statement and that precedes the next conditional statement or closing if tag is executed.

To use a less-than (<), greater-than (>), or and (&&) condition in an if element, replace the symbols with the &lt; &gt;, or &amp;&amp; entities respectively.

The cond attributes considers expressions that evaluate to 0, -0, null, false, NaN, undefined, and the empty string to be false. All other values, including the strings "0" and "false", are equivalent to true.

The following example request the name of a flower from the user, compares the value returned by the grammar, and plays back audio about the flower.

<?xml version="1.0"?>
<vxml version="2.1"
 xmlns="http://www.w3.org/2001/vxml">
   <link event="event.exit">
   
   <grammar mode="voice"
         root="root_rule"
         tag-format="semantics/1.0"
         type="application/srgs+xml"
         version="1.0"
         xml:lang="en-US">
      <rule id="root_rule" scope="public">
         <one-of>
            <item>
               quit
            </item>
         </one-of>
      </rule>

   </grammar>

   
   <grammar mode="dtmf"
         root="root_rule"
         tag-format="semantics/1.0"
         type="application/srgs+xml"
         version="1.0">
      <rule id="root_rule" scope="public">
         <one-of>
            <item>
               *
               *
            </item>
         </one-of>
      </rule>

   </grammar>

   </link>

   <catch event="event.exit">
      <exit />
   </catch>

   <link event="help">
   
   <grammar mode="voice"
         root="root_rule"
         tag-format="semantics/1.0"
         type="application/srgs+xml"
         version="1.0"
         xml:lang="en-US">
      <rule id="root_rule" scope="public">
         <one-of>
            <item weight="0.5">
               help
            </item>
            <item weight="0.5">
               mercy
            </item>
         </one-of>
      </rule>

   </grammar>

   
   <grammar mode="dtmf"
         root="root_rule"
         tag-format="semantics/1.0"
         type="application/srgs+xml"
         version="1.0">
      <rule id="root_rule" scope="public">
         <one-of>
            <item>
               0
            </item>
         </one-of>
      </rule>

   </grammar>

   </link>

   <form id="pick_flower">
   <field name="flower">
      <prompt>
      Pick a flower
      </prompt>

      
      <grammar mode="voice"
         root="root_rule"
         tag-format="semantics/1.0"
         type="application/srgs+xml"
         version="1.0"
         xml:lang="en-US">
            <rule id="root_rule" scope="public">
                  <one-of>
                        <item>
                              <one-of>
                                    <item>
                                          rose
                                    </item>
                                    <item>
                                          roses
                                    </item>
                              </one-of>
                              <tag>out.flower = "rose";</tag>
                        </item>
                        <item>
                              <one-of>
                                    <item>
                                          violet
                                    </item>
                                    <item>
                                          violets
                                    </item>
                              </one-of>
                              <tag>out.flower = "violet";</tag>
                        </item>
                        <item>
                              <one-of>
                                    <item>
                                          grass
                                    </item>
                                    <item>
                                          grasses
                                    </item>
                              </one-of>
                              <tag>out.flower = "grass";</tag>
                        </item>
                        <item>
                              <one-of>
                                    <item>
                                          tulip
                                    </item>
                              </one-of>
                              <tag>out.flower = "tulip";</tag>
                        </item>
                  </one-of>
            </rule>

      </grammar>


      <!-- handle the first noinput for this field -->
      <catch event="noinput nomatch">
         I'm sorry. I didn't get that.
         <throw event="help"/>
      </catch>

      <!-- handle all help events for this field -->
      <help>
         Say rose, violet, grass, tulip. Say quit at any time to exit.
      </help>

      <filled>
         <if cond="'rose' == flower">
            roses are red
         <elseif cond="'violet' == flower"/>
            violets are blue
         <elseif cond="'tulip' == flower"/>
            tulips come in many colors
         <else/>      
            the grass is greener over here.
         </if>
         <clear/>
      </filled>

      </field>
   </form>
</vxml>
<?xml version="1.0"?>
<vxml version="2.1"
 xmlns="http://www.w3.org/2001/vxml">
   <link event="help">
      
      <grammar mode="voice"
         root="root_rule"
         tag-format="semantics/1.0"
         type="application/srgs+xml"
         version="1.0"
         xml:lang="en-US">
            <rule id="root_rule" scope="public">
                  <one-of>
                        <item weight="1.0">
                              help
                        </item>
                  </one-of>
            </rule>

      </grammar>

      
      <grammar mode="dtmf"
         root="root_rule"
         tag-format="semantics/1.0"
         type="application/srgs+xml"
         version="1.0">
            <rule id="root_rule" scope="public">
                  <one-of>
                        <item>
                              0
                        </item>
                  </one-of>
            </rule>

      </grammar>

   </link>

   <form id="pick_fruit">

   <field name="fruit">
      <prompt>
      Pick a fruit
      </prompt>

      
      <grammar mode="voice"
         root="root_rule"
         tag-format="semantics/1.0"
         type="application/srgs+xml"
         version="1.0"
         xml:lang="en-US">
            <rule id="root_rule" scope="public">
                  <one-of>
                        <item>
                              <one-of>
                                    <item>
                                          apple
                                    </item>
                              </one-of>
                              <tag>out.fruit = "apple";</tag>
                        </item>
                        <item>
                              <one-of>
                                    <item>
                                          orange
                                    </item>
                              </one-of>
                              <tag>out.fruit = "orange";</tag>
                        </item>
                        <item>
                              <one-of>
                                    <item>
                                          pear
                                    </item>
                              </one-of>
                              <tag>out.fruit = "pear";</tag>
                        </item>
                        <item>
                              <one-of>
                                    <item>
                                          repeat
                                    </item>
                              </one-of>
                              <tag>out.fruit = "repeat";</tag>
                        </item>
                  </one-of>
            </rule>

      </grammar>


      <!-- handle all help events for this field -->
      <help>
         
         You're picking a fruit. 
         To pick a fruit, say apple, orange, or pear.
         
         <reprompt/>
      </help>

      <!-- handle all noinput events for this field -->
      <noinput>
         I'm sorry. I didn't hear you.
         <reprompt/>
      </noinput>

      <!-- handle all nomatch events for this field -->
      <nomatch>
         I'm sorry. I didn't get that.
         <reprompt/>
      </nomatch>

      <filled>
      <log>Recognized <value expr="fruit"/></log>
      <if cond="'repeat' == fruit">
         <clear namelist="fruit"/>
         <reprompt/>
      <else/>
         you chose <value expr="fruit"/>
         <exit />
      </if>
      </filled>

      </field>
   </form>
</vxml>

The following example simulates an "at bat" in baseball. The BatterUp dialog calls a user-defined function pickRandNum to store a random integer between 1 and 3 in the variable iPitchRes. Conditional logic inside the named block EvalPitch tests the value of iPitchRes to determine how the batter is doing. If the batter accumulates three strikes, she's out. If the batter accumulates four balls, she walks. If the batter hits, she gets on base. By using the clear element to clear EvalPitch after each pitch, the VoiceXML interpreter continues to re-execute the block. While this simple example doesn't track outs or runs, you're free to extend the example to more accurately simulate a baseball game.

<?xml version="1.0"?>
<vxml version="2.1"
 xmlns="http://www.w3.org/2001/vxml">
<!-- constants -->
<var name="iMaxStrikes" expr="3"/>
<var name="iMaxBalls" expr="4"/>

<script>
function pickRandNum(max) 
{
   return Math.floor(Math.random()*parseInt(max, 10))+1;
}
</script>

<form id="PlayBall">
<block>
   Play ball!
   <goto next="#BatterUp"/>
</block>
</form>

<form id="BatterUp">
<!-- keep the current batter's count -->
<var name="iCurStrikes" expr="0"/>
<var name="iCurBalls" expr="0"/>

<block>
   Batter up!
</block>

<block name="EvalPitch">
   <!-- simulate pitch: 1 == ball, 2 == strike, 3 == hit -->
   <var name="iPitchRes" expr="pickRandNum(3)"/>

   <if cond="1 == iPitchRes">
      <assign name="iCurBalls" expr="iCurBalls+1"/>
      Ball <value expr="iCurBalls"/>!
      <if cond="iCurBalls == iMaxBalls">
         <goto next="#Walk"/>
      </if>
   <elseif cond="2 == iPitchRes"/>
      <assign name="iCurStrikes" expr="iCurStrikes+1"/>
      Stee rike <value expr="iCurStrikes"/>!
      <if cond="iCurStrikes == iMaxStrikes">
         <goto next="#BigK"/>
      </if>
   <elseif cond="3 == iPitchRes"/>
      <goto next="#Hit"/>
   <else/>
      <log>Unknown pitch result: <value expr="iPitchRes"/></log>
   </if>

   <!-- clear form item so FIA returns to it -->
   <clear namelist="EvalPitch"/>
 
</block>
</form>

<form id="Walk">
<block>
   Batter walks!
   <break/>
   Let's have a new pitcher!
   <goto next="#WhatNext"/>
</block>
</form>

<form id="BigK">
<block>
   Yer out!
   <break/>
   Trade that batter!
   <goto next="#WhatNext"/>
</block>
</form>

<form id="Hit">
<block>
   Hit!
   Give that batter a bonus!
   <goto next="#WhatNext"/>
</block>
</form>

<form id="WhatNext">
<field type="boolean" name="yesno">
<prompt>Care to simulate another batter?</prompt>
<catch event="noinput nomatch">
   Sorry. I didn't get that.
   <reprompt/>
</catch>
<filled>
   <if cond="yesno">
      <goto next="#BatterUp"/>
   <else/>
      <exit />
   </if>
</filled>
</field>
</form>

</vxml><?xml version="1.0"?>
<vxml version="2.1"
 xmlns="http://www.w3.org/2001/vxml">

   <link event="help">
      
      <grammar mode="voice"
         root="root_rule"
         tag-format="semantics/1.0"
         type="application/srgs+xml"
         version="1.0"
         xml:lang="en-US">
            <rule id="root_rule" scope="public">
                  <one-of>
                        <item weight="1.0">
                              help
                        </item>
                  </one-of>
            </rule>

      </grammar>

      
      <grammar mode="dtmf"
         root="root_rule"
         tag-format="semantics/1.0"
         type="application/srgs+xml"
         version="1.0">
            <rule id="root_rule" scope="public">
                  <one-of>
                        <item>
                              0
                        </item>
                  </one-of>
            </rule>

      </grammar>

   </link>

   <form id="pick_fruit">

   <field name="fruit">
      <prompt>
      Pick a fruit
      </prompt>

      
      <grammar mode="voice"
         root="root_rule"
         tag-format="semantics/1.0"
         type="application/srgs+xml"
         version="1.0"
         xml:lang="en-US">
            <rule id="root_rule" scope="public">
                  <one-of>
                        <item>
                              <one-of>
                                    <item>
                                          apple
                                    </item>
                              </one-of>
                              <tag>out.fruit = "apple";</tag>
                        </item>
                        <item>
                              <one-of>
                                    <item>
                                          orange
                                    </item>
                              </one-of>
                              <tag>out.fruit = "orange";</tag>
                        </item>
                        <item>
                              <one-of>
                                    <item>
                                          pear
                                    </item>
                              </one-of>
                              <tag>out.fruit = "pear";</tag>
                        </item>
                        <item>
                              <one-of>
                                    <item>
                                          repeat
                                    </item>
                              </one-of>
                              <tag>out.fruit = "repeat";</tag>
                        </item>
                  </one-of>
            </rule>

      </grammar>


      <!-- handle all help events for this field -->
      <help>
         
         You're picking a fruit. 
         To pick a fruit, say apple, orange, or pear.
         
         <reprompt/>
      </help>

      <!-- handle all noinput events for this field -->
      <noinput>
         I'm sorry. I didn't hear you.
         <reprompt/>
      </noinput>

      <!-- handle all nomatch events for this field -->
      <nomatch>
         I'm sorry. I didn't get that.
         <reprompt/>
      </nomatch>

      <filled>
      <log>Recognized <value expr="fruit"/></log>
      <if cond="'repeat' == fruit">
         <clear namelist="fruit"/>
         <reprompt/>
      <else/>
         you chose <value expr="fruit"/>
         <exit />
      </if>
      </filled>

      </field>
   </form>
</vxml>

Community Additions

ADD
Show:
© 2014 Microsoft