Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random Items in XSLT

Tags:

random

xslt

fxsl

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.

like image 644
Sean McMains Avatar asked Mar 01 '10 21:03

Sean McMains


2 Answers

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".

like image 89
Dimitre Novatchev Avatar answered Oct 26 '22 18:10

Dimitre Novatchev


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.

like image 44
No Refunds No Returns Avatar answered Oct 26 '22 19:10

No Refunds No Returns