Copied to clipboard

Flag this post as spam?

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


  • Profiterole 232 posts 264 karma points
    Feb 18, 2010 @ 20:22
    Profiterole
    0

    Random news

    Hi, I have 15 news in a nodeType called RunwayNews. I try to display a random one. It partly works, but sometimes it chooses nothing. Like if the random chooses 0 so it displays nothing. What I've done wrong?

     

    <?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:bdk="http://bodenko.com"
            xmlns:umbraco.library="urn:umbraco.library"
            exclude-result-prefixes="msxml msxsl umbraco.library bdk">
            <xsl:output method="xml" omit-xml-declaration="yes" />

            <xsl:param name="currentPage" />

            <msxsl:script language="JavaScript" implements-prefix="bdk">
                    function Random(r) { return Math.ceil(Math.random()*r); }
            </msxsl:script>

            <xsl:template match="/">      
                    <xsl:variable name="newsNodes" select="$currentPage/ancestor-or-self::node[@level=1]/descendant-or-self::node[@nodeTypeAlias='RunwayNews']" />        
            <xsl:value-of select="$newsNodes[bdk:Random(count($newsNodes))]/data[@alias='textIntro']" disable-output-escaping="yes"/>
            </xsl:template>
    </xsl:stylesheet>

     

    Thanks for your help.

     

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 18, 2010 @ 20:25
    Morten Bock
    1

    xpath is 1-indexed, so the first element is at position 1. So there is nothing a position 0. Just add 1 to you random number, and that should do it :)

  • Profiterole 232 posts 264 karma points
    Feb 18, 2010 @ 20:37
    Profiterole
    0

    Thank you, I try but sometimes it still display nothing...

  • Profiterole 232 posts 264 karma points
    Feb 18, 2010 @ 20:40
    Profiterole
    0

    Ok, I read about javascript math.random an it's based on the computer clock. So if a refresh to fast it gives nothing.

Please Sign in or register to post replies

Write your reply to:

Draft