-Output the value of $random variable, it might be a problem with the floor(Exslt.ExsltMath:random() * $numNodes) + 1 statement (might always give the same result)?
- Hope no caching is used?
And while you're at it, make some optimizations:
- Create a new variable to store the random node and avoid multiple GetXmlNodeById() and GetMedia(). Especially the last call is not database cost effective
Display random node
I'm building a "highlighted product" kind of widget using the following XSLT:
<?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.ExsltMath="urn:Exslt.ExsltMath"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltMath">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<xsl:variable name="bannerFolder" select="1060"/>
<!-- ============================================================= -->
<xsl:template match="/">
<xsl:variable name="numNodes" select="count(umbraco.library:GetXmlNodeById($bannerFolder)/node)"/>
<xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $numNodes) + 1"/>
<table>
<tr>
<xsl:if test="umbraco.library:GetXmlNodeById($bannerFolder)/node[position() = $random]/node[position() = $random]/data[@alias='produktbillede'] != ''">
<td style="vertical-align: top;"><a href="{umbraco.library:GetMedia(umbraco.library:GetXmlNodeById($bannerFolder)/node[position() = $random]/node[position() = $random]/data[@alias='produktbillede'],false)/data[@alias='umbracoFile']}"><img src="/umbraco/ImageGen.ashx?image={umbraco.library:GetMedia(umbraco.library:GetXmlNodeById($bannerFolder)/node[position() = $random]/node[position() = $random]/data[@alias='produktbillede'],false)/data[@alias='umbracoFile']}&height=100" /></a></td>
</xsl:if>
<td style="vertical-align: top;"><strong><xsl:value-of select="umbraco.library:GetXmlNodeById($bannerFolder)/node[position() = $random]/node[position() = $random]/data [@alias='produktnavn']"/></strong><br />
<xsl:value-of select="umbraco.library:GetXmlNodeById($bannerFolder)/node[position() = $random]/node[position() = $random]/data [@alias='kortbeskrivelse']"/></td>
</tr>
</table>
</xsl:template>
I'm having some difficulties getting it to work correctly with the following menu structure:
Root
|-- Category A
|---- Item 1
|---- Item 2
|-- Category B
|---- Item 1
|---- Item 2
The script seems to always select the same items in each category
Hi Simon,
First things to check:
-Output the value of $random variable, it might be a problem with the floor(Exslt.ExsltMath:random() * $numNodes) + 1 statement (might always give the same result)?
- Hope no caching is used?
And while you're at it, make some optimizations:
- Create a new variable to store the random node and avoid multiple GetXmlNodeById() and GetMedia(). Especially the last call is not database cost effective
(Not exactly an answer to the question tho...)
Cheers,
/Dirk
Thanks Dirk, I'll have a look at it, and yes I'll need to do some optimisations :)
is working on a reply...