Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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?
I have some random code done here:
<?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: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
ahh, btw. to stop after 3 or 4 nodes just use the position:
<xsl:if test="position() < 4" >...
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>
<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 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>
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
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
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?
I have some random code done here:
hth, Thomas
ahh, btw. to stop after 3 or 4 nodes just use the position:
You could also modify the following code to achieve what you are after...
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
is working on a reply...