Sean McMains
Sean McMains

Reputation: 59223

Random Items in XSLT

I'm customizing a Google Search appliance, which uses XSLT to present results to the user. Our design calls for one of several images to be included randomly on the results page. Is there a way to use randomness in XSLT? (Pseudo-randomness is just fine for this application.)

Calling random templates would be fine, as would just being able to generate a random number and branch based on that.

Upvotes: 15

Views: 15061

Answers (6)

Volodya Lombrozo
Volodya Lombrozo

Reputation: 3466

The following simple solution greatly assisted me with XSLT 2 (generates random number):

sum(string-to-codepoints(generate-id($generated//random))

Or you can use it as a function:

<xsl:function name=“your:random-int" as="xs:integer">
  <xsl:variable name="generated">
    <random/>
  </xsl:variable>
  <xsl:value-of select="sum(string-to-codepoints(generate-id($generated//random)))"/>
</xsl:function>

Here's how it works:

  1. The generate-id() function generates a string containing the generated id, such as wfx2d123d8.
  2. The string-to-codepoints() function transforms the string obtained in the previous step into a list of nodes of ASCII codes of the string , similar to the following: (119, 102, 120, 50, 100, 49, 50, 51, 100, 56).
  3. Finally, the sum() function calculates the sum of all the numbers obtained in step 2, resulting in a single value

As you can observe, the random number generator mentioned is not reliable and lacks a uniform distribution. Therefore, I strongly advise against using it for important tasks. However, if you require a simple solution and are unconcerned about distribution, as was the case for me, it could potentially serve as an option.

Upvotes: 0

Defigo
Defigo

Reputation: 451

If you are doing this for anything Microsoft, I found that using XSLT's function ddwrt:Random works.

I use the following to create the random number

<xsl:variable name="RowCount" select="count($Rows)" />
<xsl:variable name="RandomNumber" select="ddwrt:Random(1, $RowCount)" />

and the following to present

<xsl:for-each select="$Rows[position() = $RandomNumber]">
<xsl:value-of select="@Title" /></xsl:for-each>

Upvotes: 1

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

You can generate in pure XSLT sequences of random numbers and also random permutations of the numbers in [1 .. N].

Just use the FXSL library (written in pure XSLT) for this.

This article explains the templates to use and has complete examples:

"Casting the Dice with FXSL: Random Number Generation Functions in XSLT".

Upvotes: 10

dacracot
dacracot

Reputation: 22358

If you use a Java based XSLT engine, this will allow you to make calls to any static method within the Java libraries, such as java.lang.Math.random(). Here is the syntax...

<?xml version='1.0'?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:math="java.lang.Math"
    version='1.1'>

    <xsl:template match="/">
        <xsl:variable name="myRandom" select="math:random()"/>
        <xsl:value-of select="$myRandom"/>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 4

Karmic Coder
Karmic Coder

Reputation: 17949

If you are not averse to including libraries, there are many available such as random:random-sequence from EXSLT

Upvotes: 2

No Refunds No Returns
No Refunds No Returns

Reputation: 8346

Depending on your platform XSL allows inject of user code like C#. I don't recommend this. Better, I would have your XSL accept a parameter and whatever is generating your XML payload or XSLT and can also generate the random number, setting the parameter. I've done this exactly using this approach except the data came from Bing, not G.

Upvotes: 5

Related Questions