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
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 " "> ]><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.
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 :)
Thank you, I try but sometimes it still display nothing...
Ok, I read about javascript math.random an it's based on the computer clock. So if a refresh to fast it gives nothing.
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
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 " "> ]>
<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.
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 :)
Thank you, I try but sometimes it still display nothing...
Ok, I read about javascript math.random an it's based on the computer clock. So if a refresh to fast it gives nothing.
is working on a reply...