Link to home
Start Free TrialLog in
Avatar of datzent83
datzent83Flag for United States of America

asked on

XSL break table after a certain number or columns

Hello Experts,

I am sure there is an easy way of doing this but I need to be able to break a table into a new raw after a certain number of columns.

Here is my code:

<td align="center" valign="middle">
<a href="{aspdnsf:ProductLink(ProductID, SEName, $EntityID, $EntityName)}">
<xsl:value-of select="aspdnsf:LookupImage(ProductID, 'product', 'icon', 1)" disable-output-escaping="yes"/>
</a>
<br/>
<a href="{aspdnsf:ProductLink(ProductID, SEName, $EntityID, $EntityName)}">
<xsl:value-of select="$vName" disable-output-escaping="yes"/>
</a>
</td>

Basically what I want to do is every time the column reaches a certain number (lets say 3) then the table breaks into a new raw and continues displaying the content on a new raw.

I know there is a way to do it with the XSL position() function but I can't seem to figure out how to implement that into my code.

Thank you all in advance.
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of datzent83

ASKER

Hello Gertone

I am having some difficulties implementing your code into my XML Package. I pasted my whole code below, can you please make the appropriate changes in that code?

<?xml version="1.0" encoding="utf-8" ?>
<package displayname="Best Sellers" version="2.1" debug="false" allowengine="true">

      <!-- ###################################################################################################### -->
      <!-- Copyright AspDotNetStorefront.com, 1995-2006.  All Rights Reserved.                                              -->
      <!-- http://www.aspdotnetstorefront.com                                                                                                    -->
      <!-- For details on this license please visit  the product homepage at the URL above.                            -->
      <!-- THE ABOVE NOTICE MUST REMAIN INTACT.                                                                   -->
      <!-- $Header: /v6.1/Web/XmlPackages/product.SimpleVariantFormat.xml.config 1     12/30/05 2:33p Administrator $              -->
      <!--                                                                                                        -->
      <!-- ###################################################################################################### -->

    <query name="Products" rowElementName="Product">
        <sql>
            <![CDATA[
                exec aspdnsf_BestSellers 180, @return, @orderby
                exec aspdnsf_GetProducts
                    @categoryID = @CatID,
                    @sectionID = @SecID,
                    @manufacturerID = @ManID,
                    @distributorID = @DistID,
                    @localeName = @locale,
                    @CustomerLevelID = @CustLevelID,
                    @affiliateID = @AffID,
                    @ProductTypeID = @ProdTypeID,
                    @ViewType = 1,
                    @pagenum = @pgnum,
                    @pagesize = 24,
                    @StatsFirst = 0,
                    @publishedonly = 1,
                    @ExcludePacks = 0,
                    @ExcludeKits = 0,
                    @ExcludeSysProds = 0,
                    @InventoryFilter = @InvFilter,
                    @sortEntityName = @entityname
            ]]>
        </sql>
        <queryparam paramname="@return" paramtype="appconfig" requestparamname="BestSellersN" sqlDataType="int" defvalue="10"  validationpattern="^\d{1,10}$" />
        <queryparam paramname="@orderby" paramtype="request" requestparamname="orderby" sqlDataType="int" defvalue="1"  validationpattern="^\d$" />
        <queryparam paramname="@CatID" paramtype="runtime" requestparamname="CatID" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@SecID" paramtype="runtime" requestparamname="SecID" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@ManID" paramtype="runtime" requestparamname="ManID" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@DistID" paramtype="runtime" requestparamname="DistID" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@locale" paramtype="runtime" requestparamname="LocaleSetting" sqlDataType="varchar" defvalue="en-US"  validationpattern="" />
        <queryparam paramname="@CustLevelID" paramtype="runtime" requestparamname="CustomerLevelID" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@AffID" paramtype="runtime" requestparamname="AffiliateID" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@ProdTypeID" paramtype="runtime" requestparamname="ProductTypeFilterID" sqlDataType="int" defvalue="1"  validationpattern="" />
        <queryparam paramname="@pgnum" paramtype="request" requestparamname="pagenum" sqlDataType="int" defvalue="1"  validationpattern="" />
        <queryparam paramname="@InvFilter" paramtype="appconfig" requestparamname="HideProductsWithLessThanThisInventoryLevel" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@entityname" paramtype="runtime" requestparamname="EntityName" sqlDataType="varchar" defvalue=""  validationpattern="" />
    </query>

    <PackageTransform>
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aspdnsf="urn:aspdnsf" exclude-result-prefixes="aspdnsf">
            <xsl:output method="html" omit-xml-declaration="yes" />
           
            <xsl:param name="ShowSubcatsInGrid"><xsl:value-of select="aspdnsf:AppConfig('ShowSubcatsInGrid')" /></xsl:param>
            <xsl:param name="SubcatGridCols"><xsl:value-of select="/root/EntityHelpers/*[name()=/root/Runtime/EntityName]/descendant::Entity[EntityID=/root/Runtime/EntityID]/ColWidth" /></xsl:param>
            <xsl:param name="EntityName"><xsl:value-of select="/root/Runtime/EntityName" /></xsl:param>
            <xsl:param name="EntityID"><xsl:value-of select="/root/Runtime/EntityID" /></xsl:param>
            <xsl:param name="WholesaleOnlySite"><xsl:value-of select="aspdnsf:AppConfigBool('WholesaleOnlySite')"/></xsl:param>

            <xsl:param name="LocaleSetting" select="/root/Runtime/LocaleSetting" />
            <xsl:param name="WebConfigLocaleSetting" select="/root/Runtime/WebConfigLocaleSetting" />

            <xsl:param name="BestSellersShowSales"><xsl:value-of select="aspdnsf:AppConfigBool('BestSellersShowSales')" disable-output-escaping="yes" /></xsl:param>
            <xsl:param name="ShowPics"><xsl:value-of select="aspdnsf:AppConfigBool('BestSellersShowPics')"/></xsl:param>
            <xsl:param name="colspan"><xsl:choose><xsl:when test="BestSellersShowSales='true'">5</xsl:when><xsl:otherwise>4</xsl:otherwise></xsl:choose></xsl:param>
            <xsl:param name="ProductCount" select="count(/root/Products/Product)" />


            <xsl:template match="/">
                <xsl:choose>
                    <xsl:when test="$ProductCount &gt; 0">
                        <table width="100%" cellpadding="2" cellspacing="0" border="0">
                            <xsl:attribute name="style">border-style: solid; border-width: 0px; border-color: #<xsl:value-of select="aspdnsf:AppConfig('HeaderBGColor')" /></xsl:attribute>
                            <tr>
                                <td align="left" valign="top">
                                    <table width="100%" cellpadding="4" cellspacing="0" border="0">
                                        <xsl:attribute name="style"><xsl:value-of select="aspdnsf:AppConfig('BoxFrameStyle')" /></xsl:attribute>
                                        <tr>
                                            <td align="left" valign="top">
                                                <table border="0" cellpadding="0" cellspacing="1" width="100%">
                                                    <tr>
                                                        <xsl:if test="BestSellersShowSales='true'">
                                                            <td align="center"><b><a href="bestsellers.aspx?orderby=units"><xsl:value-of select="aspdnsf:GetString('bestsellers.aspx.1')" disable-output-escaping="yes" /></a></b></td>
                                                            <td align="center"><b><a href="bestsellers.aspx?orderby=amount"><xsl:value-of select="aspdnsf:GetString('bestsellers.aspx.2')" disable-output-escaping="yes" /></a></b></td>
                                                        </xsl:if>
                                                    </tr>
                                                    <tr><td colspan="{$colspan}" height="4" width="100%"><img src="images/spacer.gif" height="1" width="1" /></td></tr>
                                                    <xsl:apply-templates select="/root/Products/Product"></xsl:apply-templates>
                                                </table>
                                            </td>
                                        </tr>
                                        <tr><td colspan="{$colspan}">&#0160;</td></tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </xsl:when>
                    <xsl:otherwise>
                        <p align="left"><b><xsl:value-of select="aspdnsf:GetString('bestsellers.aspx.3')" disable-output-escaping="yes" /></b></p>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:template>
           
            <xsl:template match="Product">
                <xsl:param name="pName">
                    <xsl:choose>
                        <xsl:when test="count(ProductName/ml/locale[@name=$LocaleSetting])!=0">
                            <xsl:value-of select="ProductName/ml/locale[@name=$LocaleSetting]"/>
                        </xsl:when>
                        <xsl:when test="count(ProductName/ml/locale[@name=$WebConfigLocaleSetting]) !=0 ">
                            <xsl:value-of select="ProductName/ml/locale[@name=$WebConfigLocaleSetting]"/>
                        </xsl:when>
                        <xsl:when test="count(ProductName/ml)=0">
                            <xsl:value-of select="ProductName"/>
                        </xsl:when>
                    </xsl:choose>
                </xsl:param>

                <xsl:param name="vName">
                    <xsl:choose>
                        <xsl:when test="count(VariantName/ml/locale[@name=$LocaleSetting])!=0">
                            <xsl:value-of select="VariantName/ml/locale[@name=$LocaleSetting]"/>
                        </xsl:when>
                        <xsl:when test="count(VariantName/ml/locale[@name=$WebConfigLocaleSetting]) !=0 ">
                            <xsl:value-of select="VariantName/ml/locale[@name=$WebConfigLocaleSetting]"/>
                        </xsl:when>
                        <xsl:when test="count(VariantName/ml)=0">
                            <xsl:value-of select="VariantName"/>
                        </xsl:when>
                    </xsl:choose>
                </xsl:param>

                <xsl:param name="ProductName"><xsl:value-of select="$pName" /><xsl:if test="$vName!=''"><xsl:value-of select="concat('-', $vName)" /></xsl:if></xsl:param>
                <xsl:param name="ProductSKU"><xsl:value-of select="SKU" /><xsl:if test="SKUSuffix!=''"><xsl:value-of select="SKUSuffix" /></xsl:if></xsl:param>
                   
                    <td align="center" valign="middle">
                        <a href="{aspdnsf:ProductLink(ProductID, SEName, $EntityID, $EntityName)}">
                        <xsl:value-of select="aspdnsf:LookupImage(ProductID, 'product', 'icon', 1)" disable-output-escaping="yes"/>
                    </a>
                    <br/>
                        <a href="{aspdnsf:ProductLink(ProductID, SEName, $EntityID, $EntityName)}">
                        <xsl:value-of select="$vName" disable-output-escaping="yes"/>
                    </a>
                    </td>
            </xsl:template>

        </xsl:stylesheet>
    </PackageTransform>
