displaying a list of child nodes in a random sort order?
I've been trying to get a list of child nodes displaying in random order, and made progress up to a point. It seemed that my xslt code for generating the random number was crashing the IIS worker process.
Is there any reliable way to generate a random number within a given range in xslt, or do I need to be looking at alternative methods? I've found plenty here on displaying random images etc. but it's not quite what I'm after.
My approach was as follows:
count the child nodes generate a unique random number for each node between 0 and the number of child nodes. Assign that number to each node (for-each loop) as a variable called sort order or similar. Sort the nodes based on that number.
Is there anything horribly wrong with trying to do it this way?
To put it in context - this is for a client site where they have a list of sponsors. The sponsors need to display in a random order each time the page is loaded, so that nobody can get accused of favoritism. It sounded fairly trivial but I've hit a huge snag on the RNG stuff.
Thanks guys, I've tried the code but am getting this error - should there be any other line for the rdm namespace or similar?: System.Xml.Xsl.XslLoadException: Prefix 'rdm' is not defined.
Out of interest - is the umbraco.library.getrandom() function still valid? I found one site that said it wasn't usable in xslt, but I couldn't get it to work or be recognized at all.
Thanks! That got rid of my error, but as with the first example I tried, my random numbers are all displaying as zero. Something does not seem to be working correctly when I call any kind of script from within the xslt. This should be possible, right? I'm wondering if it's a server config issue.
If I change the random number code to simply return 1, or perform the random calculation +1, it returns 1 so it seems to be getting into that function ok just not processing the javascript.
I reset the permissions and that seemed to do the trick. The page loads nice and fast and hasn't crashed the worker process. Thanks a ton for that code snippet, I've got my count() stuff in place and it seems to be working perfectly.
As promised here my approach I am using to display images on a frontpage in a random order. I took the code from some posts in the old and new forum, but I don#t know the original locations anymore.
displaying a list of child nodes in a random sort order?
I've been trying to get a list of child nodes displaying in random order, and made progress up to a point. It seemed that my xslt code for generating the random number was crashing the IIS worker process.
I tracked it down to my random number generation code which I got from here: http://blogs.msdn.com/markhsch/archive/2009/04/20/random-number-generation-using-xslt.aspx
Is there any reliable way to generate a random number within a given range in xslt, or do I need to be looking at alternative methods? I've found plenty here on displaying random images etc. but it's not quite what I'm after.
My approach was as follows:
count the child nodes
generate a unique random number for each node between 0 and the number of child nodes.
Assign that number to each node (for-each loop) as a variable called sort order or similar.
Sort the nodes based on that number.
Is there anything horribly wrong with trying to do it this way?
To put it in context - this is for a client site where they have a list of sponsors. The sponsors need to display in a random order each time the page is loaded, so that nobody can get accused of favoritism. It sounded fairly trivial but I've hit a huge snag on the RNG stuff.
Thanks very much for any help.
Here is some code I use in one of my XSLT files to rotate random testimonials for a client using a Javascript function. Maybe this will help.
http://our.umbraco.org/wiki/reference/xslt/snippets/getting-a-series-of-unique-random-numbers
This should point you in the right direction.
Looks like the code formatter messed it up a bit, sorry.
Also, you will need to count your nodes and assign that number to the maxItems variable. You should be able to use the count() method for this.
Thanks guys, I've tried the code but am getting this error - should there be any other line for the rdm namespace or similar?: System.Xml.Xsl.XslLoadException: Prefix 'rdm' is not defined.
Out of interest - is the umbraco.library.getrandom() function still valid? I found one site that said it wasn't usable in xslt, but I couldn't get it to work or be recognized at all.
I can give you tomorrow a working example (I have access to it tomorrow).
Thomas
Sorry about that... please add the following at the top:
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:rdm="urn:schemas-microsoft-com:xslt"
Thanks! That got rid of my error, but as with the first example I tried, my random numbers are all displaying as zero. Something does not seem to be working correctly when I call any kind of script from within the xslt. This should be possible, right? I'm wondering if it's a server config issue.
If I change the random number code to simply return 1, or perform the random calculation +1, it returns 1 so it seems to be getting into that function ok just not processing the javascript.
Any ideas?
I reset the permissions and that seemed to do the trick. The page loads nice and fast and hasn't crashed the worker process. Thanks a ton for that code snippet, I've got my count() stuff in place and it seems to be working perfectly.
You bet, glad I could help!
As promised here my approach I am using to display images on a frontpage in a random order. I took the code from some posts in the old and new forum, but I don#t know the original locations anymore.
This example is working with the media nodes, but can also be used on content nodes.
hth, Thomas
Thomas, you just saved me hours of work thanks!
L
is working on a reply...