Copied to clipboard

Flag this post as spam?

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


  • MartinB 411 posts 512 karma points
    May 16, 2010 @ 02:33
    MartinB
    0

    New to Xslt - Menu only a tags

    Hi there

    I'm building a site where i need to have menu items that are not clickable but still contains children.

    So far i found and edited the following xslt:

    As you can see i just removed the a tag for the ul "menu" which only contains 1 link and that works great, but it doesn't help me further down the road in levels >1

    Any suggestions? Thanks in advance

    <?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 -->
    <!-- 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="1"/>

    <xsl:template match="/">

    <!-- The fun starts here -->
    <ul id="menu">
    <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']">
    <li>

    <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <!-- we're under the item - you can do your own styling here -->
    <xsl:attribute name="class">current</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="@nodeName"/>

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

    </xsl:template>

    </xsl:stylesheet>

  • MartinB 411 posts 512 karma points
    May 16, 2010 @ 02:34
    MartinB
    0

    Oh i forgot to mention that 3rd level links are handled by another xslt

  • Sebastiaan Janssen 4899 posts 14655 karma points MVP admin hq
    May 16, 2010 @ 17:47
    Sebastiaan Janssen
    0

    You could do something in an xsl:choose, like so:

    <xsl:choose>
     <xsl:when test="level() &gt; 2">
      <li>
       <!-- you menu code here without a link -->
      </li>
     </xsl:when>
     <xsl:otherwise>
      <li>
       <a href={umbraco.library:NiceUrl(@id)}>
        <!-- your other menu code -->
       </a>
      </li>
     </xsl:otherwise>
    </xsl:choose>

    You can do multiple xsl:when's if you want.

  • MartinB 411 posts 512 karma points
    May 17, 2010 @ 18:28
    MartinB
    0

    Hi Sebastiaan

    Thanks for the reply.

    As i've worked a bit more with the site and Umbraco i've got a working menu with dropdown features from jQuery.

    What you suggest is a nice solution, but actually i need to test wether a page has childnodes or not. It's only then that i dont want the parent to be an active link.

    How would that look if i had to put it into the following xslt?

    <?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 -->
    <!-- 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="1"/>

    <xsl:template match="/">
    <!-- The fun starts here -->
    <ul id="menu" class="topMenu">
    <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']">
    <li>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <!-- we're under the item - you can do your own styling here -->
    <xsl:attribute name="class">current</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="@nodeName"/>
    </a>
    <!--If a node has children, show them on hover-->
    <xsl:if test="($currentPage/@id and count(current()/node) > 0) or ($currentPage/@parentID = ./@id)">
    <ul id="submenu" class="topmenuSub">
    <xsl:for-each select="current()/node">
    <li>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <!-- we're under the item - you can do your own styling here -->
    <xsl:attribute name="class">current</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="@nodeName"/>
    </a>
    </li>
    </xsl:for-each>
    </ul>
    </xsl:if>
    </li>
    </xsl:for-each>
    </ul>

    </xsl:template>

    </xsl:stylesheet>
  • MartinB 411 posts 512 karma points
    May 17, 2010 @ 18:30
    MartinB
    0

    Obviously it seems like some of this can be used, i just dont know much about xslt yet:

     <!--If a node has children, show them on hover-->
             
    <xsl:if test="($currentPage/@id and count(current()/node) > 0) or ($currentPage/@parentID = ./@id)">
  • MartinB 411 posts 512 karma points
    May 18, 2010 @ 22:49
    MartinB
    0

    Anyone who has a suggestion about this?

  • Rich Green 2246 posts 4006 karma points
    May 18, 2010 @ 23:06
    Rich Green
    0

    Hey Martin,

    Try something like this

     

    <!--If a node has children, show them on hover-->
    <xsl:if test="(count(./node) &gt; 0 and $currentPage/ancestor-or-self::node/@id = current()/@id)">
    
        This is the current node and it has children
    
    </xsl:if>

     

    Rich

  • Tom Fulton 2030 posts 4996 karma points c-trib
    May 18, 2010 @ 23:11
    Tom Fulton
    0

    You can use the count() function to check for child nodes and assign an attribute if it meets your conditions

    This should work....

              <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id and count(current()/node) = 0">
                  <!-- we're under the item - you can do your own styling here -->
                  <xsl:attribute name="class">current</xsl:attribute>
                </xsl:if>
  • MartinB 411 posts 512 karma points
    May 19, 2010 @ 21:24
    MartinB
    0

    Hi guys

    Thanks for your help, i'll give it a go in just a moment! :)

  • MartinB 411 posts 512 karma points
    May 29, 2010 @ 23:42
    MartinB
    0

    Hi again

    sorry for the late reply. I've been busy setting up content :-)

    Though both your suggestions are valid, i came up with the following, for full control:

    <xsl:otherwise>
    <xsl:choose>
    <xsl:when test="./data [@alias='menuOnly'] = 1">
    <a>
    <xsl:value-of select="@nodeName"/>
    </a>
    </xsl:when>

    <xsl:otherwise>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <!-- we're under the item - you can do your own styling here -->
    <xsl:attribute name="class">current</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="@nodeName"/>
    </a>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:otherwise>
  • MartinB 411 posts 512 karma points
    May 29, 2010 @ 23:42
    MartinB
    0

    And the full xslt (if someone could benefit from it)

    <?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 -->
    <!-- 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="1"/>

    <xsl:template match="/">

    <!-- The fun starts here -->
    <ul id="menu">
    <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']">
    <li>

    <xsl:choose>
    <xsl:when test="./data [@alias='externalLink'] = 1">
    <a href="{./data [@alias='link']}" target="_blank">
    <xsl:value-of select="@nodeName"/>
    </a>
    </xsl:when>

    <xsl:otherwise>
    <xsl:choose>
    <xsl:when test="./data [@alias='menuOnly'] = 1">
    <a>
    <xsl:value-of select="@nodeName"/>
    </a>
    </xsl:when>

    <xsl:otherwise>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <!-- we're under the item - you can do your own styling here -->
    <xsl:attribute name="class">current</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="@nodeName"/>
    </a>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:otherwise>
    </xsl:choose>

    <!--If a node has children, show them on hover-->
    <xsl:if test="($currentPage/@id and count(current()/node [string(data [@alias='umbracoNaviHide']) != '1']) > 0) or ($currentPage/@parentID = ./@id) ">
    <ul id="submenu">
    <xsl:for-each select="current()/node">
    <li>
    <xsl:choose>
    <xsl:when test="./data [@alias='externalLink'] = 1">
    <a href="{./data [@alias='link']}" target="_blank">
    <xsl:value-of select="@nodeName"/>
    </a>
    </xsl:when>

    <xsl:otherwise>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <!-- we're under the item - you can do your own styling here -->
    <xsl:attribute name="class">current</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="@nodeName"/>
    </a>
    </xsl:otherwise>
    </xsl:choose>
    </li>
    </xsl:for-each>
    </ul>
    </xsl:if>
    </li>
    </xsl:for-each>
    </ul>

    </xsl:template>

    </xsl:stylesheet>

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

Please Sign in or register to post replies