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
Hello
I am trying to create a xslt file that showes a random image from a folder.
I have found many suggestion on the internet, but none that works in Umbraco 4.5.
Here are what i have tried so fare.
No matter what i try, it goes wrong.. Anyone have a hint / example ?
<?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="pictureFolder" select="$currentPage/ancestor-or-self::*[@level = '1']/topPictures" /><!-- ============================================================= --> <xsl:template match="/"> <xsl:variable name="numNodes" select="count(umbraco.library:GetMedia($pictureFolder, 1)/node)"/> <xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $numNodes) + 1"/> <xsl:value-of select="umbraco.library:GetMedia($pictureFolder, 1)/node[position() = 0]/data"/> -- <xsl:value-of select="umbraco.library:GetMedia('1071', 1)"/> <img alt="Our Banner Image"> <xsl:attribute name="src"> <xsl:value-of select="umbraco.library:GetMedia($pictureFolder, 1)/node [position() = $random]/data"/> </xsl:attribute> </img> </xsl:template> <!-- ============================================================= <xsl:template name="GetRandomNodeID"> <xsl:variable name="numNodes" select="count(umbraco.library:GetXmlNodeById($pictureFolder)/node)"/> <xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $numNodes) + 1"/> <xsl:when test="umbraco.library:GetXmlNodeById($pictureFolder)/node [position() = $random] [string(data [@alias='umbracoNaviHide']) != '1'] "> <xsl:value-of select="$currentPage/ancestor-or-self::node[@level = 1]//node[@nodeTypeAlias = $documentTypeAlias] [position() = $random]/@id"/> </xsl:if> </xsl:template> --> </xsl:stylesheet>
first of all I would try outputing the $random variable to make sure that it is calculating correctly.
then (if you are using 4.5.2) i think the assignment to src could be something like:
<xsl:value-of select="umbraco.library:GetMedia($pictureFolder, 1)/* /umbracoFile [$random]"/>
Hi Thomas
Have a look at this wiki entry as this is probably what you are after. It's easy and fast to implement.
http://our.umbraco.org/wiki/reference/xslt/snippets/getting-a-series-of-unique-random-numbers
/Jan
Thanks for te replys, If anyone is interested, here are a solution that works.
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp " "> ]><xsl:stylesheet version="1.0"xmlns:msxsl="urn:schemas-microsoft-com:xslt" 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"xmlns:myFuncs="urn:my-scripts" exclude-result-prefixes="msxml myFuncs umbraco.library">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="currentPage"/>
<xsl:variable name="imageRoot" select="$currentPage/ancestor-or-self:: * [@isDoc][@level = 1]/topPictures"/>
<xsl:template match="/"> <xsl:variable name="numNodes" select="count(umbraco.library:GetMedia($imageRoot, 1)/*)-1"/> <xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $numNodes)+1"/> <xsl:value-of select="umbraco.library:GetMedia($imageRoot, 1)/* [$random+1]/umbracoFile"/>
</xsl:template>
</xsl:stylesheet>
I don't think you should be adding to the random variable more than once, i could be terrible wrong though.Is it working with only one image in the folder? if yes then i was wrong =)
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Random image ??
Hello
I am trying to create a xslt file that showes a random image from a folder.
I have found many suggestion on the internet, but none that works in Umbraco 4.5.
Here are what i have tried so fare.
No matter what i try, it goes wrong.. Anyone have a hint / example ?
<?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="pictureFolder" select="$currentPage/ancestor-or-self::*[@level = '1']/topPictures" />
<!-- ============================================================= -->
<xsl:template match="/">
<xsl:variable name="numNodes" select="count(umbraco.library:GetMedia($pictureFolder, 1)/node)"/>
<xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $numNodes) + 1"/>
<xsl:value-of select="umbraco.library:GetMedia($pictureFolder, 1)/node[position() = 0]/data"/> --
<xsl:value-of select="umbraco.library:GetMedia('1071', 1)"/>
<img alt="Our Banner Image">
<xsl:attribute name="src">
<xsl:value-of select="umbraco.library:GetMedia($pictureFolder, 1)/node [position() = $random]/data"/>
</xsl:attribute>
</img>
</xsl:template>
<!-- =============================================================
<xsl:template name="GetRandomNodeID">
<xsl:variable name="numNodes" select="count(umbraco.library:GetXmlNodeById($pictureFolder)/node)"/>
<xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $numNodes) + 1"/>
<xsl:when test="umbraco.library:GetXmlNodeById($pictureFolder)/node [position() = $random] [string(data [@alias='umbracoNaviHide']) != '1'] ">
<xsl:value-of select="$currentPage/ancestor-or-self::node[@level = 1]//node[@nodeTypeAlias = $documentTypeAlias] [position() = $random]/@id"/>
</xsl:if>
</xsl:template>
-->
</xsl:stylesheet>
first of all I would try outputing the $random variable to make sure that it is calculating correctly.
then (if you are using 4.5.2) i think the assignment to src could be something like:
<xsl:value-of select="umbraco.library:GetMedia($pictureFolder, 1)/* /umbracoFile [$random]"/>
Hi Thomas
Have a look at this wiki entry as this is probably what you are after. It's easy and fast to implement.
http://our.umbraco.org/wiki/reference/xslt/snippets/getting-a-series-of-unique-random-numbers
/Jan
Thanks for te replys, If anyone is interested, here are a solution that works.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet
version="1.0"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
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"
xmlns:myFuncs="urn:my-scripts"
exclude-result-prefixes="msxml myFuncs umbraco.library">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="currentPage"/>
<xsl:variable name="imageRoot"
select="$currentPage/ancestor-or-self:: * [@isDoc][@level = 1]/topPictures"/>
<xsl:template match="/">
<xsl:variable name="numNodes" select="count(umbraco.library:GetMedia($imageRoot, 1)/*)-1"/>
<xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $numNodes)+1"/>
<xsl:value-of select="umbraco.library:GetMedia($imageRoot, 1)/* [$random+1]/umbracoFile"/>
</xsl:template>
</xsl:stylesheet>
I don't think you should be adding to the random variable more than once, i could be terrible wrong though.
Is it working with only one image in the folder? if yes then i was wrong =)
is working on a reply...