Copied to clipboard

Flag this post as spam?

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


  • Jeffrey Schoemaker 408 posts 2138 karma points MVP 8x c-trib
    Mar 19, 2010 @ 09:09
    Jeffrey Schoemaker
    0

    Select a random member in XSLT

    I want to display a random member on my homepage. The only way I can find to select a member is by the function umbraco.library:GetMember('1060'), but I don't want to always show member 1060

    Is there a way to select a random member in XSLT?

     

  • Dan 1288 posts 3921 karma points c-trib
    Mar 19, 2010 @ 10:07
    Dan
    1

    Hi Jeffrey,

    I can't solve your exact problem right now, but here's some code I use to select a random node of site content on a page - you can probably modify it quite easily to pick members rather than content nodes:

    <?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"/>
    <xsl:variable name="source" select="/macro/source"/>

    <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="umbraco.library:GetXmlNodeById($source)/node [string(data [@alias='umbracoNaviHide']) != '1']">
    <xsl:sort select="random:GetRandom(1, count($currentPage/node))" order="ascending" />
    <xsl:if test="position() &lt; 2">
    <p>...Output content...</p>
    </xsl:if>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

    Hope it helps...

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 19, 2010 @ 10:11
    Jan Skovgaard
    0

    Maybe this Wiki-Entry http://our.umbraco.org/wiki/reference/code-snippets/getmembersbygroupname can be usefull in combination with the random code Dan provided?

    /Jan

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Mar 19, 2010 @ 10:39
    Lee Kelleher
    1

    Hi Jeffrey,

    Here's an alternative suggestion that makes use of the SQL for XSLT package.  Install the package, follow the instructions, then try the following SQL query:

    SELECT
        TOP 1
        CAST(xml AS XML)
    FROM
        cmsContentXml AS x
        INNER JOIN cmsMember AS m ON x.nodeID = m.nodeID
    ORDER BY
        NEWID()
    FOR XML
        PATH('')
    ;

    This will return a single XML node of the member data - selected randomly, (of course).

    An example XSLT would be:

    <xsl:template match="/">
    
        <xsl:variable name="sql">
            <xsl:text disable-output-escaping="yes"><![CDATA[
    SELECT
        TOP 1
        CAST(xml AS XML)
    FROM
        cmsContentXml AS x
        INNER JOIN cmsMember AS m ON x.nodeID = m.nodeID
    ORDER BY
        NEWID()
    FOR XML
        PATH('')
    ;
    ]]></xsl:text>
        </xsl:variable>
    
        <xsl:variable name="randomMember" select="jesper.sql:SQLXml($sql)"/>
    
        <xsl:if test="count($randomMember/node) &gt; 0">
            <xsl:value-of select="$randomMember/node/@nodeName" />
        </xsl:if>
    
    </xsl:template>

    I haven't tested this myself, but it should work... might need some playing around with it!

    Good luck, Lee.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Mar 19, 2010 @ 10:42
    Lee Kelleher
    0

    Quick update, (because we can't edit posts).

    Get rid of the "FOR XML PATH('')" bit ... the SQL for XSLT extension does that automatically for you.

    It will return the XML in a root element called <x> ... so change the XSLT example to use "$randomMember/x/node".

    Cheers, Lee.

  • Jeffrey Schoemaker 408 posts 2138 karma points MVP 8x c-trib
    Mar 22, 2010 @ 10:34
    Jeffrey Schoemaker
    0

    Hi Lee,

    it works great!Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft