VoiceXML 2.1 Development GuideHome  |  Frameset Home


<if>  element

The if element, (in conjunction with the else/elseif elements), provides a method to utilize conditional logic expressions which allow the developer to change the control flow within the application based on user utterances, variable values, or events.



usage
<if cond="CDATA">


attributes
condData Type: CDATADefault: Required
The cond attribute specifies any valid expression, which equates to the value to be evaluated.



shadow variables
none


parents
<block> <catch> <error> <filled> <help> <if> <noinput> <nomatch>


children
<assign> <audio> <clear> <data> <disconnect> <else> <elseif> <enumerate> <exit> <goto> <if> <log> <prompt> <reprompt> <return> <script> <submit> <throw> <value> <var>


code samples
<If-Else-ElseIf> sample
<?xml version="1.0" encoding="UTF-8"?>

<vxml version = "2.1">

<meta name="author" content="Matthew Henry"/>
<meta name="copyright" content="2005 voxeo corporation"/>
<meta name="maintainer" content="YOUR_EMAIL@HERE.COM"/>


<form id="F1">
  <field name="F_1">
    <prompt> who would win in a fist fight, Oprah, Madonna, or James Brown? </prompt>
    <grammar type="text/gsl">[ oprah madonna (james brown) ] </grammar>

    <filled>
      <if cond="F_1=='oprah'">
        <prompt>
          you really think Oprah could duke it out
          with the Godfather of soul?
        </prompt>

      <elseif cond="F_1=='madonna'"/>
        <prompt>
          madonna would get creamed, who do
          you think you are kidding?
        </prompt>

      <else/>
        <prompt>
          James Brown would whip up on both
          Oprah and Madonna
        </prompt>

      </if>
    </filled>
</field>

</form>
</vxml>



additional links
W3C 2.0 Specification


  ANNOTATIONS: EXISTING POSTS
DaveMorris
2/6/2006 7:11 PM (EST)
You should mention that to use a less-than (<), greater-than (>), or and (&&) condition in a if element, you must replace the symbols with the escaped versions, i.e. ampersand LT semicolon, etc.  Otherwise you'll get a error.semantic XML parse error at runtime.  Took me 8 hours of wasted time to discover this.

Dave

kevinlim
2/6/2006 9:16 PM (EST)
Dave,

Thanks for the input. 

We will look into compiling a list of common practices. 

As with any other markup languages that use special characters, escape characters are necessary to disambiguate your code. 

A complete specification of this is found in the W3C XML page: http://www.w3.org/TR/REC-xml/#syntax

When generating VoiceXML, it's always good to view the source produced in a webbrowser, so you can see what the VoiceXML browser sees. 

If you have any other comments or questions, feel free to contact us: support@voxeo.com

Best,
Kevin

lambchops7
7/18/2007 10:40 AM (EDT)
What if we wanted the if to satisfy any number of conditions.
Will this suffice?  It doesn't seem to work for me.
  <filled>
      <if cond="F_1=='oprah' || 'one' || 'DTMF-1'">
        <prompt>
          you really think Oprah could duke it out
          with the Godfather of soul?
        </prompt>
      </if>
  </filled>
voxeojeff
7/18/2007 12:50 PM (EDT)
Hi there,

This is certainly possible.  I'll attach a sample script below that will allow such a condition:

<?xml version="1.0" encoding="UTF-8"?>
<vxml version = "2.1">
<form id="F1">
  <field name="F_1">
    <prompt> Please pick a number between one and six </prompt>
    <grammar type="text/gsl">[ one two three four five six ] </grammar>
    <filled>
      <if cond="F_1=='one' || F_1=='two' || F_1=='three'">
        <prompt>
          You said <value expr="F_1"/>
        </prompt>
      </if>
    </filled>
</field>
</form>
</vxml>

I hope this helps!

Cheers,

Jeff Menkel
Voxeo Corporation
lambchops7
8/3/2007 12:14 PM (EDT)
How would you write the logic if you want two conditions to be true.  I don't understand the first response about replacing it with the escaped versions "ampersand".

You can imagine here that I'm getting someone's username:
<filled namelist="u_check">
  <if cond="u_check=='1' ampersand usr=='1234'"><goto next="#Password"/>
  <elseif cond="u_check=='2'"/> <goto next="#Username"/>
  <else/>
    <prompt>  This is an incorrect username.  Please try again. </prompt>
    <goto next="#Username"/>
  </if>
</filled>
usr is a variable that I declared at the beginning of the document to keep track of the usrname.

I tried && or ampersand or ampersandampersand, and I can't figure it out.  How do you write the logic?
mikethompson
8/3/2007 12:22 PM (EDT)
Hello Lambchop,

If you would like more than one condition to be met in order to have the content be executed, you want to structure your if statement like so:

<if cond="F_1=='one' || F_1=='two' || F_1=='three'">

As for ampersand encoding, if && does not work, you can try "&amp;&amp;".

Hope this helps,
Mike Thompson
Voxeo Corporation
n2731568
10/12/2007 11:55 PM (EDT)
Is it possible make your condition in the "if" tag be "not equal to" i.e. somthing like !=. I can't seem to find this type of condition anywhere.

Cheers
Chris
voxeojeremy
10/13/2007 12:04 AM (EDT)
Hey Chris!

Here's how to do is not equal to -

Let's say we want to exit on every condition where F_1 is not equal to "moo".  Here's the code for it:

-----------------------------------------------
  <field name="F_1">
    <prompt> are cows great? </prompt>
    <grammar type="text/gsl">[ moo boo foo ] </grammar>

    <filled>
      <if cond="F_1=='moo'">
        <prompt>
          well moo to you.
        </prompt>

      <else/>

        <exit/>

      </if>
    </filled>
  </field>
-----------------------------------------------
chuchoomar
5/12/2008 6:49 PM (EDT)
<filled>
    <if cond="confirm=='si'">        can i do that?
    <prompt xml:lang="es-es">
    lindo muņeco
    </prompt>
    /else>
    <goto nextitem="nombre_usuario"/>
    </if>


hey i need to know which are the diferents cojunctions....for instance  how i can do ...the and, or and diferent condition in the element if....


||....what is the meaning?

&&& what is the meaning?
voxeoblehn
5/13/2008 10:20 AM (EDT)
Hello,

This -[code] <if cond="confirm=='si'">[/code] - simply checks if the value of confirm is equal to  the string 'si', which is fine.  Your syntax should work, except for the missing bracket in your <else/> statement :)

As for the ampersand (&&) and ||, these are conditional operators in ECMAScript.

&& is the AND operator, meaning that both conditions must evaluate to true for the statement to be true. For example:
[code]
<if cond="1 && 1 == 1"> <!-- this is true, as both 1 and 1 are equal to 1 -->

<if cond="1 && 2 == 1"> <!-- this is fale, as 1 is equal to 1, but 2 is not equal to 1 -->
[/code]

|| is the OR operator, meaning that if either condition evaluates to true, then the statement is true. For example:

[code]
<if cond="1 || 2 == 1"> <!-- this will evaluate to true. 2 does not equal 1, but 1 does, so at least one of our conditions is met. -->

<if cond="2 || 3 == 1"> <!-- this is false, as neither 2 or 3 equal 1. -->
[/code]

One of the great things about CCXML and VoiceXML is the integration of ECMAScript(or JavaScript). You can find more operators and more info on them here:

http://www.w3schools.com/js/js_operators.asp
http://www.w3schools.com/js/js_comparisons.asp
http://www.w3schools.com/js/js_if_else.asp

Best Regards,

Brian Lehnen
Voxeo Support
jefo12
6/18/2008 12:32 PM (EDT)
<if cond="callerid.length == 10 &amp;&amp; callerid != 'restricted'">
          <log expr="'***** CALLER ID: ' + callerid"/>
          <submit next="mydoc.jsp" namelist="callerid"/>
      <else/>
          <log expr="'***** CALLER ID NOT PRESENT'"/>
          <goto nextitem="enterPhone"/>
      </if>

if cond="callerid.length == 10 &amp;&amp; callerid

i can't understand this can u plz explain it clearlly
voxeojohnq
6/18/2008 1:13 PM (EDT)
Hello,

<if cond="callerid.length == 10 &amp;&amp; callerid != 'restricted'">
<log expr="'***** CALLER ID: ' + callerid"/>
<submit next="mydoc.jsp" namelist="callerid"/>
<else/>
<log expr="'***** CALLER ID NOT PRESENT'"/>
<goto nextitem="enterPhone"/>
</if>

