Copied to clipboard

Flag this post as spam?

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


  • Daniel Wainwright 2 posts 32 karma points
    Jul 22, 2013 @ 12:13
    Daniel Wainwright
    0

    Xslt Menu: Remove .aspx from Hyperlinks

    Hi,

    I'm currently stuck trying to modify my websites navigation header menu using xslt through Umbraco (I'm a bit of a novice when it comes to xslt unfortunately). The menu has 2 levels and functions correctly but I would like to remove the extension of the URLs so that it displays SEO friendly URLs e.g. change http://www.home.com/about-us.aspx to http://www.home.com/about-us

    The current code is below:

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

    <xsl:variable name="level" select="1"/>

    <xsl:template match="/">

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

         <xsl:if test="count(current()/* [@isDoc]) > 0">
     <ul>
            <xsl:for-each select="current()/* [@isDoc and string(umbracoNaviHide) != '1']">
              <li>
                  <a href="{umbraco.library:NiceUrl(@id)}" class="sf-with-ul">
                    <xsl:value-of select="@nodeName" />
                </a>
              </li>
            </xsl:for-each>
          </ul>
    </xsl:if>

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

    </xsl:template>

    </xsl:stylesheet>

    I have managed to remove the extensions using the following code (however this has led to a bug within the menu):

     <xsl:if test="count(current()/* [@isDoc]) > 0">
     <ul>
            <xsl:for-each select="current()/* [@isDoc and string(umbracoNaviHide) != '1']">
              <li>
                  <a href="{umbraco.library:Replace(umbraco.library:NiceUrl(@id),'.aspx','')}" class="sf-with-ul">
                    <xsl:value-of select="@nodeName" />
                </a>
              </li>
            </xsl:for-each>
          </ul>
    </xsl:if>

    Basically I think the above code somehow confuses the @isDoc and string(umbracoNaviHide) != '1' check as on the second level of the menu unwanted pages appear (basically any page within the Home folder in the Content tab of Umbraco). For example the correct menu structure would be:

    Home: About, Contact

    However with the code implemented that replaces '.aspx' the following menu structure is displayed:

    Home: About, Contact, Page Not Found, pdfviewer

    If anyone know of a solution on how to remove the URL extension and retain the correct menu structure then any help would be very much appreciated!

    Thanks,

    Daniel

     

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jul 22, 2013 @ 12:22
    Chriztian Steinmeier
    100

    Hi Daniel,

    The extensions are handled separately from XSLT - they're handled within the NiceUrl() method based on a setting in web.config:

    In the <appSettings> section there's a key called umbracoUseDirectoryUrls - set that to true, like this:

    <add key="umbracoUseDirectoryUrls" value="true" />

    ... and republish your site (right-click the Content node). Then that should be it.

    /Chriztian

  • Daniel Wainwright 2 posts 32 karma points
    Jul 23, 2013 @ 10:19
    Daniel Wainwright
    0

    Ok that's great! I don't currently have access to the web.config at the moment but should hopefully be gaining access over the next couple of days so will let you know if the umbracoUseDirectoryUrls works.

    Just out of curiosity when using the umbraco.library:Replace command to remove '.aspx' (code below) from a URL, is the reason every node suddenly appears in the menu due to the fact the @isDoc check is no longer valid?

    <xsl:iftest="count(current()/* [@isDoc]) > 0">
     <ul>
            <xsl:for-eachselect="current()/* [@isDoc and string(umbracoNaviHide) != '1']">
              <li>
                  <ahref="{umbraco.library:Replace(umbraco.library:NiceUrl(@id),'.aspx','')}"class="sf-with-ul">
                    <xsl:value-ofselect="@nodeName"/>
                </a>
              </li>
            </xsl:for-each>
          </ul>
    </xsl:if>

    Thank you for your help.

    Daniel

Please Sign in or register to post replies

Write your reply to:

Draft