Copied to clipboard

Flag this post as spam?

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


  • Sean Dooley 289 posts 528 karma points
    Apr 14, 2011 @ 14:09
    Sean Dooley
    0

    Current page in subnavigation

    I have created 1 XSLT to deal with the main and sub navigation for a site. The main navigation works without any issues.

    The sub navigation fails when I am checking for the current/selected page. If I remove the current/selected page if statement, then the sub navigation works without any issues.

    I have included the code below so you can see what is happening. Does anyone have any ideas?

    <?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"/>

    <!-- Input the documenttype you want here -->
    <!-- Typically '1' for topnavigtaion and '2' for 2nd level -->
    <!-- Use div elements around this macro combined with css -->
    <!-- for styling the navigation -->
    <xsl:variable name="level" select="/macro/level"/>
    <!--xsl:variable name="level" select="1"/-->

    <xsl:template match="/">
    <xsl:choose>
    <xsl:when test="$level = 1">
    <xsl:call-template name="Main">
    <xsl:with-param name="childNodes" select="$currentPage/ancestor-or-self::* [@isDoc and @level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']" />
    </xsl:call-template>
    </xsl:when>
    <xsl:when test="$level = 2">
    <xsl:call-template name="Subnav">
    <xsl:with-param name="childNodes" select="$currentPage/ancestor-or-self::* [@isDoc and @level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']" />
    </xsl:call-template>
    </xsl:when>
    </xsl:choose>
    </xsl:template>

    <xsl:template name="Main">
    <xsl:param name="childNodes"/>
    <xsl:if test="count($childNodes) > 0">
    <ul class="left_col_style">
    <li>
    <a href="/default.aspx">Home</a>
    </li>
    <xsl:for-each select="$childNodes">
    <li>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:if test="$currentPage/ancestor-or-self::*/@id = current()/@id">
    <!-- we're under the item - you can do your own styling here -->
    <xsl:attribute name="class">selected</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="@nodeName"/>
    </a>
    </li>
    </xsl:for-each>
    </ul>
    </xsl:if>
    </xsl:template>

    <xsl:template name="Subnav">
    <xsl:param name="childNodes"/>
    <xsl:if test="count($childNodes) > 0">
    <ul id="sub" class="navigation">
    <xsl:for-each select="$childNodes">
    <li>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:attribute name="class">
    <xsl:if test="$currentPage/ancestor-or-self::*/@id = current()/@id">
    <xsl:attribute name="class">selected </xsl:attribute>
    </xsl:if>
    <xsl:if test="position() = last()">
    <xsl:text>last</xsl:text>
    </xsl:if>
    </xsl:attribute>
    <xsl:value-of select="@nodeName"/>
    </a>
    </li>
    </xsl:for-each>
    </ul>
    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>
  • John Walker 43 posts 66 karma points
    Apr 14, 2011 @ 15:13
    John Walker
    0

    Hello Sean,

    Below is a xslt I created the other day for the same sort of navigation hopefully this will help.

    <?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"
    exclude-result-prefixes="msxml umbraco.library">


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

    <xsl:param name="currentPage"/>

    <!-- Input the documenttype you want here -->
    <xsl:variable name="level" select="1"/>
    <xsl:variable name="maxLevel" select="3"/>
    <xsl:variable name="parentNodeId" select="$currentPage/@parentID"></xsl:variable>
    <xsl:variable name="parentNode" select="umbraco.library:GetXmlNodeById($currentPage/@parentID)"></xsl:variable>

    <xsl:template match="/">

    <!-- The fun starts here -->
    <ul id="nav">

    <li class="blue">
    <a href="/">
    Home
    </a>
    </li>

    <xsl:for-each select="$currentPage/ancestor-or-self::* [@level = $level]/* [string(umbracoNaviHide) != '1' and @isDoc]">
    <li class="{./sectionColour}">
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:value-of select="@nodeName"/>
    </a>

    <!-- Loop over childs -->
    <xsl:if test="$currentPage/ancestor-or-self::*[@isDoc]/@id = @id or ./@id = $parentNode/@id">
    <xsl:if test="count($currentPage/* [@isDoc and string(umbracoNaviHide) != '1' and @isDoc and @level = $maxLevel]) &gt; 0">
    <ul>
    <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1' and @isDoc and @level = $maxLevel]">
    <li>
    <a href="{umbraco.library:NiceUrl(@id)}">

    <xsl:if test="$currentPage/@id = @id">
    <xsl:attribute name="class">
    <xsl:text>selected</xsl:text>
    </xsl:attribute>
    </xsl:if>

    <xsl:value-of select="@nodeName"/>
    </a>
    </li>
    </xsl:for-each>
    </ul>
    </xsl:if>
    </xsl:if>

    <!-- Loop over parent childs -->
    <xsl:if test="./@id = $parentNode/@id">
    <xsl:if test="count($parentNode/* [@isDoc and string(umbracoNaviHide) != '1' and @isDoc and @level = $maxLevel]) &gt; 0">
    <ul>
    <xsl:for-each select="$parentNode/* [@isDoc and string(umbracoNaviHide) != '1' and @isDoc and @level = $maxLevel]">
    <li>
    <a href="{umbraco.library:NiceUrl(@id)}">

    <xsl:if test="$currentPage/@id = @id">
    <xsl:attribute name="class">
    <xsl:text>selected</xsl:text>
    </xsl:attribute>
    </xsl:if>

    <xsl:value-of select="@nodeName"/>
    </a>
    </li>
    </xsl:for-each>
    </ul>
    </xsl:if>
    </xsl:if>
    </li>

    </xsl:for-each>
    </ul>

    </xsl:template>

    </xsl:stylesheet>
  • Sean Dooley 289 posts 528 karma points
    Apr 14, 2011 @ 15:31
    Sean Dooley
    0

    Thanks for the reply John.

    I have managed to fix it using the following code. The issue was around how the class attribute and current page was being tested.

    <xsl:template name="Subnav">
    <xsl:param name="childNodes"/>
    <xsl:if test="count($childNodes) > 0">
    <xsl:if test="$currentPage/@id = @id">
    <p>success</p>
    </xsl:if>
    <ul id="sub" class="navigation">
    <xsl:for-each select="$childNodes">
    <li>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:attribute name="class">
    <xsl:if test="$currentPage/ancestor-or-self::*/@id = @id">
    <xsl:text>selected </xsl:text>
    </xsl:if>
    <xsl:if test="position() = last()">
    <xsl:text>last</xsl:text>
    </xsl:if>
    </xsl:attribute>
    <xsl:value-of select="@nodeName"/>
    </a>
    </li>
    </xsl:for-each>
    </ul>
    </xsl:if>
    </xsl:template>
  • 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.

    Continue discussion

Please Sign in or register to post replies