Copied to clipboard

Flag this post as spam?

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


  • Simon Justesen 436 posts 203 karma points
    Mar 17, 2010 @ 10:26
    Simon Justesen
    0

    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 "&#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="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']}&amp;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

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Mar 17, 2010 @ 10:37
    Dirk De Grave
    0

    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

  • Simon Justesen 436 posts 203 karma points
    Apr 10, 2010 @ 11:57
    Simon Justesen
    0

    Thanks Dirk, I'll have a look at it, and yes I'll need to do some optimisations :)

Please Sign in or register to post replies

Write your reply to:

Draft