What's happening here is some fancy conditional logic.  We have two conditions that BOTH need to be met in order to successfully execute a piece of code with some specific requirements.  In this case, "mydoc.jsp" needs the variable "callerid" to have a specific string length and it also must not be equal to the string "restricted".  If both of those conditions are not met, the next point in the program that will be executed is what is in between the <else /> and the </if> elements.  The <else> section will now goto the next <form> or <block> element labeled "enterPhone".

Pretty easy eh?

OK, to add a little more clarification to the above code.

== is the equality operator.  This will test to see if the left side is equal to the right side of the equation.

!= is the inequality operator.  This will test to see if the left side is NOT equal to the right side of the equation.

&& is the AND operator.  This will test to see if the results of the two, (or more), equality tests are both met.  Since this code is formatted in a well-formed XML document, we need to encode the ampersands as &amp;

You may want to read a little bit about boolean logic.  The wikipedia is a good place to start for a thorough overview of what it is.

http://en.wikipedia.org/wiki/Boolean_logic

Regards,
John Quinn
Voxeo Support
jefo12
6/19/2008 12:53 AM (EDT)
Thank u very much john ..
jefo12
6/19/2008 2:50 AM (EDT)
HI JOHN CAN U PLZ CHECK THIS OUT
<vxml>
<var name="callerid" expr="session.callerid"/>
<form id = "callerid">
<field name="caller">
<grammar type= text/gsl mode="dtmf">
[dtmf-1 dtmf-2]  </grammar>
<prompt> if you are a new user press 1 else if u hav already an appointment press 2
</prompt>
<filled>
<block>

<If cond="caller=='dtmf-1'">
<goto next="F1">
<else/ >
<goto next ="welcome">
</filled>
<form id="welcome">
<field name="cancelres">
<grammar type="text/gsl">
[cancel reschedule]</grammar>
<prompt > wat do u want to cancel or reschedule the appointment </prompt>
<filled>
  <block>
<subdialog name="getdetails" src="mydoc.jsp"/> //WHERE TO STORE THE VALUES COMING FROM JSP AND HOW TO PLAY THE CORRESPONDING DATE AND TIME..
      </block>
  </field>
</form>


<form id ="newapp">
<field name="consult">
<prompt bargein="false"> welcome to abc phone appointmentscheduling.please request the date for your appointment.
To request an appointment this week you can say today,tomorrow ,day after tomorrow or a specific day such as this Friday.you can also request the day next week such as next Friday.plz make ur request now Such as today,tomorrow,day after tomorrow,this Friday or next Friday.
</prompt>

<grammar type="text.gsl">
[today tomorrow dayaftertommorrow Sunday Monday Tuesday Wednesday Thursday Friday Saturday (next Sunday) (next Monday) (next Tuesday) (next Wednesday) (next Thursday) (next Friday) (next Saturday) morning afternoon evening]
</grammar>

//IS THERE ANY EASY WAY WITHOUT WRITING ALL THESE DAYS...

//IF THE USER SAYS A  SPECIFIC DATE SUCH AS MARCH 2ND HOW TO INCLUDE GRAMMAR FOR THAT

<filled>
<block>
<goto nextitem="day">
</block>
<filled>
<field name="day">
<grammar type="text.gsl">
[morning evening] </grammar>

<prompt>please indicate your time facult by saying morning or afternoon</prompt>

<filled>
<subdialog name="availabletimes" src="mypage.jsp">//HOW TO play all available timings on that date..
</subdialog>
<field name="confirm" type="boolean">
</filled>

<block>
<if cond="confirm=='yes'">
Then play the exact appointment day,date and time..
<elseif cond="confirm='no'">
<goto nextitem="day">
</block>
</field>
</form>

</vxml>
ABOVE IS THE CODE FOR APPOINTMENT SCHEDULING PLZ VERIFY WHETHER IT IS VALID OR NOT ..IF NOT GIV ME THE EFFICIENT VXMLCODE.


VoxeoDustin
6/19/2008 9:35 AM (EDT)
Hey Jefo,

Attached is grammar that supports date entry, including day of the week, day, month and year.

Give this a try and let us know if it's what you're looking for.

Cheers,
Dustin

login



© 2008 Voxeo Corporation  |  Voxeo IVR  |  VoiceXML & CCXML IVR Developer Site