|
|
 Rank: Fanatic
Joined: 11/24/2006 Posts: 299 Location: Stockholm, Sweden
|
I'm looking for idéas on how to create a macro that picks a random quote every time the user reloads the page. Judging from previous posts here on the forum I understand that randomizing does not exist in XSLT. Therefore I guess I'd have to create some sort of hybrid between XSLT and C#? Does anyone have a working (3.0) macro that does randomizing that I could look at? If not, do you have any general pointers how I should solve this problem?
Web Developer at Kärnhuset - http://www.karnhuset.net - Stockholm, Sweden
|
|
 Rank: Fanatic
Joined: 7/25/2006 Posts: 375 Location: Silkeborg, Denmark
|
|
|
 Rank: Fanatic
Joined: 7/20/2006 Posts: 420 Location: NL
|
I do have a site running which shows a random picture/flash-file on each reqeusted page. All done in a macro, site is running v3.02 Code: <?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:msxsl="urn:schemas-microsoft-com:xslt" xmlns:math="urn:schemas-hizi-nl:math" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" exclude-result-prefixes="msxml Exslt.ExsltMath Exslt.ExsltStrings math umbraco.library">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<msxml:script language="JavaScript" implements-prefix="math"> function random(numDie,numMax,numMin){ if (numMin==null){numMin=1;} var sum=0; for (var index=0;index<numDie;index++){ sum+=Math.floor(Math.random()*(numMax-numMin) + numMin); } return "" + sum; } function floorme(numFloor){ return "" + Math.floor(numFloor); } </msxml:script>
<xsl:variable name="item" select="/macro/linksource"/> <xsl:variable name="parent" select="umbraco.library:GetMedia(1063, true())" /> <xsl:variable name="random" select="math:random(1,count($parent/node)+1,1)"/> <xsl:template match="/">
<xsl:for-each select="$parent/node"> <xsl:if test="position()=$random"> <xsl:if test="./data [@alias = 'umbracoExtension'] = 'swf'"> <script type="text/javascript"> var FO = { movie:"<xsl:value-of select="./data [@alias = 'umbracoFile']"/>", width:"160", height:"230", majorversion:"7", build:"0", menu:"false", quality:"best", wmode:"transparent", bgcolor:"#FFFFFF", allowscriptaccess:"samedomain" }; UFO.create(FO, "righttop"); </script> <div id="righttop"> <p>Hier staat reclame</p> </div> </xsl:if> <xsl:if test="./data [@alias = 'umbracoExtension'] = 'gif'"> <img alt=""> <xsl:attribute name="src"><xsl:value-of select="./data [@alias = 'umbracoFile']"/></xsl:attribute> </img> </xsl:if> </xsl:if> </xsl:for-each> </xsl:template>
</xsl:stylesheet>
Hope this is of some help PeterD Working on an events-calendar with recursion. Post requests on my blog!
|
|
 Rank: Aficionado
Joined: 10/31/2007 Posts: 109 Location: Birmingham (UK)
|
Hi Peter, I'm trying to do something similar by selecting a random header image from a media folder however whenever I try this code I get "System.Xml.XmlException: The ';' character, hexadecimal value 0x3B, cannot be included in a name. Line 22, position 30." The offending line appears to be: for (var index=0;index<numDie;index++){ "numDie;" to be specific. Any ideas why this might be? Tim
Managing Director at The Site Doctor Ltd - My personal blog is here - Umbraco Newbie ;)
|
|
 Rank: Fanatic
Joined: 7/20/2006 Posts: 420 Location: NL
|
Yes, the line should be:
for (var index=0;index<numDie;index++){
Working on an events-calendar with recursion. Post requests on my blog!
|
|
 Rank: Aficionado
Joined: 10/31/2007 Posts: 109 Location: Birmingham (UK)
|
Of course, thanks :) Tim
Managing Director at The Site Doctor Ltd - My personal blog is here - Umbraco Newbie ;)
|
|
|
Guest |