Copied to clipboard

Flag this post as spam?

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


  • Trine 29 posts 89 karma points
    Aug 05, 2013 @ 23:29
    Trine
    0

    Showing parent page in sub navigation

    My website has several sections, each with subpages. On these subpages I can use this XSLT to show all the subpages for the current section:

        <?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:twitter.library="urn:twitter.library" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch"     xmlns:google.maps="urn:google.maps"
            exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes     Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets twitter.library     PS.XSLTsearch google.maps ">

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

        <xsl:param name="currentPage"/>

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

        <xsl:template match="/">

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

        </xsl:template>

        </xsl:stylesheet>

     

    This is the "List Subpages by Level" XSLT template already built into Umbraco. But I also need to show the section parent page so the users can see which section they are in.

     

    So if the menu hiararchy is:

    Home

     - 1. Numbers

     -- 1.1. One

     -- 1.2. Two

     - 2. Colours

     -- 2.1. Blue

     -- 2.2. Red

    then on the Blue and Red pages I also need a link to Colours in the sub navigation. I hope this makes sense.

    I'm using Umbraco 4.9.0 if that helps in any way. Any help is gratefully accepted.

    :-)

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Aug 06, 2013 @ 05:57
    Chriztian Steinmeier
    0

    Hi Trine,

    Here's a chunk that usually does it for me when I need this:

    <xsl:param name="currentPage" />
    
    <!-- The level of your "Home" node - usually 1 -->
    <xsl:variable name="homeLevel" select="1" />
    
    <xsl:variable name="sectionsLevel" select="$homeLevel + 1" />
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = $homeLevel]" />
    <xsl:variable name="currentSection" select="$currentPage/ancestor-or-self::*[@level = $sectionsLevel]" />
    
    <xsl:template match="/">
        <ul>
            <!-- Start at the current section -->
            <xsl:apply-templates select="$currentSection" />
        </ul>
    </xsl:template>
    
    <!-- Template for any page -->
    <xsl:template match="*[@isDoc][not(umbracoNaviHIde = 1)]">
        <xsl:variable name="subPages" select="*[@isDoc][not(umbracoNaviHide = 1)]" />
        <li>
            <!-- Add class=selected if this is the current page -->
            <xsl:if test="@id = $currentPage/@id"><xsl:attribute name="class">selected</xsl:attribute></xsl:if>
            <a href="{umbraco.library:NiceUrl(@isDoc)}">
                <xsl:value-of select="@nodeName" />
            </a>
            <!-- Process subPages (if any) -->
            <xsl:if test="$subPages">
                <ul>
                    <xsl:apply-templates select="$subPages" />
                </ul>
            </xsl:if>
        </li>
    </xsl:template>
    

    /Chriztian

    (Edit: Fixed an error in a select attribute)

  • Trine 29 posts 89 karma points
    Aug 07, 2013 @ 19:28
    Trine
    0

     

    Hi Chriztian!

    Thanks for that! It looks like it should work, but the client's server is down so I can't test it at the moment. :-(

    I'll try again tomorrow and let you know!

     

    Trine

     

     

  • Trine 29 posts 89 karma points
    Aug 07, 2013 @ 20:42
    Trine
    0

    Hi Chriztian!

    I'm getting an error when I try the code:

    My code is:

    <?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:twitter.library="urn:twitter.library" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch" xmlns:google.maps="urn:google.maps"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets twitter.library PS.XSLTsearch google.maps ">

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

    <xsl:param name="currentPage" />

    <!-- The level of your "Home" node - usually 1 -->
    <xsl:variable name="homeLevel" select="1" />

    <xsl:variable name="sectionsLevel" select="$homeLevel + 1" />
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = $homeLevel]" />
    <xsl:variable name="currentSection" select="$currentSection/ancestor-or-self::*[@level = $sectionsLevel]" />

    <xsl:template match="/">
        <ul>
            <!-- Start at the current section -->
            <xsl:apply-templates select="$currentSection" />
        </ul>
    </xsl:template>

    <!-- Template for any page -->
    <xsl:template match="*[@isDoc][not(umbracoNaviHIde = 1)]">
        <xsl:variable name="subPages" select="*[@isDoc][not(umbracoNaviHide = 1)]" />
        <li>
            <!-- Add class=selected if this is the current page -->
            <xsl:if test="@id = $currentPage/@id"><xsl:attribute name="class">selected</xsl:attribute></xsl:if>
            <a href="{umbraco.library:NiceUrl(@isDoc)}">
                <xsl:value-of select="@nodeName" />
            </a>
            <!-- Process subPages (if any) -->
            <xsl:if test="$subPages">
                <ul>
                    <xsl:apply-templates select="$subPages" />
                </ul>
            </xsl:if>
        </li>
    </xsl:template>

     

    I've tried adding </xsl:stylesheet> to the end after </xsl:template>, but that is giving me this error:



    Any ideas?

     

    Trine

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Aug 07, 2013 @ 23:30
    Dennis Aaen
    1

    Hi Trine,

    I think I spotted what gives you the error: Try this one and see if it´s goes better.

    <?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:umbraco.contour="urn:umbraco.contour" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets umbraco.contour PS.XSLTsearch ">


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

    <xsl:param name="currentPage" />

    <!-- The level of your "Home" node - usually 1 -->
    <xsl:variable name="homeLevel" select="1" />

    <xsl:variable name="sectionsLevel" select="$homeLevel + 1" />
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = $homeLevel]" />
    <xsl:variable name="currentSection" select="$currentPage/ancestor-or-self::*[@level = $sectionsLevel]" />

    <xsl:template match="/">
        <ul>
            <!-- Start at the current section -->
            <xsl:apply-templates select="$currentSection" />
        </ul>
    </xsl:template>

    <!-- Template for any page -->
    <xsl:template match="*[@isDoc][not(umbracoNaviHIde = 1)]">
        <xsl:variable name="subPages" select="*[@isDoc][not(umbracoNaviHide = 1)]" />
        <li>
            <!-- Add class=selected if this is the current page -->
            <xsl:if test="@id = $currentPage/@id"><xsl:attribute name="class">selected</xsl:attribute></xsl:if>
            <a href="{umbraco.library:NiceUrl(@isDoc)}">
                <xsl:value-of select="@nodeName" />
            </a>
            <!-- Process subPages (if any) -->
            <xsl:if test="$subPages">
                <ul>
                    <xsl:apply-templates select="$subPages" />
                </ul>
            </xsl:if>
        </li>
    </xsl:template>

    </xsl:stylesheet>

    The differnce between the code I posted and this one you have tried is, the select in the variable called currentSection. I have change

    <xsl:variable name="currentSection" select="$currentSection/ancestor-or-self::*[@level = $sectionsLevel]" />

    To

    <xsl:variable name="currentSection" select="$currentPage/ancestor-or-self::*[@level = $sectionsLevel]" />

    Hope this helps you.

    /Dennis

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Aug 07, 2013 @ 23:40
    Chriztian Steinmeier
    0

    Well spotted Dennis - that must've been an auto-completion error on my part - I've corrected my code so future copy/paste actions doesn't fail.

    @Trine: The first error is of course that you needed to close the <xsl:stylesheet> element - the second error, I think, is some sort of typo or a weird character - can you check that you really have </xsl:stylesheet> and not some variation (e.g. 'styleSheet' or 'styllesheet' etc.) ?

    /Chriztian

  • Trine 29 posts 89 karma points
    Aug 08, 2013 @ 00:50
    Trine
    0

    @Chriztian: The </xsl:stylesheet> vanishes down past the bottom of the input window and it won't let me scroll down past </xsl:template> to see it, but it is there. I'll put it down to an Umbraco quirk. :-) I think I tried to put too many </xsl:stylesheet> in to close it and of course that caused an error.

    @Dennis: Your amended version of the code worked better (no error messages when I try to save), but on the site itself I get: "Error parsing XSLT file: \xslt\BranchSubNav4.xslt" where the code should work? What's happening?

    Thank you both for your help so far!

     

    Trine 

  • nadine 25 posts 75 karma points
    Aug 08, 2013 @ 07:19
    nadine
    0

    I've tried this code too and it doesn't work for me! It can't find that first 'currentSection' path so it won't find the subsequent sub pages.

     

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Aug 08, 2013 @ 07:49
    Chriztian Steinmeier
    0

    @Trine: Ah yes, but of course - a duplicate <xsl:stylesheet> element would generate that error too.

    @Nadine: Did you try the fixed version? I've fixed the error related to the $currentSection parameter since posting the original. Could you try adding this querystring to the URL of the page you're running the macro on: ?umbDebugShowTrace=True ? This should output a more detailed error message...

    Note: This macro will not work if you run it on the "Home" page; it only works on the subpages.

    /Chriztian

  • nadine 25 posts 75 karma points
    Aug 08, 2013 @ 07:51
    nadine
    100

    I found a simplistic solution that might help you @Trine that you can build on, it still has a for-each statement and using @Chriztian's methodology should be in an apply templates statement but I don't want to mess with it any more because it works. Hope it's useful for you...

    <xsl:param name="currentPage"/>
    <xsl:template match="/">
     
    <xsl:variable name="level" select="2"/>
    <ul>
      <xsl:apply-templates select="$currentPage/ancestor-or-self::* [@level=$level]" />
    </ul> 
    </xsl:template>
     
    <xsl:template match="*[@isDoc]">
      <li>
        <a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a>  
         <xsl:if test="count(./* [@isDoc and string(umbracoNaviHide) != '1']) &gt; 0">    
       <ul>
                <xsl:for-each select="./* [@isDoc and string(umbracoNaviHide) != '1']">
                    <li>
          <a href="{umbraco.library:NiceUrl(@id)}">
           <xsl:value-of select="@nodeName"/>
          </a>
         </li>
                </xsl:for-each>        
           </ul>
         </xsl:if>
      </li>
    </xsl:template>
    <xsl:template match="*[umbracoNaviHide = 1]" />
    </xsl:stylesheet>
  • nadine 25 posts 75 karma points
    Aug 08, 2013 @ 09:28
    nadine
    0

    Hi @Chriztian yes I tried the fix you suggested but it didn't work for me I did put in the trace URL and it didn't show any extra information on the page or is the trace info written to a log file somewhere?

    I've had a go at getting rid of that for-each statement as it didn't recurse more than the 2 levels and I have surprised myself and done it successfully, I just had to stick in some if statements to control the formatting via CSS. It doesn't look as sexy as your code @Chriztian but it works for me. Love your work!

    Hope you have found this helpful @Trine.

    <xsl:param name="currentPage"/>

    <xsl:template match="/">
     
    <xsl:variable name="level" select="2"/>
    <ul>
     <xsl:apply-templates select="$currentPage/ancestor-or-self::* [@level=$level]" />
    </ul> 
    </xsl:template>
     
    <xsl:template match="*[@isDoc]">
      <xsl:variable name="level" select="2"/> 
       <li>
        <xsl:if test="@level=$level"><xsl:attribute name="class">parentlev</xsl:attribute></xsl:if>
        <xsl:if test="@level=$level+2"><xsl:attribute name="class">gclev</xsl:attribute></xsl:if>
       <a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a>  
      </li>
     
         <xsl:if test="count(./* [@isDoc and string(umbracoNaviHide) != '1']) &gt; 0"> 
       <xsl:apply-templates select="*[@isDoc]"/>
         </xsl:if>
    </xsl:template>
    <xsl:template match="*[umbracoNaviHide = 1]" />
  • Trine 29 posts 89 karma points
    Aug 08, 2013 @ 20:16
    Trine
    0

    @nadine: Awesome! That worked! I just needed to change the level values and it worked perfectly! And I can see how it relates to @Chriztian's code as well so I have definitely learned something here!

    One quick question though: how do I style the <li> and <a> of the current page so the user can see which page they are on? Doing it on a non-responsive navigation is easy, but I'm unsure on Umbraco responsive ones. Where in this code do I specify the class, or is that completely the wrong way of doing it?

  • Trine 29 posts 89 karma points
    Aug 08, 2013 @ 21:05
    Trine
    0

    Strike that last one, I've found the solution! Add

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

            <xsl:attribute name="class">current-page</xsl:attribute>

        </xsl:if> 

    under the <li> and <a> I want to have the class name "current-page".

    Got this from http://our.umbraco.org/forum/developers/xslt/24351-Navigation-Current-state-on-parent-node so I'm not taking credit for it.

    @nadine, @Chriztian and @Dennis: Thank you all so much for your help!! Kudos to all!

     

    Trine :-)

  • Trine 29 posts 89 karma points
    Aug 11, 2013 @ 12:19
    Trine
    0

    I know I posted this as solved, but since then the client has added another level to the page structure and now the solution doesn't work!

    @nadine: Having some trouble with the second code you offered up. The first one worked perfectly, but the client has since decided to add another level in and that meant that on the pages I need it it's only showing the parent page.

    The second code only seems to show either the current page, not the parent page, current page and sibling page.

    So you can see the levels I'm dealing with, this is the full breadcrumb for the pages I need this code to work on: Home - Branches - County - Branch - Team. The code needs to work on the last level and I can't get it to work!

    Is there any way of getting the first code to work on a level this deep?

Please Sign in or register to post replies

Write your reply to:

Draft