Copied to clipboard

Flag this post as spam?

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


  • Graeme Paul 44 posts 64 karma points
    Feb 01, 2011 @ 11:52
    Graeme Paul
    0

    Xslt - Newbie help with active class and listing level nodes

    I was wondering if anyone could help me, Im pretty new to umbraco and Xslt.

    ive been trying to unsuccessfully create a left hand nav for our website. However I cannot seem to get all of it working.

    I am trying to get the nav to show the selected level 2 node page and then the selected level 3 node aswell when active.

    -Homepage
    --House
    ---windows
    ---doors

    Below is an html version of what im trying to achieve

    <ul class="left_nav">
                <li><a href="#" class="selected">House</a>
                    <ul class="sub_left_nav">
                        <li><a href="#" class="selected">- Windows</a></li>
                        <li><a href="#">- Doors</a></li>
                <li><a href="#">- Bricks</a></li>
                    </ul>
                </li>
            </ul>

    Below is my xslt which only shows the level 3 nodes.

    <?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 -->
    <xsl:variable name="level" select="2"/>
    <xsl:template match="/">
    <!-- The fun starts here -->
    <ul class="left_nav">
        <li><a class="selected" href="#">House</a>
    <ul class="sub_left_nav">
    <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
       <li>  
        <a href="{umbraco.library:NiceUrl(@id)}" >
          <xsl:value-of select="@nodeName"/>
        </a>
      </li>
    </xsl:for-each>
    </ul>
        </li>
        </ul>
    </xsl:template>
    </xsl:stylesheet>
  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 01, 2011 @ 11:59
    Jan Skovgaard
    0

    Hi Graeme and welcome to our and the joyfull world of Umbraco development :)

    I think your sample looks very identical to what you get from the "navigation prototype", which is a predefined XSLT snippet that you can use as a starting point for your navigation.

    Therefore I think you actually just need to go to the developer section, right click "XSLT" and create a new XSLT macro and choose the "navigation prototype" from the dropdown that appears.

    There you will notice that there this section beneath the opening <li> element

                <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>

    This is where the magic of adding the active class happens.

    Hope this helps, otherwise don't hesitate to ask :)

    /Jan

  • Graeme Paul 44 posts 64 karma points
    Feb 01, 2011 @ 12:28
    Graeme Paul
    0

    Thanks for the quick reply Jan, I have got the selected to work on the level 3 nodes :)

    I just need to get the level 2 bit working so that it shows the parent node and adds the selected class ( where i have the link with house)?

    thanks again for the getting back to me so quickly :)

    <ul class="left_nav">
      <li>
        <a class="selected" href="#"> house</a>
           
    <
    ul class="sub_left_nav">
              <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
                  <li>    
                      <a href="{umbraco.library:NiceUrl(@id)}" >
                        <xsl:if test="$currentPage/ancestor-or-self::*/@id = current()/@id">
                          <xsl:attribute name="class">selected</xsl:attribute>
                        </xsl:if>
                        <xsl:value-of select="@nodeName"/>
                      </a>
                  </li>
              </xsl:for-each>
            </ul>
      </li>
    </ul>
  • Graeme Paul 44 posts 64 karma points
    Feb 02, 2011 @ 16:28
    Graeme Paul
    0

    ive been playing arround with it a bit more and still cannot get the parent node to appear, and i get errors with the following?

    <ul class="left_nav">
     <li>
          <a href="{umbraco.library:NiceUrl($currentPage/parent::node/@id)}">
            <xsl:attribute name="class">selected</xsl:attribute>
            <xsl:value-of select="$currentPage/parent::node/@nodeName"/>
            </a>
    <ul class="sub_left_nav">
    <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
                  <li>   
                      <a href="{umbraco.library:NiceUrl(@id)}" >
                        <xsl:if test="$currentPage/ancestor-or-self::*/@id = current()/@id">
                          <xsl:attribute name="class">selected</xsl:attribute>
                        </xsl:if>
                        - <xsl:value-of select="@nodeName"/>
                      </a>
                  </li>
              </xsl:for-each>
            </ul>
      </li>
    </ul>

     

    Is there any recommended reading material or sources for xslt? I have to learn it pretty quickly.

    Thanks

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 02, 2011 @ 19:55
    Jan Skovgaard
    0

    Hi Graeme

    Not sure what it is you've got going on in the above...what kind of errors do you receive?

    /Jan

  • Graeme Paul 44 posts 64 karma points
    Feb 03, 2011 @ 09:52
    Graeme Paul
    0

    Jan, im trying to show 2 levels, my level 2 parent node, and the level 3 nodes that are allocated to it.

    To call the level 3 nodes the below works fine

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

     

    But then before the "for each" im trying to display a link to the parent node, is this where im going wrong?

    <ul class="left_nav">
     <li>
    <a href="{umbraco.library:NiceUrl($currentPage/parent::node/@id)}">
          <xsl:value-of select="$currentPage/parent::node/@nodeName"/>
         
    </a>
    <ul class="sub_left_nav">
    <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
             <li>    
                 <a href="{umbraco.library:NiceUrl(@id)}" >
                 <xsl:if test="$currentPage/ancestor-or-self::*/@id = current()/@id">
                <xsl:attribute name="class">selected</xsl:attribute>
                </xsl:if>
                    - <xsl:value-of select="@nodeName"/>
                 </a>
                  </li>
              </xsl:for-each>
            </ul>
      </li>
    </ul>

     

    Jan the error im receiving is

    System.OverflowException: Value was either too large or too small for an Int32.
    at System.Convert.ToInt32(Double value)

    Thanks

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 03, 2011 @ 10:10
    Jan Skovgaard
    0

    Hi Graeme

    Does it work if you check "skip error testing" when you save the XSLT?

    Sometimes the passed id's is not known on the runtime of the XSLT, which is why you get an error probably.

    /Jan

  • Sam 184 posts 209 karma points
    Feb 03, 2011 @ 10:32
    Sam
    0

    Hi Graeme,

    Isn't this the old xml schema?

         <a href="{umbraco.library:NiceUrl($currentPage/parent::node/@id)}">
            <xsl:value-of select="$currentPage/parent::node/@nodeName"/>
         
    </a>

    Maybe you're mixing new with old?

    Could try this:

     <a href="{umbraco.library:NiceUrl($currentPage/parent::* /@id)}">
            <xsl:value-of select="$currentPage/parent::* /@nodeName"/>
         
    </a>

    Sam.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 03, 2011 @ 10:36
    Jan Skovgaard
    0

    Hi Sam

    If it was the old schema some of the previous code would not have been working, which it apparently has :-)

    The above error is something that many people gets confused about - and with good reason.

    Cheers

    /Jan

  • Graeme Paul 44 posts 64 karma points
    Feb 03, 2011 @ 10:38
    Graeme Paul
    0

    Thanks Sam and Jan I got it working, I must have been mixing old and new schemas.

    Thanks again :)

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 03, 2011 @ 10:40
    Jan Skovgaard
    0

    Hi Graeme

    Glad you got it working - I must admit I'm a bit embarresed. Sam is totally right...I'll remember to wear my glasses next time I answer a post with code samples, sorry Sam ;-)

    /Jan

  • Sam 184 posts 209 karma points
    Feb 03, 2011 @ 11:10
    Sam
    0

    lol. I'm just pleased that my learning has finally paid off a bit with being able to help out :)

    Sam.

  • Graeme Paul 44 posts 64 karma points
    Feb 07, 2011 @ 16:40
    Graeme Paul
    0

    -Homepage
    --House
    ---windows
    ---doors

    Ive just been testing my nav again, the nav is great for the level 3 nav, but when i click on the level 2 it shows the parent which is home.Im trying to write a if statement to say, if on level 2 then show current nodeName? is this possible?

     

       <xsl:if test="$currentPage = @level=$level2">
      <a class="selected" href="{umbraco.library:NiceUrl(@id)}">
          <xsl:value-of select="@nodeName"/>
         </a>
    </xsl:if>   
       <a class="selected" href="{umbraco.library:NiceUrl($currentPage/parent::* /@id)}">
            <xsl:value-of select="$currentPage/parent::* /@nodeName"/>
         </a>

     

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 07, 2011 @ 23:00
    Jan Skovgaard
    0

    Hi Graeme..

    That should be possible but perhaps it's better to make use of the @path attribute?

    it conatins the @path id's listing them as "-1,1100,1304,1540" for instance. The position of the parent id is always the same in this sequence, so by using umbraco.library:Split() on this value you should be able to get the value of the parent id, which is always the same in the same spot no matter how far down you are in the structure.

    So maybe it would be easier to make a comparison on this?

    Does it make sense?

    /Jan

  • Graeme Paul 44 posts 64 karma points
    Feb 08, 2011 @ 11:23
    Graeme Paul
    0

    yes it does Jan, do you have any examples of @path in use?

  • Graeme Paul 44 posts 64 karma points
    Feb 08, 2011 @ 17:00
    Graeme Paul
    0

    Jan ive got it working, it may not be the best way of doing it but its working :) Thanks again!! :)

     

    <xsl:choose>
    <xsl:when test="$currentPage [string(@level) = '2']">
    <a class="selected" href="{umbraco.library:NiceUrl($currentPage/@id)}">
    <xsl:value-of select="$currentPage/@nodeName"/>
    a>
    xsl:when>
    <xsl:otherwise>
    <a class="selected" href="{umbraco.library:NiceUrl($currentPage/parent::* /@id)}">
    <xsl:value-of select="$currentPage/parent::* /@nodeName"/>
    a>
    xsl:otherwise>
    xsl:choose>
Please Sign in or register to post replies

Write your reply to:

Draft