Copied to clipboard

Flag this post as spam?

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


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 28, 2010 @ 10:57
    Bo Damgaard Mortensen
    0

    Get related nodes and display them random

    Hi all,

    I'm struggeling a bit with some xslt code where I need to get all related nodes to the current node and then display them random, but limit the amount of nodes displayed by an integer.

    So far, I have this code:

      <xsl:variable name="currentPageId" select="$currentPage/@id" />
      <xsl:variable name="numSpots" select="umbraco.library:GetXmlNodeById(1177)/antalPartnerSpots" />
    <xsl:variable name="spots">
      <xsl:for-each select="umbraco.library:GetRelatedNodesAsXml($currentPage/@id)/relations/relation/node">
          <xsl:if test="../@typeId = 6">
            <xsl:copy-of select="umbraco.library:GetXmlNodeById(@id)" />
          </xsl:if>     
      </xsl:for-each>
        </xsl:variable>

    The above returns a set of the correct nodes, but from there I'm quite lost to be honest :( The following code doesn't work as the $spotCount variable returns 1 (I would guess it's because it's counting one set of nodes and not the actual amount of nodes in the set?)

    <xsl:variable name="spotCount" select="count(msxml:node-set($spots)/node/*)" />
      <xsl:value-of select="$spotCount" /><br />
      <xsl:choose>
        <xsl:when test="$spotCount &lt; $numSpots">
          <xsl:variable name="randomNumber" select="floor(Exslt.ExsltMath:random() * $spotCount) + 1"/>
          <xsl:variable name="randomItem" select="msxml:node-set($spots)/node [position() = $randomNumber]" />
          <xsl:value-of select="$randomItem/@nodeName" />
        </xsl:when>
        <xsl:otherwise>
           <p>No item found</p>
        </xsl:otherwise>
      </xsl:choose>

    Would be great if some of you hardcore xslt-heads out there could shed some light on this ;) Thanks in advance! / Bo

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Nov 28, 2010 @ 11:28
    Morten Bock
    0

    Try something like this:

    <xsl:variable name="currentPageId" select="$currentPage/@id" />
    <xsl:variable name="numSpots" select="umbraco.library:GetXmlNodeById(1177)/antalPartnerSpots" />
    <xsl:variable name="spots" select="umbraco.library:GetRelatedNodesAsXml($currentPage/@id)/relations/relation/node[@typeId = 6]"/>
    
    
    <xsl:variable name="spotCount" select="count($spots)" />
    <xsl:value-of select="$spotCount" />
    <br />
    <xsl:choose>
      <xsl:when test="$spotCount &lt; $numSpots">
        <xsl:variable name="randomNumber" select="floor(Exslt.ExsltMath:random() * $spotCount) + 1"/>
        <xsl:variable name="randomItem" select="$spots[position() = $randomNumber]" />
        <xsl:value-of select="$randomItem/@nodeName" />
      </xsl:when>
      <xsl:otherwise>
        <p>No item found</p>
      </xsl:otherwise>
    </xsl:choose>
    
  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 28, 2010 @ 12:06
    Bo Damgaard Mortensen
    0

    Alright, thanks to Morten I got this solved and the solutions was:

    <xsl:variable name="currentPageId" select="$currentPage/@id" />
    <xsl:variable name="numSpots" select="umbraco.library:GetXmlNodeById(1177)/antalPartnerSpots" />
    <xsl:variable name="spots" select="umbraco.library:GetRelatedNodesAsXml($currentPage/@id)/relations/relation[@typeId = 6]"/>

    <xsl:variable name="spotCount" select="count($spots)" />
    <xsl:value-of select="$spotCount" />
    <br />
    <xsl:choose>
      <xsl:when test="$spotCount &lt; $numSpots">
        <xsl:variable name="randomNumber" select="floor(Exslt.ExsltMath:random() * $spotCount) + 1"/>
        <xsl:variable name="randomItemId" select="$spots[position() = $randomNumber]/node/@id" />
        <xsl:value-of select="$randomItemId" />
      </xsl:when>
      <xsl:otherwise>
          // do something else..
      </xsl:otherwise>
    </xsl:choose>

     

Please Sign in or register to post replies

Write your reply to:

Draft