Copied to clipboard

Flag this post as spam?

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


  • Eddie Foreman 215 posts 288 karma points
    Apr 28, 2010 @ 10:16
    Eddie Foreman
    0

    Homepage link showing in sub menu

    Hi All

    I was wondering if someone could look at the following xslt.  The menu works correclty and list the sub pages with current page.  But adds the hardcoded home page link to all nested list. If possible I need the link to only appear once and should be the first link of the root menu.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [
      <!ENTITY nbsp "&#x00A0;">
    ]>
    <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"/>

      <!-- Input the documenttype you want here -->
      <xsl:variable name="level" select="1"/>

      <xsl:template match="/">

        <xsl:call-template name="menu">
          <xsl:with-param name="level" select="$level"/>
        </xsl:call-template>
      </xsl:template>

      <xsl:template name="menu">
        <xsl:param name="level"/>
        <xsl:if test="count($currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']) &gt; '0'">
          <ul>
    <!--start of home page link -->
            <li>
              <xsl:if test="string($currentPage/@level) = '1'">
                <xsl:attribute name="class">active</xsl:attribute>
              </xsl:if>
              <a href="/">
                <xsl:text>Home</xsl:text>
              </a>
            </li>
    <!--end of home page link -->
            <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']">
              <li>
                <xsl:choose>
                  <xsl:when test="$currentPage/ancestor-or-self::node/@id = current()/@id">
                    <xsl:attribute name="class">active</xsl:attribute>
                    <a href="{umbraco.library:NiceUrl(@id)}">
                      <xsl:value-of select="@nodeName"/>
                    </a>
                  </xsl:when>
                  <xsl:otherwise>
                    <a href="{umbraco.library:NiceUrl(@id)}">
                      <xsl:value-of select="@nodeName"/>
                    </a>
                  </xsl:otherwise>
                </xsl:choose>
                <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
                  <xsl:call-template name="menu">
                    <xsl:with-param name="level" select="$level+1"/>
                  </xsl:call-template>
                </xsl:if>
              </li>
            </xsl:for-each>
          </ul>
        </xsl:if>

      </xsl:template>

    </xsl:stylesheet>

    I've added comment tags around the part of the xlst that adds in the homepage link.  

    Thanks for your help.

    Eddie

  • elspiko 133 posts 302 karma points
    Apr 28, 2010 @ 10:32
    elspiko
    0

    I've just re-jigged your code slightly, hopefully this should sort your problem

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [
      <!ENTITY nbsp "&#x00A0;">
    ]>
    <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"/>
    
      <!-- Input the documenttype you want here -->
      <xsl:variable name="level" select="2"/>
    
      <xsl:template match="/">
        <ul>
            <li>
              <xsl:if test="string($currentPage/@level) = '1'">
                <xsl:attribute name="class">active</xsl:attribute>
              </xsl:if>
              <a href="/">
                <xsl:text>Home</xsl:text>
              </a>
            </li>
            <xsl:call-template name="menu">
              <xsl:with-param name="level" select="$level"/>
            </xsl:call-template>
        </ul>
      </xsl:template>
    
      <xsl:template name="menu">
        <xsl:param name="level"/>
        <xsl:if test="count($currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']) &gt; '0'">
            <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']">
              <li>
                <xsl:choose>
                  <xsl:when test="$currentPage/ancestor-or-self::node/@id = current()/@id">
                    <xsl:attribute name="class">active</xsl:attribute>
                    <a href="{umbraco.library:NiceUrl(@id)}">
                      <xsl:value-of select="@nodeName"/>
                    </a>
                  </xsl:when>
                  <xsl:otherwise>
                    <a href="{umbraco.library:NiceUrl(@id)}">
                      <xsl:value-of select="@nodeName"/>
                    </a>
                  </xsl:otherwise>
                </xsl:choose>
                <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
            <ul>
              <xsl:call-template name="menu">
                <xsl:with-param name="level" select="$level+1"/>
              </xsl:call-template>
            </ul>
                </xsl:if>
              </li>
            </xsl:for-each>
        </xsl:if>
    
      </xsl:template>
    
    </xsl:stylesheet>

     

  • Eddie Foreman 215 posts 288 karma points
    Apr 28, 2010 @ 10:45
    Eddie Foreman
    0

    Hi Elspiko

    Thanks for the quick reply.  Have tested the revised code and only the home page link is listed!  Any ideas?

    Eddie

  • elspiko 133 posts 302 karma points
    Apr 28, 2010 @ 10:51
    elspiko
    0

    Try changing the "level" variable back to 1 - sorry, i haven't woken up properly yet

  • elspiko 133 posts 302 karma points
    Apr 28, 2010 @ 11:37
    elspiko
    0

    if this works can you please mark it as solved :)

  • Eddie Foreman 215 posts 288 karma points
    Apr 28, 2010 @ 11:41
    Eddie Foreman
    0

    Hi Elspiko

    Thanks the home page link is now displayed correctly.  But now the generated sub menus are not nesting correctly.

    <!-- Before amend -->
    <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/service.aspx">Service</a></li>
    <li><a href="/maintenance-contracts.aspx">Maintenance Contracts</a></li>
    <li><a href="/refrigeration.aspx">Refrigeration</a></li>
    <li class="active"><a href="/product-range.aspx">Product Range</a>
    <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/product-range/falcon.aspx">Falcon</a></li>
    <li><a href="/product-range/falcon-(1).aspx">Falcon (1)</a></li>
    <li class="active"><a href="/product-range/falcon-(2).aspx">Falcon (2)</a></li>
    </ul>
    </li>
    <li><a href="/design-and-planning.aspx">Design and Planning</a></li>
    <li><a href="/contact.aspx">Contact</a></li><li><a href="/site-map.aspx">Site Map</a></li>
    <li><a href="/privacy-policy.aspx">Privacy Policy</a></li>
    <li><a href="/terms-and-conditions.aspx">Terms and Conditions</a></li>
    <li><a href="/enquiry.aspx">Enquiry</a></li>
    </ul>

    <!-- After Amend - IE Rendered Menu below -->
    <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/service.aspx">Service</a></li>
    <li><a href="/maintenance-contracts.aspx">Maintenance Contracts</a></li>
    <li><a href="/refrigeration.aspx">Refrigeration</a></li>
    <li class="active"><a href="/product-range.aspx">Product Range</a>
    <ul>
    <li><a href="/product-range/falcon.aspx">Falcon</a></li>
    <li><a href="/product-range/falcon-(1).aspx">Falcon (1)</a></li>
    <li class="active"><a href="/product-range/falcon-(2).aspx">Falcon (2)</a>
    <ul />
    </li>
    </ul>
    </li>
    <li><a href="/design-and-planning.aspx">Design and Planning</a></li>
    <li><a href="/contact.aspx">Contact</a></li>
    <li><a href="/site-map.aspx">Site Map</a></li>
    <li><a href="/privacy-policy.aspx">Privacy Policy</a></li>
    <li><a href="/terms-and-conditions.aspx">Terms and Conditions</a></li>
    <li><a href="/enquiry.aspx">Enquiry</a></li>
    </ul>

    <!--After Amend - FF Rendered Menu below -->
    <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/service.aspx">Service</a></li>
    <li><a href="/maintenance-contracts.aspx">Maintenance Contracts</a></li>
    <li><a href="/refrigeration.aspx">Refrigeration</a></li>
    <li class="active"><a href="/product-range.aspx">Product Range</a>
    <ul>
    <li class="active"><a href="/product-range/falcon.aspx">Falcon</a>
    <ul />
    </li>
    <li><a href="/product-range/falcon-(1).aspx">Falcon (1)</a></li>
    <li><a href="/product-range/falcon-(2).aspx">Falcon (2)</a></li>
    </ul>
    </li>
    <li><a href="/design-and-planning.aspx">Design and Planning</a></li>
    <li><a href="/contact.aspx">Contact</a></li>
    <li><a href="/site-map.aspx">Site Map</a></li>
    <li><a href="/privacy-policy.aspx">Privacy Policy</a></li>
    <li><a href="/terms-and-conditions.aspx">Terms and Conditions</a></li>
    <li><a href="/enquiry.aspx">Enquiry</a></li>
    </ul>

    Thanks

    Eddie

  • elspiko 133 posts 302 karma points
    Apr 28, 2010 @ 12:11
    elspiko
    0
    <xsl:template name="menu">
        <xsl:param name="level"/>
    
            <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']">
              <li>
                <xsl:choose>
                  <xsl:when test="$currentPage/ancestor-or-self::node/@id = current()/@id">
                    <xsl:attribute name="class">active</xsl:attribute>
                    <a href="{umbraco.library:NiceUrl(@id)}">
                      <xsl:value-of select="@nodeName"/>
                    </a>
                  </xsl:when>
                  <xsl:otherwise>
                    <a href="{umbraco.library:NiceUrl(@id)}">
                      <xsl:value-of select="@nodeName"/>
                    </a>
                  </xsl:otherwise>
                </xsl:choose>
                <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
                    <xsl:if test="count($currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']) &gt; '0'">
                        <ul>
                          <xsl:call-template name="menu">
                                <xsl:with-param name="level" select="$level+1"/>
                          </xsl:call-template>
                        </ul>
                    </xsl:if>
                </xsl:if>
              </li>
            </xsl:for-each>
    
    
      </xsl:template>

    A simple mistake I missed, basically you need to check whether the nodes has any children BEFORE opening a ul - replace the current menu template with the above

  • Eddie Foreman 215 posts 288 karma points
    Apr 28, 2010 @ 12:35
    Eddie Foreman
    0

    Hi Elspiko

    I appreiacte all of your help and thanks again for the quick response.   The generated markup is now:

    <li><a href="/service.aspx">Service</a></li>
    <li><a href="/maintenance-contracts.aspx">Maintenance Contracts</a></li>
    <li><a href="/refrigeration.aspx">Refrigeration</a></li>
    <li class="active"><a href="/product-range.aspx">Product Range</a>
    <ul><li class="active"><a href="/product-range/falcon.aspx">Falcon</a>
    <ul />
    </li>
    <li><a href="/product-range/falcon-(1).aspx">Falcon (1)</a></li>
    <li><a href="/product-range/falcon-(2).aspx">Falcon (2)</a></li>
    </ul></li>
    <li><a href="/design-and-planning.aspx">Design and Planning</a></li>
    <li><a href="/contact.aspx">Contact</a></li>
    <li><a href="/site-map.aspx">Site Map</a></li>
    <li><a href="/privacy-policy.aspx">Privacy Policy</a></li>
    <li><a href="/terms-and-conditions.aspx">Terms and Conditions</a></li>
    <li><a href="/enquiry.aspx">Enquiry</a></li>

    All the <li> tags are being produced correctly, but the home page link has gone and the <ul> tags are missing or nested incorrectly.  The above markup is from FF.

    The current xlst file is below:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [
      <!ENTITY nbsp "&#x00A0;">
    ]>
    <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"/>

      <!-- Input the documenttype you want here -->
      <xsl:variable name="level" select="1"/>

      <xsl:template match="/">

        <xsl:call-template name="menu">
          <xsl:with-param name="level" select="$level"/>
        </xsl:call-template>
      </xsl:template>

      <xsl:template name="menu">
        <xsl:param name="level"/>

        <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']">
          <li>
            <xsl:choose>
              <xsl:when test="$currentPage/ancestor-or-self::node/@id = current()/@id">
                <xsl:attribute name="class">active</xsl:attribute>
                <a href="{umbraco.library:NiceUrl(@id)}">
                  <xsl:value-of select="@nodeName"/>
                </a>
              </xsl:when>
              <xsl:otherwise>
                <a href="{umbraco.library:NiceUrl(@id)}">
                  <xsl:value-of select="@nodeName"/>
                </a>
              </xsl:otherwise>
            </xsl:choose>
            <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
              <xsl:if test="count($currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']) &gt; '0'">
                <ul>
                  <xsl:call-template name="menu">
                    <xsl:with-param name="level" select="$level+1"/>
                  </xsl:call-template>
                </ul>
              </xsl:if>
            </xsl:if>
          </li>
        </xsl:for-each>

      </xsl:template>

    </xsl:stylesheet>

    Thanks

    Eddie

  • elspiko 133 posts 302 karma points
    Apr 28, 2010 @ 13:12
    elspiko
    0

    I'll check this out later on today - watch this space ;)

  • Eddie Foreman 215 posts 288 karma points
    Apr 28, 2010 @ 15:16
    Eddie Foreman
    0

    Your a star, many thanks.

  • elspiko 133 posts 302 karma points
    Apr 28, 2010 @ 21:24
    elspiko
    0

    Hi Eddie, sorry its taken so long. Below is a version i've tested and seems to work well with the setup i have

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [
      <!ENTITY nbsp "&#x00A0;">
    ]>
    <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="rootNode" select="$currentPage/ancestor-or-self::node[@level=1]"/>
    
      <xsl:template match="/">
            <ul>
                    <li>
              <xsl:if test="string($currentPage/@level) = '1'">
                <xsl:attribute name="class">active</xsl:attribute>
              </xsl:if>
              <a href="/">
                <xsl:text>Home</xsl:text>
              </a>
            </li>
                    <xsl:apply-templates select="$rootNode/node" />
            </ul>
      </xsl:template>
      <xsl:template match="node">
      <li>
      <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
      <xsl:attribute name="class">active</xsl:attribute>
      </xsl:if>
      <a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName" /></a>
      <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id and count(./node) &gt; 0">
        <ul>
        <xsl:apply-templates select="./node" />
        </ul>
      </xsl:if>
      </li>
      </xsl:template>
    
    </xsl:stylesheet>

    I hope this solves the problem :)

  • Eddie Foreman 215 posts 288 karma points
    Apr 28, 2010 @ 23:19
    Eddie Foreman
    0

    Hi Elspiko

    Tested the changes and the menu structure is spot on :-)  Have tried to add the test so that any items hidden from menu are not displayed. Previously I done this with:

    [string(data [@alias='umbracoNaviHide']) != '1']

    But have tried adding in the test but get an error reading xslt file.  If possible could you add in the test?

    Thanks again for all your time and effort.

    Eddie

     

  • elspiko 133 posts 302 karma points
    Apr 29, 2010 @ 01:27
    elspiko
    0

    Not a problem, happy to help. Sorry is been a bit long winded

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [
      <!ENTITY nbsp "&#x00A0;">
    ]>
    <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"/>
    
      <!-- Input the documenttype you want here -->
      <xsl:variable name="rootNode" select="$currentPage/ancestor-or-self::node[@level=1]"/>
    
      <xsl:template match="/">
            <ul>
                    <li>
              <xsl:if test="string($currentPage/@level) = '1'">
                <xsl:attribute name="class">active</xsl:attribute>
              </xsl:if>
              <a href="/">
                <xsl:text>Home</xsl:text>
              </a>
            </li>
                    <xsl:apply-templates select="$rootNode/node[string(data [@alias='umbracoNaviHide']) != '1']" />
            </ul>
      </xsl:template>
      <xsl:template match="node">
      <li>
      <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
      <xsl:attribute name="class">active</xsl:attribute>
      </xsl:if>
      <a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName" /></a>
      <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id and count(./node[string(data [@alias='umbracoNaviHide']) != '1']) &gt; 0">
        <ul>
        <xsl:apply-templates select="./node[string(data [@alias='umbracoNaviHide']) != '1']" />
        </ul>
      </xsl:if>
      </li>
      </xsl:template>
    
    </xsl:stylesheet>
  • Eddie Foreman 215 posts 288 karma points
    Apr 29, 2010 @ 01:59
    Eddie Foreman
    0

    Worked a treat, thanks again.

Please Sign in or register to post replies

Write your reply to:

Draft