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:text> active</xsl:text> </xsl:if> <xsl:if test="position() = last()"> <xsl:text>last</xsl:text> </xsl:if> <xsl:if test="$subPages > 0"> <xsl:text> has-sub</xsl:text> </xsl:if> </xsl:variable> <li> <xsl:if test="string-length($class) > 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?
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:
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:
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:text> active</xsl:text>
</xsl:if>
<xsl:if test="position() = last()">
<xsl:text>last</xsl:text>
</xsl:if>
<xsl:if test="$subPages > 0">
<xsl:text> has-sub</xsl:text>
</xsl:if>
</xsl:variable>
<li>
<xsl:if test="string-length($class) > 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!
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]) > 0">
<xsl:text> active</xsl:text>
</xsl:if>
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
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:
/Chriztian
Hi Chriztian
Yes of course - Guess there really is a reason why I'm using glasses...must be blind! :D
/Jan
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 " "> ]>
<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"><a 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 > 0">
<xsl:text> has-sub</xsl:text>
</xsl:if>
<xsl:if test="$currentPage/ancestor-or-self::*/@id = ./@id">
<xsl:text> active</xsl:text>
</xsl:if>
</xsl:variable>
<li>
<xsl:if test="string-length($class) > 0">
<xsl:attribute name="class">
<xsl:value-of select="normalize-space($class)"/>
</xsl:attribute>
</xsl:if>
<a href="{umbraco.library:NiceUrl($page/@id)}"><span><xsl:value-of select="$page/navigationText"/></span></a>
<xsl:if test="$subPages > 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>
<a 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>
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:
/Chriztian
Hi Chriztian,
Thanks for that, can't believe I didn't spot it myself!
Dave
is working on a reply...