Copied to clipboard

Flag this post as spam?

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


  • Sean Holmesby 61 posts 82 karma points
    May 23, 2011 @ 15:05
    Sean Holmesby
    0

    Set Variable to a document node

    Hi, I'm having an issue with some XSLT that I'm hoping someone can help me with.

    I have Gold Sponsors, and Silver Sponsors, each under their respective parent nodes, and I'm looking to randomly choose one of the them from the entire list.

    So:-

    - Gold Sponsors

    ----Sponsor 1

    ----Sponsor 2

    - Silver Sponsors

    ----Sponsor 3

    ----Sponsor 4

    ----Sponsor 5

     

    Now my code to figure out an index (from the overall number of sponsors) looks like this:-

     

     

     

    <xsl:variable name="numberOfGoldSponsors" select="count($goldSponsorNode/*[@isDoc])"/>

    <

     

     

    xsl:variable name="numberOfSilverSponsors" select="count($silverSponsorNode/*[@isDoc])"/>

    <

     

     

    xsl:variable name="totalNumberOfSponsors" select="($numberOfGoldSponsors + $numberOfSilverSponsors)" />

    <

     

     

    xsl:variable name="randomNodeIndex" select="floor(Exslt.ExsltMath:random() * $totalNumberOfSponsors) + 1"/>

    What I want to do now is select a specific node, either from the gold or the silver sponsors, depending on the value of the randomNodeIndex. I was hoping to set a variable to be my random node, and work with that by passing it into my template, but I've had no luck.

     

     

     

     

     

     

     

     

     

     

     

    <xsl:variable name="randomNode">

    <xsl:choose>

    <

     

     

    xsl:when test="$randomNodeIndex > $numberOfGoldSponsors">

    <!--

     

     

    Get a silver sponsor -->

    <

     

     

    xsl:value-of select="$silverSponsorNode/*[@isDoc][position() = ($randomNodeIndex- $numberOfGoldSponsors)]"/>

    </

     

     

    xsl:when>

    <

     

     

    xsl:otherwise>

    <!--

     

     

    Get a gold sponsor -->

    <

     

     

    xsl:value-of select="$goldSponsorNode/*[@isDoc][position() = $randomNodeIndex]"/>

    </

     

     

    xsl:otherwise>

    </

     

     

    xsl:choose>

    </

     

     

    xsl:variable>

     

    <

     

     

    xsl:apply-templates mode="sponsorImage" select="$randomNode" />

    Is there a way to set my variable to be my random sponsor, or do I have to call my template directly within the choose statement?

     

    Cheers,

    Sean

  • Sean Holmesby 61 posts 82 karma points
    May 23, 2011 @ 15:20
    Sean Holmesby
    0

    Sorry about the code.... You know how difficult it is to simply post code on this forum?!?!?!

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    May 23, 2011 @ 19:11
    Bo Damgaard Mortensen
    1

    Hi Sean,

    Which version of Umbraco are you working with? If it's 4.6 or 4.7 I'd recommed you to check out the Razor syntax where you're able to write your own extension class for choosing a single, random and unique DynamicNode.

    Check it out here under "Custom extenstion methods"

    http://umbraco.com/follow-us/blog-archive/2011/2/28/umbraco-razor-feature-walkthrough-%E2%80%93-part-3

     

  • Jesper Hauge 298 posts 487 karma points c-trib
    May 23, 2011 @ 22:01
    Jesper Hauge
    1

    Hi Sean,

    Or you could look in this thread to see two other approaches.

    Regards
    Jesper Hauge

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    May 23, 2011 @ 22:52
    Bo Damgaard Mortensen
    0

    Ssh, Jesper - was trying to hijack members to the Razor Empire here ;)

    j/k - nice thread there!

  • Sean Holmesby 61 posts 82 karma points
    May 24, 2011 @ 01:42
    Sean Holmesby
    0

    Haha.... thanks for your help guys. Jesper, I think the xslt in that thread makes sense. I can make my variable be the id of the node, and not the node itself. Then get the node later, either in the template, or when passing it to the template.

    Bo, I need a quick solution, so I might go with the XSLT for now coz that's what I'm most comfortable with.... But I promise you I'll have a look at Razor when I get a chance. :-)

Please Sign in or register to post replies

Write your reply to:

Draft