While it certainly lists random nodes it lists anything from no nodes at all to five per page refresh. What I want is to constantly list random five nodes.
Here what i am doing is that first i take all nodes of hotel type and then select one random node id of hotel amongst them and then i loop through all the nodes and select the one which is marked for random.
I am sure there are probably many other ways of achieving this but here is the solution I used recently. I've tried to strip out everything from my solution that was not relevant so hopefully it still works, if not should at least point you in the right direction.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
]>
<xsl:stylesheet
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:math="urn:schemas-hizi-nl:math"
xmlns:pronotion="http://www.prolificnotion.co.uk"
xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:autofolders.library="urn:autofolders.library" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch"
exclude-result-prefixes="msxml Exslt.ExsltMath umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets autofolders.library PS.XSLTsearch pronotion math">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
<xsl:param name="currentPage"/>
<!-- Input the documenttype you want here -->
<xsl:variable name="documentTypeAlias" select="SPECIFY YOUR DOCTYPE"/>
<xsl:param name="numberToDisplay" select="/macro/numberToDisplay" />
<xsl:variable name="nodes" select="SELECT YOUR NODES" />
<msxsl:script language="c#" implements-prefix="pronotion">
<msxsl:assembly href="../bin/umbraco.dll"/>
<![CDATA[
/// <summary>
/// Gets a random integer that falls between the specified limits
/// </summary>
/// <param name="lowerLimit">An integer that defines the lower-boundary of the range</param>
/// <param name="upperLimit">An integer that defines the upper-boundary of the range</param>
/// <returns>A random integer within the specified range</returns>
public static int GetRandom(int lowerLimit,int upperLimit) {
Random r = umbraco.library.GetRandom();
int returnedNumber = 0;
lock (r)
{
returnedNumber = r.Next(lowerLimit, upperLimit);
}
return returnedNumber;
}
]]>
</msxsl:script>
<xsl:template match="/">
<xsl:apply-templates select="$nodes">
<xsl:sort select="pronotion:GetRandom(0,count($nodes))" order="ascending" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="YOUR DOCTYPE ALIAS">
<xsl:if test="position() <= $numberToDisplay">
<li>
<a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="nodeName"/></a>
</li>
</xsl:if>
</xsl:template>
<xsl:template match="*[umbracoNaviHide = 1]" />
</xsl:stylesheet>
Thanks for your solution! I've actually tried that C# GetRandom method before and it works just aswell as my javascript hack ;-) Thanks for your help and time! Greatly appreciated.
It looks like the cleanest code to generate random numbers in XSLT.. also just got to know from your code we can use Javascript functions like that in XSLT :)
List five random nodes
Hi all,
I'm trying to list five random nodes from a collection of nodes in XSLT. I've searched the forums thin and came up with this solution:
<xsl:for-each select="$currentPage/ancestor-or-self::*//DocType">
<xsl:sort select="floor(Exslt.ExsltMath:random() * count($currentPage/ancestor-or-self::*//DocType) + 1)" />
<xsl:if test="position() <= 6">
// ....
While it certainly lists random nodes it lists anything from no nodes at all to five per page refresh. What I want is to constantly list random five nodes.
Does anyone know how to do this? :-)
Thanks in advance!
/ Bo
Anyone? :)
Hi Bo,
you could try following.
Here what i am doing is that first i take all nodes of hotel type and then select one random node id of hotel amongst them and then i loop through all the nodes and select the one which is marked for random.
Let me know if this helps.
Regards,
Jigar ([email protected])
Hi Jigar,
Sorry for the *very* delayed response - I honestly forgot about this post.#embarassed
While it certainly lists a random node, I would like it to list at least 5 random nodes :-/ simply can't figure it out...
Got it solved with some help from javascript ;-)
This did the trick:
I am sure there are probably many other ways of achieving this but here is the solution I used recently. I've tried to strip out everything from my solution that was not relevant so hopefully it still works, if not should at least point you in the right direction.
Got it solved with some help from javascript ;-)
This did the trick:
Hi Simon,
Thanks for your solution! I've actually tried that C# GetRandom method before and it works just aswell as my javascript hack ;-) Thanks for your help and time! Greatly appreciated.
For the benefit of others can you show the full solution including the Random() method?
Oh, sorry - didn't see I missed out the js method. Here's the full XSLT file:
Thanks again :-)
But Do goes that function if browser block javascript?
The script is processed before the results are sent to the browser so the answer is yes as far as I am aware.
Hi Bo Mortensen.. Thanks for the code sample..
It looks like the cleanest code to generate random numbers in XSLT.. also just got to know from your code we can use Javascript functions like that in XSLT :)
is working on a reply...