Copied to clipboard

Flag this post as spam?

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


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Mar 02, 2011 @ 16:29
    Bo Damgaard Mortensen
    0

    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() &lt;= 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

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Mar 04, 2011 @ 14:03
    Bo Damgaard Mortensen
    0

    Anyone? :)

  • jigar 170 posts 233 karma points
    Mar 04, 2011 @ 15:07
    jigar
    0

    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])

     

     <xsl:variable name="HotelRoot" select="umbraco.library:GetXmlAll()//node[@nodeTypeAlias ='Hotel'] />

    <xsl:variable name="HotelNodeSet" select="$HotelRoot" />

    <xsl:variable name="numNodes" select="count($HotelNodeSet)" />
    <xsl:variable name="RandomID" select="floor(Exslt.ExsltMath:random() * $numNodes) + 1"/>
    <xsl:for-each select="$HotelNodeSet[position() = $RandomID]">

    <div class="header-arrow-home">
    <a href="{umbraco.library:NiceUrl(@id)}">FEATURED Hotel</a>
    </div>
    <div id="future-content">
    <h3>
    <xsl:value-of select="data [@alias = 'Hotel_Name']"/>

    </h3>
    <p class="larg14">
    <xsl:value-of select="substring(umbraco.library:StripHtml(data [@alias = 'Hotel_Short_Details']),1,400)" />....
    </p>

    <a href="{umbraco.library:NiceUrl(@id)}" class="more-button">&nbsp;</a>
    </div>
  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Mar 18, 2011 @ 13:09
    Bo Damgaard Mortensen
    0

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

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Mar 18, 2011 @ 13:27
    Bo Damgaard Mortensen
    0

    Got it solved with some help from javascript ;-)

    This did the trick:

      <xsl:variable name="allSpots" select="umbraco.library:GetXmlAll()//PartnerSpot" />
      <xsl:variable name="spotNodeSet" select="$allSpots" />

      <xsl:for-each select="$spotNodeSet">
        <xsl:sort select="rdm:Random(4)" order="descending" />
         <xsl:if test="position() &lt;= 4">
          <p><xsl:value-of select="current()/@nodeName" /></p>
        </xsl:if>
      </xsl:for-each>

     

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Mar 18, 2011 @ 13:28
    Simon Dingley
    0

    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 "&#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: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() &lt;= $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>
  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Mar 18, 2011 @ 13:31
    Bo Damgaard Mortensen
    0

    Got it solved with some help from javascript ;-)

    This did the trick:

      <xsl:variable name="allSpots" select="umbraco.library:GetXmlAll()//PartnerSpot" />
      <xsl:variable name="spotNodeSet" select="$allSpots" />

      <xsl:for-each select="$spotNodeSet">
        <xsl:sort select="rdm:Random(4)" order="descending" />
         <xsl:if test="position() &lt;= 4">
          <p><xsl:value-of select="current()/@nodeName" /></p>
        </xsl:if>
      </xsl:for-each>

     

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Mar 18, 2011 @ 13:33
    Bo Damgaard Mortensen
    0

    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.

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Mar 18, 2011 @ 13:33
    Simon Dingley
    0

    For the benefit of others can you show the full solution including the Random() method?

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Mar 18, 2011 @ 13:40
    Bo Damgaard Mortensen
    2

    Oh, sorry - didn't see I missed out the js method. Here's the full XSLT file:

    <?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:rdm="http://www.umbraco.org/"
      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:PS.XSLTsearch="urn:PS.XSLTsearch"
      exclude-result-prefixes="rdm
    msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes
    Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings
    Exslt.ExsltSets PS.XSLTsearch"
    >


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

    <xsl:param name="currentPage"/>
    <msxml:script language="JavaScript" implements-prefix="rdm">
        function Random(r) { return Math.ceil(Math.random()*r); }
    </msxml:script>
    <xsl:template match="/">

    <!-- start writing XSLT -->   
      <xsl:variable name="allSpots" select="umbraco.library:GetXmlAll()//PartnerSpot" />
      <xsl:variable name="spotNodeSet" select="$allSpots" />

      <xsl:for-each select="$spotNodeSet">
        <xsl:sort select="rdm:Random(4)" order="descending" />
         <xsl:if test="position() &lt;= 4">
          <p><xsl:value-of select="current()/@nodeName" /></p>
        </xsl:if>
      </xsl:for-each
    </xsl:template>

    </xsl:stylesheet>

    Thanks again :-)

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Mar 24, 2011 @ 15:04
    Biagio Paruolo
    0

    But Do goes that function if browser block javascript?

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Mar 24, 2011 @ 15:10
    Simon Dingley
    0

    The script is processed before the results are sent to the browser so the answer is yes as far as I am aware.

  • Ansar 181 posts 291 karma points
    Jul 03, 2011 @ 14:32
    Ansar
    0

    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 :)

Please Sign in or register to post replies

Write your reply to:

Draft