Copied to clipboard

Flag this post as spam?

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


  • Lee 1130 posts 3088 karma points
    Jan 21, 2010 @ 13:04
    Lee
    0

    Best Way To Get Random Nodes From Specific Node?

    I have searched on this and can't find anything, I have about 50 child nodes under a specific parent - What would be the best/fastest way to return a random 3 or 4 nodes from the list of child nodes only?

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Jan 21, 2010 @ 13:18
    Thomas Höhler
    4

    I have some random code done here:

    <?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:random="urn:random"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets">

    <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:for-each select="$currentPage/node">
    <xsl:sort select="random:GetRandom(1, count($currentPage/node))" order="ascending" />
    ...
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

    hth, Thomas

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Jan 21, 2010 @ 13:19
    Thomas Höhler
    0

    ahh, btw. to stop after 3 or 4 nodes just use the position:

    <xsl:if test="position() &lt; 4" >...
  • Laurence Gillian 600 posts 1219 karma points
    Jan 21, 2010 @ 13:37
    Laurence Gillian
    2

    You could also modify the following code to achieve what you are after...

    <xsl:variable name="siteRoot">

        <xsl:value-of select="$currentPage/ancestor::root/node/@id"/>

      </xsl:variable>

      

      <xsl:variable name="preNodes">

        <xsl:variable name="selectCaseStudies" select="umbraco.library:GetXmlNodeById($siteRoot)/data[@alias='layoutGlobalCaseStudyPicker']" />

        <xsl:variable name="nodeIds" select="umbraco.library:Split($selectCaseStudies, ',')" />

        <xsl:for-each select="$nodeIds/value">

          <xsl:copy-of select="umbraco.library:GetXmlNodeById(.)"/>

        </xsl:for-each>

      </xsl:variable>

     

      <xsl:variable name="nodes" select="msxml:node-set($preNodes)/node" />

      <xsl:variable name="numNodes" select="count($nodes)"/>

      <xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $numNodes) + 1"/>

      <xsl:if test="count($nodes) > 0">

        <xsl:for-each select="$nodes [position() = $random]">

          Test

    <xsl:for-each>

     

    </xsl:if>

  • dandrayne 1138 posts 2262 karma points
    Jan 21, 2010 @ 15:38
    dandrayne
    0

    There's a good page on the wiki about this, but it's essentially the same solution as thomas as far as i can tell

    http://our.umbraco.org/wiki/reference/xslt/snippets/getting-a-series-of-unique-random-numbers

    Dan

Please Sign in or register to post replies

Write your reply to:

Draft