Developer forum

Forum » Templates » XSLT - Show 3 unique random ecom groups

XSLT - Show 3 unique random ecom groups


Reply
Hi,

I would like to do the following in XLST:

The page I'm implementing has 7 ecom product groups. I would like to show 3 random groups on the frontpage of the site. The 3 shown groups has to be unique. It is not okay to show groups 1, 1, 4 or 2, 3, 3.

(Possibly using EXSLT http://www.exslt.org/random/functions/random-sequence/index.html - but can't get my head around implementing it)


I've had some success using the following code, but the results are not unique...


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxml="urn:schemas-microsoft-com:xslt" xmlns:random="http://www.microsoft.com/msxsl" exclude-result-prefixes="msxml random">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" encoding="utf-8" />
      <xsl:variable name="antal"><xsl:value-of select="count(/Template/loop[@name='Groups']/item)"/></xsl:variable>
  <msxml:script implements-prefix="random">function range(min, max)
   
    {
   
    var dist = max - min + 1;
   
    return Math.floor(Math.random() * dist + min);
   
    }</msxml:script>

<xsl:variable name="randomValue1">
<xsl:call-template name="GenerateRandomNumbers">
      <xsl:with-param name="curIndex">
        1
      </xsl:with-param>
    </xsl:call-template>
</xsl:variable>
<xsl:variable name="randomValue2">
<xsl:call-template name="GenerateRandomNumbers">
      <xsl:with-param name="curIndex">
        1
      </xsl:with-param>
    </xsl:call-template>
</xsl:variable>
<xsl:variable name="randomValue3">
<xsl:call-template name="GenerateRandomNumbers">
      <xsl:with-param name="curIndex">
        1
      </xsl:with-param>
    </xsl:call-template>
</xsl:variable>





  <xsl:param name="numcolumns" select="3" />
    <xsl:variable name="availableloops" select="'Groups'" />
    <xsl:template match="/">
        <xsl:apply-templates select="Template" />
    </xsl:template>
   <xsl:template match="Template">
        <xsl:call-template name="GroupList" />
    </xsl:template>
    <xsl:template name="GroupList">
    <xsl:value-of select="$randomValue1"/>
    <xsl:value-of select="$randomValue2"/>
    <xsl:value-of select="$randomValue3"/>
        <ul class="groupList">
            <xsl:for-each select="loop[@name='Groups']/item">
           
                <xsl:if test="position() = $randomValue1 or position() = $randomValue2 or position() = $randomValue3">
                    <li>
                        <xsl:attribute name="class">groupListElementBox</xsl:attribute>                          
                        <a>
                            <xsl:attribute name="class">groupListElementHeaderLink</xsl:attribute>
                            <xsl:attribute name="href">
                                da-DK/Produkter.aspx?GroupID=<xsl:value-of select="Ecom.Group.ID" />
                            </xsl:attribute>
                            <xsl:attribute name="title">
                                <xsl:text>Se produkterne i: </xsl:text><xsl:value-of select="Ecom.Group.Name" />
                            </xsl:attribute>
                            <xsl:choose>
                                <xsl:when test="string-length(Ecom.Group.Field.GroupImage.Value.Clean) != 0">
                                    <img class="groupImage" src="{concat('/admin/public/getimage.aspx?image=Files/Billeder/', Ecom.Group.Field.GroupImage.Value.Clean, '&amp;width=218','&amp;height=152','&amp;Crop=0','&amp;compression=95')}" border="0" />
                                </xsl:when>
                                <xsl:when test="string-length(Ecom.Group.Field.GroupImage.Value.Clean) = 0">
                                    <img class="groupImage" src="{concat('/admin/public/getimage.aspx?image=Files/Templates/Designs/AS-Nordana/images/foto-paa-vej.jpg', '&amp;width=218','&amp;height=152','&amp;Crop=0','&amp;compression=95')}" border="0" />
                                </xsl:when>
                             </xsl:choose>
                           
                            <div>
                                <xsl:attribute name="class">groupListElementHeader</xsl:attribute>
                                <xsl:value-of select="Ecom.Group.Name" /><xsl:value-of select="position()"/>
                            </div>
                        </a>
                    </li>
                </xsl:if>
            </xsl:for-each>
        </ul>
    </xsl:template>
    <xsl:template name="GenerateRandomNumbers">
    <xsl:param name="count"/>
    <xsl:param name="curIndex">0</xsl:param>
    <xsl:value-of select="random:range(1, $antal)"/>
    <xsl:if test="number($count) != (number($curIndex) + 1)">
      <xsl:call-template name="GenerateRandomNumbers">
        <xsl:with-param name="count">
          <xsl:value-of select="$count"/>
        </xsl:with-param>
        <xsl:with-param name="curIndex">
          <xsl:value-of select="string(number($curIndex) + 1)"/>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>


Replies

 

You must be logged in to post in the forum