Copied to clipboard

Flag this post as spam?

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


  • TaoistTotty 246 posts 314 karma points
    Sep 16, 2010 @ 12:17
    TaoistTotty
    0

    Getting value from HTML select to use in XSLT

    I have an some XSLT creating an HTML page by pulling information from a number of different nodes around the whole site.

    This is also using paging so that only 10 results are shown each page.

    I am trying to add a dropdown, currently an HTML select statement, so that the results can be sorted, but am stuck as to how to get the selected value into the XSLT to then change the sort order.

    Hopefully this makes sense. I have posted the whole code below.

     

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;">
                               <!ENTITY node "*[@isDoc]">
                               <!ENTITY hidden "umbracoNaviHide = 1">
    ]>
    <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.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">


    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>
        
        <xsl:variable name="homeMenu" select="$currentPage/@id"/>
        <xsl:variable name="homePage" select="umbraco.library:GetXmlNodeById($homeMenu)"/>
        <xsl:variable name="itemsPerPage" select="/macro/itemsOnPage"/>
        <xsl:variable name="numberOfItems" select="count($currentPage/&node;/&node;)"/>
        <xsl:variable name="pageNumber">
          <xsl:choose>
            <xsl:when test="umbraco.library:RequestQueryString('page') = ''">
              <xsl:value-of select="1"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>

    <xsl:template match="/">
      
      
      <div class="shop-page-number-main shop-page-number-top">
        Sort By:
        <select>
          <option value="ascending">
            Produce A to Z
          </option>
          <option value="descending">
            Produce Z to A
          </option>
          <option value="local">
            Collection Only
          </option>
          <option value="delivery">
            Delivery Only
          </option>
        </select>
        <div class="shop-number-position">
          <xsl:call-template name="paging"/>
        </div>
      </div>
      
      <div id="shop-sub-categories">
        <ul>
          <xsl:for-each select="$homePage | $homePage/&node;">
            <li>
              <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:if test="@id = $currentPage/@id">
                  <xsl:attribute name="class">
                    current
                  </xsl:attribute>
                </xsl:if>
                <xsl:value-of select="@nodeName"/>
              </a>
            </li>
          </xsl:for-each>
        </ul>
      </div>
        
      <xsl:apply-templates select="$currentPage//&node;"/>
      
      <br class="clear"/>
      <div class="shop-page-number-main shop-page-number-bottom">
        <div class="shop-number-position">
          <xsl:call-template name="paging"/>
        </div>
      </div>
    </xsl:template>
        
    <xsl:template match="&node;">
      <xsl:if test="position() &gt; $itemsPerPage * number($pageNumber - 1) and position() &lt;= number($itemsPerPage * number($pageNumber - 1) + $itemsPerPage)">
      <xsl:if test="productName !=''">
      <div class="shop-item">
        <xsl:if test="productImage !=''">
          <xsl:variable name="imageProduct" select="umbraco.library:GetMedia(productImage, 0)"/>
          <img src="{$imageProduct/*}"/>
        </xsl:if>
        <div class="shop-item-details">
          <div class="shop-item-details-top">
            <div class="shop-item-details-title">
              <xsl:value-of select="productName"/>&nbsp;
              <span class="shop-item-details-code">
                <xsl:value-of select="productReferenceNumber"/>
              </span>
            </div>
              <div class="shop-item-conditions">
              <xsl:variable name="preNodes">
              <xsl:for-each select="umbraco.library:Split(qualifierItems, ',')/value">
                <xsl:copy-of select="umbraco.library:GetXmlNodeById(.)"/>
              </xsl:for-each>
            </xsl:variable>
            <xsl:variable name="nodes" select="msxml:node-set($preNodes)/*[@isDoc]"/>
            <xsl:if test="$nodes">
              <xsl:for-each select="$nodes">
                <xsl:variable name="image" select="qualifierImage"/>
                <xsl:choose>
                  <xsl:when test="$image !=''">
                    <xsl:variable name="qualifierImage" select="umbraco.library:GetMedia($image, 0)"/>
                    <xsl:variable name="qualifierAlt" select="qualifierAltTag"/>
                    <img src="{$qualifierImage/*}" alt="{$qualifierAlt}"/>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="quantifierName"/>&nbsp;
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:for-each>
            </xsl:if>
              </div>

            <div class="shop-item-details-collection">
              Collection&nbsp;
              <xsl:choose>
                <xsl:when test="productCollection = 1">
                  <img src="../../../images/shop/delivery-collection/tick.png" alt="tick"/>
                </xsl:when>
                <xsl:otherwise>
                  <img src="../../../images/shop/delivery-collection/cross.png" alt="cross"/>
                </xsl:otherwise>
              </xsl:choose>
            </div>
            <div class="shop-item-details-delivery">
              Delivery&nbsp;
              <xsl:choose>
                <xsl:when test="productDelivery = 1">
                  <img src="../../../images/shop/delivery-collection/tick.png" alt="tick"/>
                </xsl:when>
                <xsl:otherwise>
                  <img src="../../../images/shop/delivery-collection/cross.png" alt="cross"/>
                </xsl:otherwise>
              </xsl:choose>
            </div>
            <br class="clear"/>
          </div>
          <div class="shop-item-details-description">
            <span class="shopdetails">Product Description:&nbsp;</span><xsl:value-of select="productDescription"/>
            <br />
            <span class="shopdetails">Conditions:&nbsp;</span><xsl:value-of select="productConditions"/>
              <br/>
            <xsl:variable name="supplierName" select="umbraco.library:GetXmlNodeById(productSupplier)"/>
            <span class="shopdetails">Supplier:&nbsp;</span><xsl:value-of select="$supplierName"/>
          </div>
          <div class="shop-item-details-grey-bar">
            <div class="shop-item-details-grey-bar-weight">
              Weight:&nbsp;
              <xsl:choose>
                <xsl:when test="weightSoldIn > 0">
                  <xsl:value-of select="weightSoldIn"/>
                </xsl:when>
                <xsl:otherwise>
                  N&#47;A
                </xsl:otherwise>
              </xsl:choose>
            </div>
            <!--<div class="shop-item-details-grey-bar-quantity">
              Number:&nbsp;
              <xsl:choose>
                <xsl:when test="productNumbersold > 0">
                  <xsl:value-of select="productNumbersold"/>
                </xsl:when>
                <xsl:otherwise>
                  N&#47;A
                </xsl:otherwise>
              </xsl:choose>
            </div>-->
            <div class="shop-item-details-dark-grey-price">
              £<xsl:value-of select="productPrice"/>
            </div>
            <div class="shop-item-details-dark-grey-each">
              Each
            </div>
            <br class="clear"/>
          </div>
          <div class="shop-item-details-dark-grey-add">
            Add
          </div>
          <br class="clear"/>
        </div>
      </div>
        </xsl:if>
        </xsl:if>
     </xsl:template>
        <xsl:template match="&node;[&hidden;]"/>   

        <xsl:template name="for.loop">
          <xsl:param name="i"/>
          <xsl:param name="count"/>
          <xsl:param name="page"/>
          <xsl:if test="$i &lt;= $count">
            <xsl:if test="$page != $i">
              <xsl:if test="$i != 0">
                <a class="numberactive" href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$i}">
                  <xsl:value-of select="$i"/>
                </a>
              </xsl:if>
            </xsl:if>
            <xsl:if test="$page = $i">
              <span class="numberinactive">
                <xsl:value-of select="$i"/>
              </span>
            </xsl:if>
          </xsl:if>
          <xsl:if test="$i &lt;= $count">
            <xsl:call-template name="for.loop">
              <xsl:with-param name="i">
                <xsl:value-of select="$i + 1"/>
              </xsl:with-param>
              <xsl:with-param name="count">
                <xsl:value-of select="$count"/>
              </xsl:with-param>
              <xsl:with-param name="page">
                <xsl:value-of select="$page"/>
              </xsl:with-param>
            </xsl:call-template>
          </xsl:if>
        </xsl:template>
        
        <xsl:template name="paging">
           
           <xsl:choose>
             <xsl:when test="$pageNumber &gt; 1">
               <a href="?page={$pageNumber - 1}">
                 <img class="shevron" src="../../../images/shop/page-nav/less-active.png" alt="less active"/>
               </a>
             </xsl:when>
             <xsl:otherwise>
               <img class="shevron" src="../../../images/shop/page-nav/less-inactive.png" alt="less inactive"/>
             </xsl:otherwise>
           </xsl:choose>
           <xsl:call-template name="for.loop">
             <xsl:with-param name="i">0
             </xsl:with-param>
             <xsl:with-param name="page" select="$pageNumber"/>
             <xsl:with-param name="count" select="ceiling($numberOfItems div $itemsPerPage)"/>
           </xsl:call-template>
           <xsl:choose>
             <xsl:when test="(($pageNumber) * $itemsPerPage) &lt; ($numberOfItems)">
               <a href="?page={$pageNumber + 1}">
                 <img class="shevron" src="../../../images/shop/page-nav/more-active.png" alt="more active"/>
               </a>
             </xsl:when>
             <xsl:otherwise>
               <img class="shevron" src="../../../images/shop/page-nav/more-inactive.png" alt="more inactive"/>
             </xsl:otherwise>
           </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
  • Nik Wahlberg 639 posts 1237 karma points MVP
    Sep 16, 2010 @ 14:24
    Nik Wahlberg
    1

    Hi there, it looks like you're trying to sort the content by several different columns and direction. I would suggest the following combination of changes:

    1. Make your select auto postback (to self) and set 2 URL paramaters dir=ASC|dir=DESC and column={columnName}
    2. Use umbraco.library:RequestQueryString('String key') to snag the current values
    3. Set the root data select into a local variable that is set and looped through in a case statement (<xsl:choose>), do a copy-of (<xsl:copy-of select==="." />)
    4. in your for-each, loop over the variable in msxsl:node-set($variableContainingSortedNodesFromCaseStatement)
    This would be my approach since you can't stick xsl:sort tags as children of any other tag. HTH.
    -- Nik

Please Sign in or register to post replies

Write your reply to:

Draft