Copied to clipboard

Flag this post as spam?

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


  • Cecilie Tveter 7 posts 27 karma points
    Oct 20, 2010 @ 21:03
    Cecilie Tveter
    0

    Trouble with navigation and selected class

    I have a tree level navigation, and its all working fine.

    One macro for the main navigation and one macro for the subnavigation. When i am on the second level everything works fine. The active element gets its class. But when i move on to the third level i still want the second level element to be active. Se the page here: http://spotpaint.no/om-oss/for-bedrift.aspx

    I want the element "om oss" to have the selected class.

    Here is the xslt for the main navigation:

    <?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:template match="/">
      <xsl:param name="navigationRoot" select="$currentPage/ancestor-or-self::*[@level=1]" />
              <xsl:param name="shouldShowHome" select="1" />
      <xsl:param name="cnt" select="count($navigationRoot/* [@isDoc])" />



    <!-- The fun starts here -->
    <ul>

    <xsl:if test="$shouldShowHome=1">

      <li>

        <a href="{umbraco.library:NiceUrl($currentPage/ancestor-or-self::*[@level=1]/@id)}">

      <xsl:if test="$currentPage/@id = $currentPage/ancestor-or-self::*[@level=1]/@id">


      <xsl:attribute name="class">selected</xsl:attribute>

     </xsl:if>

            <xsl:value-of select="$currentPage/ancestor-or-self::*[@level=1]/@nodeName"/>

        </a>  

      </li>

    </xsl:if>

     

    <xsl:for-each select="$navigationRoot/* [@isDoc]">

        <xsl:if test="./@id &gt; 0 and ./umbracoNaviHide!=1">

          <li><a>

     <xsl:choose>

      <xsl:when test="$cnt=position()">

     <xsl:attribute name="class">

     last<xsl:if test="$currentPage/ancestor-or-self::node/@id= current()/@id"> selected</xsl:if>

     </xsl:attribute>

      </xsl:when>

      <xsl:when test="$currentPage/@id = current()/@id">
        
     
       <xsl:attribute name="class"> selected</xsl:attribute>

      </xsl:when>

     </xsl:choose>

          <xsl:attribute name="href">

            <xsl:value-of select="umbraco.library:NiceUrl(./@id)" />

          </xsl:attribute>

          <xsl:value-of select="./@nodeName"/>

        </a>

        </li>

        </xsl:if>

    </xsl:for-each>

    </ul>

     

    </xsl:template>

    </xsl:stylesheet>

    I really hope someone can help me width this :)

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Oct 20, 2010 @ 21:46
    Lee Kelleher
    1

    Hi Cecilie,

    The problem is that you were referencing the node name as "node" (old XML schema) instead of "*[@isDoc]" (new XML schema).

    No worries... I've tried to help by re-writing a little snippet of your XSLT - which you might find easier to use?

    <xsl:for-each select="$navigationRoot/*[@isDoc]">
        <xsl:if test="umbracoNaviHide != 1">
            <li>
                <a href="{umbraco.library:NiceUrl(@id)}">
                    <xsl:attribute name="class">
                        <xsl:text>link</xsl:text>
                        <xsl:if test="position() = last()">
                            <xsl:text> last</xsl:text>
                        </xsl:if>
                        <xsl:if test="$currentPage/ancestor-or-self::*[@isDoc]/@id = @id">
                            <xsl:text> selected</xsl:text>
                        </xsl:if>
                    </xsl:attribute>
                    <xsl:value-of select="@nodeName" />
                </a>
            </li>
        </xsl:if>
    </xsl:for-each>

    I changed a few things, like moving the HREF to be inline and setting a CSS class for each of the links - so you don't need to double-check for the "selected" page in the "last" section.  Lastly, I removed a few of the "./" (dot-slash) as you don't really need them in this context.

    Cheers, Lee.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Oct 20, 2010 @ 21:47
    Jan Skovgaard
    0

    Hi Cecillie and welcome to the forum :)

    I think it's because of this these lines of code you're having problems:

    t<xsl:if test="$currentPage/ancestor-or-self::node/@id= current()/@id"> - this should read t<xsl:if test="$currentPage/ancestor-or-self::*/@id= current()/@id">

    and

    <xsl:when test="$currentPage/@id = current()/@id"> - This should read <xsl:when test="$currentPage/ancestor-or-self::*/@id = current()/@id">

    I hope this helps.

    /Jan

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Oct 20, 2010 @ 21:48
    Jan Skovgaard
    1

    Too slow, Lee is da man :)

  • Cecilie Tveter 7 posts 27 karma points
    Oct 20, 2010 @ 21:56
    Cecilie Tveter
    0

    Magic, thank you so much, works like a dream :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies