Copied to clipboard

Flag this post as spam?

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


  • Bent Holz 100 posts 273 karma points
    Nov 29, 2011 @ 20:42
    Bent Holz
    0

    Randomize Xslt list

    Hi there...

    Is there a simple way to randomize the list items of a Xslt generated ul?

    I have the following code 

    <ul>

      <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/* [@isDoc and string(umbracoNaviHide) != '1']">

      <xsl:if test="billede != ''">
      <xsl:variable name="myPic" select="umbraco.library:GetMedia(billede,'false')/umbracoFile" />
      
          <li class="s3sliderImage">
          <href="{umbraco.library:NiceUrl(@id)}" ><img src="{concat(substring-before($myPic,'.'),'_FrontSlide.',substring-after($myPic,'.'))}" border="0" alt="{@nodeName}"/></a>
            <span><xsl:value-of select="@nodeName"/><xsl:value-of select="vejlPris"/> kun kr. <font color="#ff0000" size="6" align="right"><xsl:value-of select="pris"/></font></span>
          </li>

      </xsl:if>
      </xsl:for-each>
    <div></div>
    </ul>

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Nov 29, 2011 @ 20:56
    Dirk De Grave
    0

    Could this be something you're after? Requires you to install uComponents package as it will contain the required assembly to run the xslt extension

     

    Cheers,

    /Dirk

  • Bent Holz 100 posts 273 karma points
    Nov 29, 2011 @ 21:19
    Bent Holz
    0

    Hi Dirk

    Thx for your time!...

    Hmmm... well I don't know if it will do the trick... i don't get very much from the link... 

    I have now installed the uComponents package...

    I can see in the link you posted that theres a RandomNode and RandomChildNode. Unfortunately i'm no coder and have no clue on where to begin. a hint on how this uComponents works?... 

    Cheers Crawn

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Nov 29, 2011 @ 21:33
    Dirk De Grave
    1

    Ok, from looking at your code, now not sure if I understood very well... you do want to have a random node choosen amonst the child nodes of the $source node, right? If that's the case, this is what to do;

    Have to make sure the xslt extension is *known* by the umbraco system. Either the uComponents installer has asked you whether and what xslt extensions to install (I even think you can still add it when going to the /installed packages node in the developer section), or you can configure manually as found on the page I've sent the link for.

    Once that's configured, you can use any xslt extension method defined in that assembly, so in your case, you could write:

    <xsl:variable name="randomNode" select="ucomponents.xml:RandomNode(umbraco.library:GetXmlNodeById($source))/*[@isDoc and string(umbracoNaviHide! = '1' and string(billede) != ''])" />

    which will return a single random node that has a property billede which has a value (probably a media picker)

     

    Hope this helps.

    Regards,

    /Dirk

     

  • Bent Holz 100 posts 273 karma points
    Nov 30, 2011 @ 13:35
    Bent Holz
    0

    Hi Dirk

    My code lists subpages from a page..

    <ul>
    <li> - Subpage 1 </li>
    <li> - Subpage 2 </li>
    <li> - Subpage 3 </li>
    </ul> 

    ...and it is that list i wanna randomize 

    <ul>
    <li> - Subpage 2 </li>
    <li> - Subpage 3 </li>
    <li> - Subpage 1 </li>
    </ul>

    Cheers

    Crawn 

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Nov 30, 2011 @ 13:43
    Dirk De Grave
    0

    Ah, afraid such an xslt extension doens't exist atm... Might want to add that as a feature request on the uComponents Codeplex page. Doesn't help you out right now. Any case, it will require some c# coding, either by you or the uComponents team...

     

    Cheers,

    /Dirk

  • Lee Kelleher 4024 posts 15833 karma points MVP 13x admin c-trib
    Nov 30, 2011 @ 14:40
    Lee Kelleher
    1

    Hi Crawn,

    Based on various experiments, you could try the following sort expression...

    <xsl:sort select="position() mod Exslt.ExsltMath:random()" order="descending"/>

    The key part here is including a reference to the "Exslt.ExsltMath" extensions and calling the "random()" function.  The beauty of this approach is that the modulus for each position against the random number will be unique - so you could base a dynamic sort on that.

    Here's a full snippet:

    <?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="source" select="/macro/source" />
    
        <xsl:template match="/">
            <ul>
                <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/*[@isDoc and string(umbracoNaviHide) != '1' and normalize-space(billede)]">
     <xsl:sort select="position() mod Exslt.ExsltMath:random()" order="descending"/>
    
                    <xsl:variable name="myPic" select="umbraco.library:GetMedia(billede, 0)/umbracoFile" />
    
                    <li class="s3sliderImage">
                        <a href="{umbraco.library:NiceUrl(@id)}">
                            <img src="{concat(substring-before($myPic, '.'), '_FrontSlide.', substring-after($myPic, '.'))}" border="0" alt="{@nodeName}" />
                        </a>
                        <span>
                            <xsl:value-of select="concat(@nodeName, ': ', vejlPris, 'kun kr. ')"/>
                            <font color="#ff0000" size="6" align="right">
                                <xsl:value-of select="pris"/>
                            </font>
                        </span>
                    </li>
                </xsl:for-each>
            </ul>
        </xsl:template>
    
    </xsl:stylesheet>

    Good luck!

    Cheers, Lee.

    PS. Please don't use <font> tags, think about the kittens!

  • Bent Holz 100 posts 273 karma points
    Nov 30, 2011 @ 15:59
    Bent Holz
    0

    Hi Lee

    Like Dirk also a big thank you for taking the time to help out...

    Before I get my hands "dirty" in code... The "Exslt.ExsltMath" is that part of the uComponents package and if yes what extension(s) do i need to install?

    And yes I do worry about the kittens... all this code is just mock-up and will be replaced by css later :)

    Cheers, Crawn


  • Lee Kelleher 4024 posts 15833 karma points MVP 13x admin c-trib
    Nov 30, 2011 @ 16:02
    Lee Kelleher
    0

    "Exslt.ExsltMath" is part of Umbraco core... it is usually included on any new XSLT files that are created via the back-office.

    Cheers, Lee.

  • Bent Holz 100 posts 273 karma points
    Dec 01, 2011 @ 08:50
    Bent Holz
    1

    All I can say... Umbraco kicks ass!... 

    Thanks Lee! That did the trick. The list is now random each time the page is loaded :)

    Think I will go save some kittens now ;)

    Cheers and Merry Christmas  ...I can say this because it's december now!  :)

    /Crawn

     

  • AdrianLove 11 posts 33 karma points
    Mar 23, 2012 @ 00:33
    AdrianLove
    1

    Holy crap Lee Keller. I've been looking for a way to randomly sort for days. using

    <xsl:sort select="floor( Exslt.ExsltMath:random()* $itemCount)" />

    and tearing my hair out because random is based off the clock, thus they would all be showing up as the same item. 

    using mod is ingenious. I have been googling for days. This needs to be made more prominent!

Please Sign in or register to post replies

Write your reply to:

Draft