Home > Articles > Web Services > XML

  • Print
  • + Share This
Like this article? We recommend

JavaScript and Client-Side Data Lookup

In the previous examples, we’ve seen how to use JavaScript to carry out some basic calculations for us. But wait—that’s not all! Let’s now look at how to use JavaScript to do client-side data lookup. Just as many web pages use JavaScript to replace server-side processing so we can use JavaScript within Voice XML to do client-side processing, possibly eliminating the need for a data server.

Let’s continue with our deal-making theme and create a Voice XML application that provides background information on some of the personalities we’ll encounter in our negotiations. One of the things that helps get discussions off to a good start is a bit of personal schmoozing: How’s the spouse? How’s your son Jimmy? And so on.

Of course, when there’s a cast of characters, it’s often tricky to keep the family members straight. What we need is a bit of JavaScript that can store data for us so when we need to find out how many kids Joe has, we can just call. Here’s a dialogue with the voice assistant:

C: For whom do you want the scoop?
U: Max
C: Here’s the story on Max.
  Max, engaged to Marla. wedding next june. That’s the scoop boss.

Now you’re prepared to greet Max and tell him how excited you are about his upcoming marriage to Marla. Here’s the JavaScript that does the lookup. We provide the name as parameter and the JavaScript returns relevant personal information.

function findUser(user) {
 var name= new Array(5)
 name[0]="joe";
 name[1]="moe";
 name[2]="ronda";
 name[3]="max";
 name[4]="sven";
 
 var detail= new Array(5)
 detail[0]="joe. wife is mary, kids are bing and bong";
 detail[1]="moe, wife is zelda. no kids.";
 detail[2]="ronda, husband is zeke. first baby due in march.";
 detail[3]="max, engaged to marla. wedding next june.";
 detail[4]="sven, one child, duke, just got divorced."; 
    
 var i=0;
 for (i=0; i<5; i++){
 if( name[i] == user) return detail[i];
 } 
}

In the above JavaScript, we’re setting up two parallel arrays, one for keys and the other for values. The name array holds the keys; the detail array holds the data we need. Once we figure out the index of the key spoken by the user, we simply return the value in the detail array with the same index.

The complete voice document appears in Listing 3.

Listing 3 Voice XML person profile lookup

<?xml version="1.0" encoding="UTF-8"?> 
<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/Voice XML20/vxml.xsd"> 
  
  <script> <![CDATA[ 
       
    function findUser(user) {
    
    var name= new Array(5)
     name[0]="joe";
     name[1]="moe";
     name[2]="ronda";
     name[3]="max";
     name[4]="sven";
     
     var detail= new Array(5)
     detail[0]="joe. wife is mary, kids are bing and bong";
     detail[1]="moe, wife is zelda. no kids.";
     detail[2]="ronda, husband is zeke. first baby due in march.";
     detail[3]="max, engaged to marla. wedding next june.";
     detail[4]="sven, one child, duke, just got divorced."; 
    
    var i=0;
    for (i=0; i<5; i++)
    {
     if( name[i] == user) return detail[i];
    } 
   }
    
    
  ]]> </script> 
 
 <form id="personProfile">  
  <field name="person" >
      <prompt>for whom do you want the scoop?</prompt>
      <grammar type="application/x-gsl" mode="voice">
    <![CDATA[
     [
      [(rollo) (rollo jones) (bighead)]  {<person "rollo">}
      [(moe) (stooge)]           {<person "moe">}
      [(ronda) (ronda marko)]       {<person "ronda">}
      [(max) (max headroom)]        {<person "max">}
      [(sven) (sven sorensen)]       {<person "sven">}

    ]
    ]]>
    </grammar>

      <noinput>not getting any input.</noinput>
      <nomatch>We’ve got rollo, moe, ronda, max and sven on file. 
       try again.</nomatch>
  </field>
  
  <filled> 
      <prompt> 
      Here’s the story on <value expr="person"/>.
      <value expr="findUser(person)"/>
      That’s the scoop boss.
    </prompt> 
  </filled> 	 
 </form>
  
</vxml>

Some things to note about this example: To allow our user to speak the names of the players, we defined a grammar that maps voice input to the keys in the JavaScript. Note that the grammar provides alternative ways to speak the individual names.

If you really need to be surreptitious, you can add code phrases to the grammar so that anyone overhearing you won’t have a clue what you are talking about. For example, instead of saying Max’s name aloud, you could define the grammar to map "See you later tonight dear" to the value "Max".

Looking at this example, you might ask this: Why not just jot down the personal profiles on a sheet of paper, slip it in your pocket and forget the Voice XML? That is certainly an option, of course. But keep in mind that the power of Voice XML lies in its capability to provide another dimension to how you may already be doing business. And with cell phones part of everyone’s wardrobe, adding voice as a service delivery option gives you yet another potential leg up over the competition.

Let’s now look at another powerful option—using server-side data to generate Voice XML on the fly.

  • + Share This
  • 🔖 Save To Your Account