</package>


Thank you in advance!
datzent83,
> <xsl:apply-templates select="/root/Products/Product"></xsl:apply-templates>

put the  <xsl:for-each select="//Product[position() mod 3 = 1]">
loop where currently the apply-templates is

put the <xsl:template name="processCell">
somewhere in your stylesheet and it should work

cheers

Geert
Hello Geert,

I have applied the code the way you instructed, but I am getting an error which I have never seen before.

This is the error that I am getting:
Exception=Last Trace Point=[]. An error occurred during a call to extension function 'ProductLink'. See InnerException for a complete description of the error.

Below is the code with your instructions. Please take a look at it and let me know what the problem is:

<?xml version="1.0" encoding="utf-8" ?>
<package displayname="Best Sellers" version="2.1" debug="false" allowengine="true">

    <query name="Products" rowElementName="Product">
        <sql>
            <![CDATA[
                exec aspdnsf_BestSellers 180, @return, @orderby
                exec aspdnsf_GetProducts
                    @categoryID = @CatID,
                    @sectionID = @SecID,
                    @manufacturerID = @ManID,
                    @distributorID = @DistID,
                    @localeName = @locale,
                    @CustomerLevelID = @CustLevelID,
                    @affiliateID = @AffID,
                    @ProductTypeID = @ProdTypeID,
                    @ViewType = 1,
                    @pagenum = @pgnum,
                    @pagesize = 24,
                    @StatsFirst = 0,
                    @publishedonly = 1,
                    @ExcludePacks = 0,
                    @ExcludeKits = 0,
                    @ExcludeSysProds = 0,
                    @InventoryFilter = @InvFilter,
                    @sortEntityName = @entityname
            ]]>
        </sql>
        <queryparam paramname="@return" paramtype="appconfig" requestparamname="BestSellersN" sqlDataType="int" defvalue="10"  validationpattern="^\d{1,10}$" />
        <queryparam paramname="@orderby" paramtype="request" requestparamname="orderby" sqlDataType="int" defvalue="1"  validationpattern="^\d$" />
        <queryparam paramname="@CatID" paramtype="runtime" requestparamname="CatID" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@SecID" paramtype="runtime" requestparamname="SecID" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@ManID" paramtype="runtime" requestparamname="ManID" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@DistID" paramtype="runtime" requestparamname="DistID" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@locale" paramtype="runtime" requestparamname="LocaleSetting" sqlDataType="varchar" defvalue="en-US"  validationpattern="" />
        <queryparam paramname="@CustLevelID" paramtype="runtime" requestparamname="CustomerLevelID" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@AffID" paramtype="runtime" requestparamname="AffiliateID" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@ProdTypeID" paramtype="runtime" requestparamname="ProductTypeFilterID" sqlDataType="int" defvalue="1"  validationpattern="" />
        <queryparam paramname="@pgnum" paramtype="request" requestparamname="pagenum" sqlDataType="int" defvalue="1"  validationpattern="" />
        <queryparam paramname="@InvFilter" paramtype="appconfig" requestparamname="HideProductsWithLessThanThisInventoryLevel" sqlDataType="int" defvalue="0"  validationpattern="" />
        <queryparam paramname="@entityname" paramtype="runtime" requestparamname="EntityName" sqlDataType="varchar" defvalue=""  validationpattern="" />
    </query>

    <PackageTransform>
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aspdnsf="urn:aspdnsf" exclude-result-prefixes="aspdnsf">
            <xsl:output method="html" omit-xml-declaration="yes" />
           
            <xsl:param name="ShowSubcatsInGrid"><xsl:value-of select="aspdnsf:AppConfig('ShowSubcatsInGrid')" /></xsl:param>
            <xsl:param name="SubcatGridCols"><xsl:value-of select="/root/EntityHelpers/*[name()=/root/Runtime/EntityName]/descendant::Entity[EntityID=/root/Runtime/EntityID]/ColWidth" /></xsl:param>
            <xsl:param name="EntityName"><xsl:value-of select="/root/Runtime/EntityName" /></xsl:param>
            <xsl:param name="EntityID"><xsl:value-of select="/root/Runtime/EntityID" /></xsl:param>
            <xsl:param name="WholesaleOnlySite"><xsl:value-of select="aspdnsf:AppConfigBool('WholesaleOnlySite')"/></xsl:param>

            <xsl:param name="LocaleSetting" select="/root/Runtime/LocaleSetting" />
            <xsl:param name="WebConfigLocaleSetting" select="/root/Runtime/WebConfigLocaleSetting" />

            <xsl:param name="BestSellersShowSales"><xsl:value-of select="aspdnsf:AppConfigBool('BestSellersShowSales')" disable-output-escaping="yes" /></xsl:param>
            <xsl:param name="ShowPics"><xsl:value-of select="aspdnsf:AppConfigBool('BestSellersShowPics')"/></xsl:param>
            <xsl:param name="colspan"><xsl:choose><xsl:when test="BestSellersShowSales='true'">5</xsl:when><xsl:otherwise>4</xsl:otherwise></xsl:choose></xsl:param>
            <xsl:param name="ProductCount" select="count(/root/Products/Product)" />

            <xsl:template match="/">
                <xsl:choose>
                    <xsl:when test="$ProductCount &gt; 0">
                        <table width="100%" cellpadding="2" cellspacing="0" border="0">
                            <xsl:attribute name="style">border-style: solid; border-width: 0px; border-color: #<xsl:value-of select="aspdnsf:AppConfig('HeaderBGColor')" /></xsl:attribute>
                            <tr>
                                <td align="left" valign="top">
                                    <table width="100%" cellpadding="4" cellspacing="0" border="0">
                                        <xsl:attribute name="style"><xsl:value-of select="aspdnsf:AppConfig('BoxFrameStyle')" /></xsl:attribute>
                                        <tr>
                                            <td align="left" valign="top">
                                                <table border="0" cellpadding="0" cellspacing="1" width="100%">
                                                    <tr>
                                                        <xsl:if test="BestSellersShowSales='true'">
                                                            <td align="center"><b><a href="bestsellers.aspx?orderby=units"><xsl:value-of select="aspdnsf:GetString('bestsellers.aspx.1')" disable-output-escaping="yes" /></a></b></td>
                                                            <td align="center"><b><a href="bestsellers.aspx?orderby=amount"><xsl:value-of select="aspdnsf:GetString('bestsellers.aspx.2')" disable-output-escaping="yes" /></a></b></td>
                                                        </xsl:if>
                                                    </tr>
                                                    <tr><td colspan="{$colspan}" height="4" width="100%"><img src="images/spacer.gif" height="1" width="1" /></td></tr>
                                                    <xsl:apply-templates select="/root/Products/Product"></xsl:apply-templates>
                                                    <xsl:for-each select="//Product[position() mod 3 = 1]">
                                    <tr>
                                  <xsl:call-template name="processCell">
                                            <xsl:with-param name="thisProductID" select="ProductID"/>
                                            <xsl:with-param name="thisSEName" select="SEName"/>
                                  </xsl:call-template>
                                  <xsl:call-template name="processCell">
                                            <xsl:with-param name="thisProductID" select="following-sibling::Product[1]/ProductID"/>
                                            <xsl:with-param name="thisSEName" select="following-sibling::Product[1]/SEName"/>
                                  </xsl:call-template>
                                  <xsl:call-template name="processCell">
                                            <xsl:with-param name="thisProductID" select="following-sibling::Product[2]/ProductID"/>
                                            <xsl:with-param name="thisSEName" select="following-sibling::Product[2]/SEName"/>
                                  </xsl:call-template>
                                    </tr>
                          </xsl:for-each>
                                                </table>
                                            </td>
                                        </tr>
                                        <tr><td colspan="{$colspan}">&#0160;</td></tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </xsl:when>
                    <xsl:otherwise>
                        <p align="left"><b><xsl:value-of select="aspdnsf:GetString('bestsellers.aspx.3')" disable-output-escaping="yes" /></b></p>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:template>

            <xsl:template name="processCell">
            <xsl:param name="pName">
                    <xsl:choose>
                        <xsl:when test="count(ProductName/ml/locale[@name=$LocaleSetting])!=0">
                            <xsl:value-of select="ProductName/ml/locale[@name=$LocaleSetting]"/>
                        </xsl:when>
                        <xsl:when test="count(ProductName/ml/locale[@name=$WebConfigLocaleSetting]) !=0 ">
                            <xsl:value-of select="ProductName/ml/locale[@name=$WebConfigLocaleSetting]"/>
                        </xsl:when>
                        <xsl:when test="count(ProductName/ml)=0">
                            <xsl:value-of select="ProductName"/>
                        </xsl:when>
                    </xsl:choose>
                </xsl:param>

                <xsl:param name="vName">
                    <xsl:choose>
                        <xsl:when test="count(VariantName/ml/locale[@name=$LocaleSetting])!=0">
                            <xsl:value-of select="VariantName/ml/locale[@name=$LocaleSetting]"/>
                        </xsl:when>
                        <xsl:when test="count(VariantName/ml/locale[@name=$WebConfigLocaleSetting]) !=0 ">
                            <xsl:value-of select="VariantName/ml/locale[@name=$WebConfigLocaleSetting]"/>
                        </xsl:when>
                        <xsl:when test="count(VariantName/ml)=0">
                            <xsl:value-of select="VariantName"/>
                        </xsl:when>
                    </xsl:choose>
                </xsl:param>
                <xsl:param name="ProductName"><xsl:value-of select="$pName" /><xsl:if test="$vName!=''"><xsl:value-of select="concat('-', $vName)" /></xsl:if></xsl:param>
                <xsl:param name="ProductSKU"><xsl:value-of select="SKU" /><xsl:if test="SKUSuffix!=''"><xsl:value-of select="SKUSuffix" /></xsl:if></xsl:param>
                    <xsl:param name="thisProductID"/>
                    <xsl:param name="thisSEName"/>
                    <td align="center" valign="middle">
                        <a href="{aspdnsf:ProductLink($thisProductID, $thisSEName, $EntityID, $EntityName)}">
                            <xsl:value-of select="aspdnsf:LookupImage($thisProductID, 'product', 'icon', 1)" disable-output-escaping="yes"/>
                             </a>
                        <br/>
                        <a href="{aspdnsf:ProductLink($thisProductID, $thisSEName, $EntityID, $EntityName)}">
                            <xsl:value-of select="$vName" disable-output-escaping="yes"/>
                        </a>
                    </td>
                </xsl:template>
        </xsl:stylesheet>
    </PackageTransform>
</package>

Please help me figure out why that is happening! Thank you!
I don't see a definition for this external function
aspdnsf:ProductLink
There is no definition for the other external functions either,
Productlink just happended to be the first one around

you need to implement the functions
something like
      <msxsl:script language="JScript" implements-prefix="aspdnsf">
      <![CDATA[
            function ProductLink()
            {
                                ....
            }
      ]]>
      </msxsl:script>

or something similar if you are not using msxml

Hello Geert,

With your help I have figured out how I can implement the loop into my code. Please take a look at what I have done at the code below. It is not exactly what you have suggested, but it worked. I will however accept your answer as correct! :-)

<xsl:template match="Product">
      <xsl:if test="position()  mod 4 = 1">
            <tr>
                  <xsl:for-each select=". | following-sibling::*[position() &lt; 4]">
                        <xsl:call-template name="ProductCell"></xsl:call-template>
                  </xsl:for-each>
           </tr>
      </xsl:if>
</xsl:template>

Thank you for your help!!!
wellcome,

what you have done is a smart way of avoiding to copy/paste all the template calls, looks nice

cheers

Geert