Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • aaronb 97 posts 116 karma points
    Aug 31, 2009 @ 20:00
    aaronb
    0

    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.

  • Ricky Beard 61 posts 116 karma points
    Aug 31, 2009 @ 20:12
    Ricky Beard
    0

    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.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <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:umbraco.library="urn:umbraco.library"
        exclude-result-prefixes="msxml msxsl umbraco.library rdm">
        <xsl:output method="xml" omit-xml-declaration="yes" />




    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage" />
    <xsl:variable name="maxItems" select="number(2)" />
    <msxsl:script language="JavaScript" implements-prefix="rdm">
        function Random(r) { return Math.ceil(Math.random()*r); }
    </msxsl:script>
    <xsl:template match="/">

    <xsl:for-each select="$currentPage/ancestor-or-self::root//node [@nodeTypeAlias='Testimonial']">
        <xsl:sort select="rdm:Random($maxItems)" order="descending" />
        <xsl:if test="position() &lt;= $maxItems">





        Place your sponsor display code here...
    </xsl:if>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>
  • Chris Koiak 700 posts 2626 karma points
    Aug 31, 2009 @ 20:13
  • Ricky Beard 61 posts 116 karma points
    Aug 31, 2009 @ 20:16
    Ricky Beard
    0

    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.

  • aaronb 97 posts 116 karma points
    Aug 31, 2009 @ 21:44
    aaronb
    0

    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.

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Aug 31, 2009 @ 22:27
    Thomas Höhler
    0

    I can give you tomorrow a working example (I have access to it tomorrow).

    Thomas

  • Ricky Beard 61 posts 116 karma points
    Aug 31, 2009 @ 22:59
    Ricky Beard
    0

    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"

  • aaronb 97 posts 116 karma points
    Aug 31, 2009 @ 23:28
    aaronb
    0

    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?

     

     

     

  • aaronb 97 posts 116 karma points
    Aug 31, 2009 @ 23:51
    aaronb
    0

    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.

  • Ricky Beard 61 posts 116 karma points
    Sep 01, 2009 @ 01:39
    Ricky Beard
    0

    You bet, glad I could help!

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Sep 01, 2009 @ 14:30
    Thomas Höhler
    2

    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.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [
    <!ENTITY nbsp "&#x00A0;">
    ]>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    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:ferinet="urn:ferinet"
    xmlns:random="urn:random"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ferinet">

    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <msxml:script implements-prefix="random" language="C#">
    <msxml:assembly name="umbraco"/>
    <msxml:using namespace="umbraco"/>
    <![CDATA[
    public int GetRandom(int minValue, int maxValue)
    {
    return umbraco.library.GetRandom().Next(minValue, maxValue);
    }
    ]]>
    </msxml:script>

    <xsl:template match="/">

    <xsl:variable name="mediaNode" select="umbraco.library:GetMedia('1850', 'true')"/>

    <xsl:for-each select="$mediaNode/node">
    <xsl:sort select="random:GetRandom(1, count($mediaNode/node))" order="ascending" />
    <a href="{umbraco.library:NiceUrl(data[@alias = 'Link'])}">
    <img src="{data [@alias='Crops']//crop/@url}" alt="pic" width="265" height="173"/>
    </a>
    </xsl:for-each>

    </xsl:template>

    </xsl:stylesheet>

    This example is working with the media nodes, but can also be used on content nodes.

    hth, Thomas

  • Lachlann 344 posts 626 karma points
    Dec 11, 2010 @ 16:40
    Lachlann
    0

    Thomas, you just saved me hours of work thanks!

     

    L

Please Sign in or register to post replies

Write your reply to:

Draft