cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Announcements
1175
Views
0
Helpful
1
Replies
Highlighted
Beginner

XPATH to select a random node

Hi

I'm trying to play a random prompt from a list of available prompts.

I recognise that the 'Generate Prompt' step can achieve this but my prompts in question are going to be sitting in an XML file (see below)


<?xml version="1.0" encoding="ISO-8859-1"?><Config xml:lang="en-gb">
 <Campaigns>
  <Campaign ID="1">
   <P ID="1">Paul\2 Welcome Prompts\paul2000.wav</P>
   <P ID="2">Paul\2 Welcome Prompts\paul2001.wav</P>
   <P ID="3">Paul\2 Welcome Prompts\paul2002.wav</P>
   <P ID="4">Paul\2 Welcome Prompts\paul2003.wav</P>
   <P ID="5">Paul\2 Welcome Prompts\paul2004.wav</P>
  </Campaign>
  <Campaign ID="2">
   <P ID="1">Paul\2 Welcome Prompts\paul2000.wav</P>
   <P ID="2">Paul\2 Welcome Prompts\paul2001.wav</P>
  </Campaign>  
 </Campaigns>
</Config>


Can someone advise on the best way to select an element at random using as few steps as possible?

Many thanks

Rob

1 REPLY 1
Highlighted
Collaborator

Hi Rob,

I think the best way would be to use a Java method to calculate a random number from 1 to however many prompts you have in the XML and use that in constructing your XPath statement.

I have written a script for the XML shown below. The Elements XML value is an integer that  that the script reads do determine how many "P" elements there are.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Campaigns>
<Campaign ID="1">
<Elements>9</Elements>
<P ID="1">abc</P>
<P ID="2">bcd</P>
<P ID="3">cde</P>
<P ID="4">def</P>
<P ID="5">efg</P>
<P ID="6">fgh</P>
<P ID="7">ghi</P>
<P ID="8">hij</P>
<P ID="9">ijk</P>
</Campaign>
</Campaigns>

The script the uses the Java snippet below to generate a random number

iRND=(Math.random() * iElements+1)

It then uses this number to create the XPath query

If you load up my script (you will have to change the dCampaign variable path to match where you store the XML file) and dial it you should hear it randomly playing the contents of the XML file - i.e. abc, hij, ghi etc.

Let me know how you get on.

James

Content for Community-Ad