Copied to clipboard

Flag this post as spam?

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


  • Thomas Sørensen 5 posts 25 karma points
    Oct 27, 2010 @ 20:21
    Thomas Sørensen
    0

    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 "&#x00A0;"> ]>
    <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>

  • Paul Blair 466 posts 731 karma points
    Oct 27, 2010 @ 22:06
    Paul Blair
    0

    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]"/>

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Oct 27, 2010 @ 22:38
    Jan Skovgaard
    0

    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

  • Thomas Sørensen 5 posts 25 karma points
    Oct 31, 2010 @ 16:53
    Thomas Sørensen
    0

    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 "&#x00A0;"> ]>
    <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>

  • Malte Baden Hansen 61 posts 158 karma points
    Oct 31, 2010 @ 17:15
    Malte Baden Hansen
    0

    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 =)

Please Sign in or register to post replies

Write your reply to:

Draft