[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: how to output elements in random order?

Subject: Re: how to output elements in random order?
From: Oliver Becker <obecker@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 1 Feb 2002 09:44:18 +0100 (MET)
xsl sort random
Hi Massimo,

> Is there a standard (clean) way to process (output) elements in random
> order, possibliy specifiyng a "seed" as a parameter to the stylesheet?
> 
> It's not just a speculation. One motivation, for example, is that I'm
> tryng to use XSLT to produce randomly premuted multiple choice tests for
> grading students. Given a set of questions each one with a various number
> of possible answers, I wuold like to output questions in random order and
> answers within a quastion in random order too.

Well, it is just a pseudo random number. But you cannot expect more than this.
So, how to compute this pseudo number from the input data and a seed parameter?

What about this: compute $seed divided by the string length of the node
and pick some of the digits in the fraction part as sort key. If some
nodes (questions) have the same length, you can also include the position
in your computation.

Example:
<xsl:param name="seed" select="1" />

<xsl:template match="test">
  <xsl:for-each select="question">
    <xsl:sort select="substring(substring-after(
                         $seed div (position() * string-length()), '.'),
                      3, 4)" 
              data-type="number" />
    <xsl:value-of select="." />
  </xsl:for-each>
</xsl:template>

Cheers,
Oliver


/-------------------------------------------------------------------\
|  ob|do        Dipl.Inf. Oliver Becker                             |
|  --+--        E-Mail: obecker@xxxxxxxxxxxxxxxxxxxxxxx             |
|  op|qo        WWW:    http://www.informatik.hu-berlin.de/~obecker |
\-------------------------------------------------------------------/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.