p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

xslt thread: random sorting


Message #1 by "Cory Upton" <cupton@b...> on Wed, 25 Jul 2001 15:28:17
Calling the random number generator within the sort expression is probably a
bit hairy: some processors may evaluate the sort key more than once for a
given node, and they could go wrong if it gives different answers. A safer
approach would be to create a document containing all the random numbers you
need, and then pick one from the list. But I may be paranoid - i't probably
OK on most processors.

I can't immediately see what you're doing wrong, but remember that the way
of calling external Java functions is different in each processor; they
don't all implement the XSLT 1.1 xsl:script element.

Mike Kay
Software AG

> -----Original Message-----
> From: Cory Upton [mailto:cupton@b...]
> Sent: 25 July 2001 15:28
> To: P2P_XSLT
> Subject: [xslt] random sorting
>
>
> My problem is......
>
> I am creating a survey using XML, XSLT, and Sun JAXP.
>
> I need to have the ability to do random group sorting.
> If we build a survey that has 5 questions and we give each
> question an
> order attribute like this.
>
> <survey>
> 	<question order="1"></question>
> 	<question order="2"></question>
> 	<question order="2"></question>
> 	<question order="2"></question>
> 	<question order="3"></question>
> </survey>
>
> The first question should come first, the three in the middle
> should be
> randomized and the last should be last.
>
> What I am trying is.....
>
>
> 1.  Created Java class RandomNumber.java.
> 	public class RandomNumber
> 	{
> 	    private static java.util.Random random;
> 	    static
> 	    {
> 		random = new
> java.util.Random(System.currentTimeMillis());
> 	    }
> 	    public static long nextRandom()
> 	    {
> 		return random.nextLong();
> 	    }
> 	}
>
> 2. I want to do something like this.  Call the Java class
> from within XSLT.
>
>
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="1.0"
>     xmlns:Random="java:RandomNumber"
> >
> <xsl:script implements-prefix="Random" language="java"
> src="java:RandomNumber"/>
>
>
> ...
> ...
>
> <xsl:sort
> select="number(concat(question/@order,'.',Random.nextRandom()))"
> data-type="number"/>
>
>
> My hope was we would sort on something like
> [1.87263978,2.98745,2.597364,2.472364,3].
>
>
> I cant get the external function call to work and I am curious
> to see if any of you think the sort has any chance of working. :-)
>
>
> Thanks in advance!!!!!
> ---
> When you work at Microsoft, you don't have to wait for the
> latest technologies to be released. You'll create them ? with
> XML, SOAP, and C# -- and help decide what happens next.
> Find out where your career can take you: take a look at our
> software design engineer opportunities at
> http://www.microsoft.com/jobs.
>


  Return to Index