Copied to clipboard

Flag this post as spam?

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


  • Amir Khan 1287 posts 2744 karma points
    Aug 03, 2011 @ 22:15
    Amir Khan
    0

    Apply class to parent if on child

    Hi, I have this relatively simple code below which is applying a class "current" to a navigation item if you're on a top level node or one of its childre. For some reason though, its applying the class to ALL top level nodes. Any idea what I'm doing wrong?

    Thanks for your help!

    Amir

    <xsl:param name="currentPage"/>

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

        <xsl:template match="/">

          
     <ul id="topNavigation">
          <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
      <li>
         
        <a href="{umbraco.library:NiceUrl(@id)}">
          <xsl:if test="count($currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']) &gt; 0">
            <xsl:attribute name="class">current</xsl:attribute>
          </xsl:if>
          <xsl:value-of select="@nodeName"/>
        </a>
      </li>
      
      
    </xsl:for-each>
    </ul>

        </xsl:template>
  • Amir Khan 1287 posts 2744 karma points
    Aug 03, 2011 @ 22:25
    Amir Khan
    0

    Of course, the desired behavior was built right into the navigation prototype ;)

  • praveity 100 posts 125 karma points
    Aug 04, 2011 @ 14:16
    praveity
    0

    You need to check if the currentpage is same as that within the loop of for-each.

    Here is modified code of your xslt. Hope this helps

    <xsl:param name="currentPage"/>
    
        <!-- Input the documenttype you want here -->
        <xsl:variable name="level" select="1"/>
    
        <xsl:template match="/">
    
          <xsl:if test="count($currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']) &gt; 0">
     <ul id="topNavigation">
          <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
          <xsl:choose>
          <xsl:when test="$currentPage/@id = ./@id">
          <li>
    
        <a href="{umbraco.library:NiceUrl(@id)}">
    
            <xsl:attribute name="class">current</xsl:attribute>
    
          <xsl:value-of select="@nodeName"/>
        </a>
      </li>
          </xsl:when>
          <xsl:otherwise>
          <li>
    
        <a href="{umbraco.library:NiceUrl(@id)}">
          <xsl:value-of select="@nodeName"/>
        </a>
      </li>
          </xsl:otherwise>
          </xsl:choose>
    
    </xsl:for-each>
    </ul>
    </xsl:if>
        </xsl:template>
Please Sign in or register to post replies

Write your reply to:

Draft