Copied to clipboard

Flag this post as spam?

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


  • Dave 16 posts 36 karma points
    Jul 16, 2010 @ 21:10
    Dave
    0

    Trouble with XSLT / parameters

    I'm attempting to set up some pages that need a random image displayed.  I've copied the code in this post exactly as it appears in the last code posted.

    I need to do exactly the same thing, but I can't seem to get this to work in my environment. The issue is that I'm not sure how to interpret the XSL, so I don't know how the variable "mediaFolderID" is getting populated in the pages.

    Is that value coming from a property named "randomImage" of the page template, and can it be a folder so it selects one of the children?

    Sorry for the newb question, just getting started here.. :)

    Here's what I have in my template: <umbraco:Macro Alias="RandomBgImg" randomImage="1216" runat="server"></umbraco:Macro>

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jul 16, 2010 @ 21:29
    Matt Brailsford
    0

    Hey Dave,

    Yes, based on the last code snippet for you referenced post, you would need a property on your document type with an alias of randomImage of data type media picker. By the looks of the code, it is expecting you to choose a folder.

    In terms of your macro code in your template, you shouldn't need the randomImage attribute as this is retreived from the document type.

    Matt

  • Hendrik Jan 71 posts 137 karma points
    Jul 16, 2010 @ 21:40
    Hendrik Jan
    0

    Are you using Umbraco 4.5?, the snippet on the other page isnt gonna work since the xml schema has changed.

  • Dave 16 posts 36 karma points
    Jul 16, 2010 @ 22:02
    Dave
    0

    Yes, I am using 4.5.  Is there any examples or other posts that I should refer to?

  • Hendrik Jan 71 posts 137 karma points
    Jul 16, 2010 @ 22:12
    Hendrik Jan
    0

    I don't know. I can help you with an exemple tho.

    This generates a random background from an media folder. Based on the Folder Node ID
    *for this script to work the images must be upload as images, not as a file.

     

     

    <xsl:variable name="folderNodeID" select="1072"/>
    <xsl:variable name="randomImageNumber" select="round( Exslt.ExsltMath:random() * (count(umbraco.library:GetMedia($folderNodeID, 1)/Folder/Image) -2 ) + 1 )"/>
    <div id="yourID" style="background-image:url({umbraco.library:GetMedia($folderNodeID, 1)/Folder/Image[$randomImageNumber]/umbracoFile});"></div>
      

     

  • Dave 16 posts 36 karma points
    Jul 16, 2010 @ 23:03
    Dave
    0

    Niels,

    Thanks for the reply,

    The solution posted works except that I only have three images to randomly select. The randomization returns no image fairly often, so it's not quite reliable.

    Any thoughts?

  • Hendrik Jan 71 posts 137 karma points
    Jul 16, 2010 @ 23:45
    Hendrik Jan
    0

    Hey dave,

    i have failed :P (i'm still a noob at xslt). I have changed the second line and works correct now.

    here is the new code:

    <xsl:variable name="folderNodeID" select="1072"/>
    <xsl:variable name="randomImageNumber" select="ceiling( Exslt.ExsltMath:random() * (count(umbraco.library:GetMedia($folderNodeID, 1)/Folder/Image) ) )"/>
    <div id="yourID" style="background-image:url({umbraco.library:GetMedia($folderNodeID, 1)/Folder/Image[$randomImageNumber]/umbracoFile});"><div>
      

     

     

    Hope this works for you

  • Dave 16 posts 36 karma points
    Nov 06, 2010 @ 14:32
    Dave
    0

    Bump

    Since upgrading the site to 4.5.2, the randomization isn't working correctly anymore, there frequency of no image coming back is now a significant problem:

    Here is the current XSLT:

    <?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.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
      exclude-result-prefixes="msxml
    umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes
    Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings
    Exslt.ExsltSets "
    >

      <xsl:output method="xml" omit-xml-declaration="yes"/>
      <xsl:param name="currentPage"/>
      <xsl:variable name="folderNodeID" select="/macro/randomImage"/>
      <xsl:variable name="debug" select="0"/>

      <xsl:template match="/">

        <xsl:if test="$debug=1">
          <xsl:comment>
            folderNodeID <xsl:value-of select="$folderNodeID"/>        
          </xsl:comment>
        </xsl:if>


        <xsl:choose>
          <xsl:when test="$folderNodeID &gt; 0">
            <xsl:variable name="randomImageNumber" select="ceiling( Exslt.ExsltMath:random() * (count(umbraco.library:GetMedia($folderNodeID, 1)/Folder/Image) ) )"/>
            <xsl:choose>
              <xsl:when test="$randomImageNumber &gt; 0">
                <img src="{umbraco.library:GetMedia($folderNodeID, 1)/Folder/Image[$randomImageNumber]/umbracoFile}" />  
              </xsl:when>
              <xsl:otherwise>
                <xsl:comment>folderNodeId lt= 0</xsl:comment>
                <xsl:call-template name="drawFromDefault" />
              </xsl:otherwise>
            </xsl:choose>

          </xsl:when>
          <xsl:otherwise>
            <xsl:comment>
              Folder Id was not provided.
            </xsl:comment>
            <xsl:call-template name="drawFromDefault" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

      <xsl:template name="drawFromDefault">
        <xsl:variable name="folderNodeID" select="1216"/>
        <xsl:if test="$debug=1">
          <xsl:comment>
            using default folderNodeID <xsl:value-of select="$folderNodeID"/>        
          </xsl:comment>
        </xsl:if>
        <xsl:variable name="randomImageNumber" select="ceiling( Exslt.ExsltMath:random() * (count(umbraco.library:GetMedia($folderNodeID, 1)/Folder/Image) ) )"/>
        <xsl:if test="$randomImageNumber &gt; 0">
          <img src="{umbraco.library:GetMedia($folderNodeID, 1)/Folder/Image[$randomImageNumber]/umbracoFile}" />
        </xsl:if>
      </xsl:template>
    </xsl:stylesheet>

    Can anyone see what the issue is here?

     

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies