Copied to clipboard

Flag this post as spam?

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


  • Dan 1285 posts 3917 karma points c-trib
    Oct 06, 2009 @ 15:14
    Dan
    0

    Add class to first level nav when second level nav selected

    Hi,

    Forgive all my XSLT questions today, I'm working on some navigation...

    I have a two-level nested list navigation system, which works great using the code 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"/>
        <xsl:variable name="maxLevelForNav" select="3"/>

        <xsl:template match="/">
            <xsl:call-template name="drawNodes"> 
                <xsl:with-param name="parent" select="$currentPage/ancestor-or-self::node [@level=1]"/>
                <xsl:with-param name="topLevel" select="1"/>
            </xsl:call-template>
        </xsl:template>

        <xsl:template name="drawNodes">
            <xsl:param name="parent"/>
            <xsl:param name="topLevel"/>
            <xsl:if test="umbraco.library:IsProtected($parent/@id, $parent/@path) = 0 or (umbraco.library:IsProtected($parent/@id, $parent/@path) = 1 and umbraco.library:IsLoggedOn() = 1)">
                <ul>
                    <xsl:if test="string($topLevel) = '1'">
                        <xsl:attribute name="id"><xsl:text>nav</xsl:text></xsl:attribute>
                        <xsl:attribute name="class"><xsl:text>sf-menu sf-navbar</xsl:text></xsl:attribute>
                    </xsl:if>
                    <xsl:for-each select="$parent/node [string(./data [@alias='umbracoNaviHide']) != '1' and @level &lt;= $maxLevelForNav]">
                        <li>
                            <xsl:if test="$currentPage/@id = current()/@id">
                                <xsl:attribute name="class"><xsl:text>current</xsl:text></xsl:attribute>
                            </xsl:if>
                            <a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a>
                            <xsl:if test="count(./node [string(./data [@alias='umbracoNaviHide']) != '1' and @level &lt;= $maxLevelForNav]) &gt; 0">  
                                <xsl:call-template name="drawNodes">   
                                    <xsl:with-param name="parent" select="."/>   
                                </xsl:call-template> 
                            </xsl:if>
                        </li>
                    </xsl:for-each>
                </ul>
            </xsl:if>
        </xsl:template>
    </xsl:stylesheet>

     

    At the moment it correctly applies a class of 'active' to a selected list item, but it doesn't apply that class to the first level list when you get into the second level.  What I want to do is to keep the class of 'active' in the li tag of the top level of nav when it's sub-nav is selected.

    Does anyone know how to achieve this?

    Thanks folks...

  • dandrayne 1138 posts 2262 karma points
    Oct 06, 2009 @ 15:19
    dandrayne
    1

    Hi again!

    Try this

    <?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="maxLevelForNav" select="3"/>

    <xsl:template match="/">
    <xsl:call-template name="drawNodes">
    <xsl:with-param name="parent" select="$currentPage/ancestor-or-self::node [@level=1]"/>
    <xsl:with-param name="topLevel" select="1"/>
    </xsl:call-template>
    </xsl:template>

    <xsl:template name="drawNodes">
    <xsl:param name="parent"/>
    <xsl:param name="topLevel"/>
    <xsl:if test="umbraco.library:IsProtected($parent/@id, $parent/@path) = 0 or (umbraco.library:IsProtected($parent/@id, $parent/@path) = 1 and umbraco.library:IsLoggedOn() = 1)">
    <ul>
    <xsl:if test="string($topLevel) = '1'">
    <xsl:attribute name="id"><xsl:text>nav</xsl:text></xsl:attribute>
    <xsl:attribute name="class"><xsl:text>sf-menu sf-navbar</xsl:text></xsl:attribute>
    </xsl:if>
    <xsl:for-each select="$parent/node [string(./data [@alias='umbracoNaviHide']) != '1' and @level &lt;= $maxLevelForNav]">
    <li>
    <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <xsl:attribute name="class"><xsl:text>current</xsl:text></xsl:attribute>
    </xsl:if>
    <a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a>
    <xsl:if test="count(./node [string(./data [@alias='umbracoNaviHide']) != '1' and @level &lt;= $maxLevelForNav]) &gt; 0">
    <xsl:call-template name="drawNodes">
    <xsl:with-param name="parent" select="."/>
    </xsl:call-template>
    </xsl:if>
    </li>
    </xsl:for-each>
    </ul>
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>

    basically it's changing the test for "is the id of the link to the current list item also the current page id?" to "is the id of this page also the id of this item in the nav, or its parent's?"

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

     

  • Dan 1285 posts 3917 karma points c-trib
    Oct 06, 2009 @ 15:23
    Dan
    0

    Absolutely perfect!  Thanks again Dan - you're a gent.

  • saintwright 69 posts 77 karma points
    May 25, 2011 @ 06:09
    saintwright
    0

    For me this worked:  <xsl:if test="$currentPage/ancestor-or-self::* [@level=2]/@id = @id">

Please Sign in or register to post replies

Write your reply to:

Draft