Solved

pick random nodes from xml doc

Posted on 2010-11-20
2
452 Views
Last Modified: 2012-05-10
I have a page with 3 spaces for information pulling from an xml file. My xml file has 20 nodes of different  information.
What can I put on my page in javascript or asp to pull a random different 3 each time the page loads?
0
Comment
Question by:rivkamak
2 Comments
 
LVL 1

Expert Comment

by:him6ul
ID: 34182169
You can write an algorithm using Random number implementation to give you three unique random numbers between 1 and 20 everytime you call that function.

Then you can pass those numbers to your processor which is processing XML node and it can give you the XML nodes data corresponding to those three numbers.
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 34182171

Without knowing your xml structure, the following picks three values from an array:

<script>
// http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q__26629425.html

var nodes = ["one","two","three","four","five"
  ,"six","seven","eight","nine","ten"
  ,"eleven","twelve","thirteen","fourteen","fifteen"
  ,"sixteen","seventeen","eighteen","nineteen","twenty"];
var pick = Array();

while ( pick.length < 3 )
{
  var index = randomTo( 20 );
  while ( isIn( pick, index ) )
  {
    index = randomTo( 20 );
  }
  pick[pick.length] = nodes[index];
}
alert(pick);

function randomTo( modulo )
{
  return Math.floor( Math.random() * new Date().getMilliseconds() ) % modulo;
}

function isIn( array, value )
{
  result = false;
  for ( var i = 0; i < array.length; i++ )
  {
    if ( array[i] == value )
    {
       result = true;
       break;
    }
  }
  return result;
}
</script>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
The viewer will learn how to dynamically set the form action using jQuery.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now