Copied to clipboard

Flag this post as spam?

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


  • Probocop 51 posts 71 karma points
    Jan 17, 2011 @ 17:15
    Probocop
    0

    Active class no applying to navigation.

    Hi, I'm trying to add an 'active' class to highlight the current website section, and the bit of XSLT that I usually use isn't working - Any idea why this wouldn't?

    <xsl:variable name="subPages" select="count($page/*[@isDoc and string(umbracoNaviHide) != '1' and contains($ignoreTypes, concat('#', Exslt.ExsltStrings:uppercase(name()), '#')) = 0])"/>
    <xsl:variable name="class">
      <!-- Not working for some reason -->
      <xsl:if test="$currentPage/ancestor-or-self::*/@id = ./@id">
        <xsl:textactive</xsl:text>
      </xsl:if>
      <xsl:if test="position() = last()">
        <xsl:text>last</xsl:text>
      </xsl:if>
      <xsl:if test="$subPages &gt; 0">
        <xsl:texthas-sub</xsl:text>
      </xsl:if>
    </xsl:variable>
    <li>
      <xsl:if test="string-length($class) &gt; 0">
        <xsl:attribute name="class">
          <xsl:value-of select="normalize-space($class)"/>
        </xsl:attribute>
     </xsl:if>
    </li>

    All the other classes are applying correctly apart from the active one, any idea why not?

    Thanks!

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 17, 2011 @ 17:30
    Tom Fulton
    0

    Just did a quick check, it looks like $currentPage/ancestor-or-self::*/@id is always returning 1 for me.

    You might try something like:

    <xsl:if test="count($currentPage/ancestor-or-self::* [@id = ./@id]) &gt; 0">
     <xsl:text> active</xsl:text>
    </xsl:if>

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jan 17, 2011 @ 19:10
    Jan Skovgaard
    2

    Hi

    Is your if not supposed to look like this making use of the current() function?

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

    /Jan

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Jan 17, 2011 @ 21:40
    Chriztian Steinmeier
    2

    Hi,

    You're right Jan, this is where you need to use current(), but you should put it inside a predicate to select only the ancestor(-or-self) that has this id:

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

    /Chriztian

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jan 17, 2011 @ 21:43
    Jan Skovgaard
    0

    Hi Chriztian

    Yes of course - Guess there really is a reason why I'm using glasses...must be blind! :D

    /Jan

  • Probocop 51 posts 71 karma points
    Jan 18, 2011 @ 11:12
    Probocop
    0

    Hi Chriztian,

    I've just tried using the your example, but I still seem to be without any active class on my <li>. I'm thinking it could be to do with the navigation being built from a MultiNodePicker? My full XSLT is as follows:

    <?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" xmlns:CWS.Twitter="urn:CWS.Twitter" xmlns:umbraco.contour="urn:umbraco.contour" xmlns:ucomponents.cms="urn:ucomponents.cms" xmlns:ucomponents.dates="urn:ucomponents.dates" xmlns:ucomponents.io="urn:ucomponents.io" xmlns:ucomponents.members="urn:ucomponents.members" xmlns:ucomponents.strings="urn:ucomponents.strings" xmlns:ucomponents.urls="urn:ucomponents.urls" xmlns:ucomponents.xml="urn:ucomponents.xml" xmlns:autofolders.library="urn:autofolders.library" xmlns:tags="urn:tags" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets CWS.Twitter umbraco.contour ucomponents.cms ucomponents.dates ucomponents.io ucomponents.members ucomponents.strings ucomponents.urls ucomponents.xml autofolders.library tags PS.XSLTsearch ">

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

      <xsl:param name="currentPage"/>
        
      <xsl:variable name="ignoreTypes" select="Exslt.ExsltStrings:uppercase('#Audio#CaseStudy#Event#NewsArticle#PressRelease#Report#Video#Person#')"/>

      <xsl:template match="/">
        <!-- get the home page so that we can loop through it's top nav property -->
        <xsl:variable name="homePage" select="$currentPage/ancestor-or-self::*[@isDoc and @level=1]" />
        
        <ul id="top-navigation">
          <li class="first"><href="/"><span>Home</span></a></li>
          <xsl:for-each select="$homePage/mainNavigation/MultiNodePicker/nodeId">
            <xsl:variable name="page" select="umbraco.library:GetXmlNodeById(.)" />
            <xsl:if test="$page/umbracoNaviHide != 1">
              <xsl:variable name="subPages" select="count($page/*[@isDoc and string(umbracoNaviHide) != '1' and contains($ignoreTypes, concat('#', Exslt.ExsltStrings:uppercase(name()), '#')) = 0])"/>
              <xsl:variable name="class">
                <xsl:if test="position() = last()">
                  <xsl:text>last</xsl:text>
                </xsl:if>
                <xsl:if test="$subPages &gt; 0">
                  <xsl:texthas-sub</xsl:text>
                </xsl:if>
                <xsl:if test="$currentPage/ancestor-or-self::*/@id = ./@id">
                  <xsl:textactive</xsl:text>
                </xsl:if>
              </xsl:variable>
              <li>
                <xsl:if test="string-length($class) &gt; 0">
                  <xsl:attribute name="class">
                    <xsl:value-of select="normalize-space($class)"/>
                  </xsl:attribute>
                </xsl:if>
                <href="{umbraco.library:NiceUrl($page/@id)}"><span><xsl:value-of select="$page/navigationText"/></span></a>
                <xsl:if test="$subPages &gt; 0">
                  <ul>
                    <xsl:for-each select="$page/*[@isDoc and string(umbracoNaviHide) != '1' and contains($ignoreTypes, concat('#', Exslt.ExsltStrings:uppercase(name()), '#')) = 0]">
                      <li>
                        <xsl:if test="position() = last()">
                          <xsl:attribute name="class">
                            <xsl:text>last</xsl:text>
                          </xsl:attribute>
                        </xsl:if>
                        <href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="navigationText"/></a>
                      </li>
                    </xsl:for-each>
                  </ul>
                </xsl:if>
              </li>
            </xsl:if>
          </xsl:for-each>
        </ul>
      </xsl:template>
    </xsl:stylesheet>

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Jan 18, 2011 @ 12:47
    Chriztian Steinmeier
    0

    Hi Probocop,

    Seeing it all, it's obvious what's wrong - the "current" page inside your for-each loop is $page, so you just need to use that:

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

    /Chriztian

  • Probocop 51 posts 71 karma points
    Jan 18, 2011 @ 13:11
    Probocop
    0

    Hi Chriztian,

    Thanks for that, can't believe I didn't spot it myself!

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft