Copied to clipboard

Flag this post as spam?

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


  • Matt 15 posts 45 karma points
    Oct 20, 2010 @ 17:53
    Matt
    0

    Help converting 4.0 left-navigation xslt to 4.5 schema

    Hello,

    Been banging my head on the wall and thought I'd see if anyone could provide a little hand-holding.  I've gotten it to partially work, but only two levels deep.  I'm sure I'm missing something simple, but I'm still learning XSLT, so the solution is evading me.

    Here's the working 4.0 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"/>

    <!-- update this variable to set how deep the navigation should go -->
    <xsl:variable name="maxLevelForNav" select="6"/>

    <xsl:template match="/">
    <ul><li class="selected">
    <xsl:if test="$currentPage [string(@level) = '2']">
    <h4><a href="{umbraco.library:NiceUrl($currentPage/@id)}"><xsl:value-of select="$currentPage/@nodeName"/></a></h4>
    <xsl:call-template name="drawNodes">
    <xsl:with-param name="parent" select="$currentPage"/>
    </xsl:call-template>
    </xsl:if>
    <xsl:if test="$currentPage [string(@level) = '3']">
    <h4><a href="{umbraco.library:NiceUrl($currentPage/../@id)}"><xsl:value-of select="$currentPage/../@nodeName"/></a></h4>
    <xsl:call-template name="drawNodes">
    <xsl:with-param name="parent" select="$currentPage/parent::node"/>
    </xsl:call-template>
    </xsl:if>
    <xsl:if test="$currentPage [string(@level) = '4']">
    <h4><a href="{umbraco.library:NiceUrl($currentPage/../../@id)}"><xsl:value-of select="$currentPage/../../@nodeName"/></a></h4>
    <xsl:call-template name="drawNodes">
    <xsl:with-param name="parent" select="$currentPage/parent::node/parent::node"/>
    </xsl:call-template>
    </xsl:if>
    <xsl:if test="$currentPage [string(@level) = '5']">
    <h4><a href="{umbraco.library:NiceUrl($currentPage/../../../@id)}"><xsl:value-of select="$currentPage/../../../@nodeName"/></a></h4>
    <xsl:call-template name="drawNodes">
    <xsl:with-param name="parent" select="$currentPage/parent::node/parent::node/parent::node"/>
    </xsl:call-template>
    </xsl:if>
    </li></ul>
    </xsl:template>

    <xsl:template name="drawNodes">
    <xsl:param name="parent"/>
    <xsl:if test="umbraco.library:IsProtected($parent/@id, $parent/@path) = 0 or (umbraco.library:IsProtected($parent/@id, $parent/@path) = 1 and umbraco.library:IsLoggedOn() = 1)">
    <ul class="lowest">
    <xsl:for-each select="$parent/node [string(./data [@alias='umbracoNaviHide']) != '1' and @level &lt;= $maxLevelForNav and @nodeTypeAlias!='News Item' and @nodeTypeAlias!='News Listing']">
    <li>
    <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <xsl:attribute name="class">selected</xsl:attribute>
    </xsl:if>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <xsl:attribute name="class">current</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="@nodeName"/></a>
    <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <xsl:if test="count(./node [string(./data [@alias='umbracoNaviHide']) != '1' and @nodeTypeAlias!='QBE News Item' and @level &lt;= $maxLevelForNav]) &gt; 0">
    <xsl:call-template name="drawNodes">
    <xsl:with-param name="parent" select="."/>
    </xsl:call-template>
    </xsl:if>
    </xsl:if>
    </li>
    </xsl:for-each>
    </ul>
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>

    And here's what I have so far for 4.5:

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

    <!-- update this variable to set how deep the navigation should go -->
    <xsl:variable name="maxLevelForNav" select="6"/>

    <xsl:template match="/">
    <ul><li class="selected">
        <xsl:if test="$currentPage [string(@level) = '2']">
            <h4><a href="{umbraco.library:NiceUrl($currentPage/@id)}"><xsl:value-of select="$currentPage/@nodeName"/></a></h4>
            <xsl:call-template name="drawNodes">
                    <xsl:with-param name="parent" select="$currentPage"/>
            </xsl:call-template>
        </xsl:if>
        <xsl:if test="$currentPage [string(@level) = '3']">
            <h4><a href="{umbraco.library:NiceUrl($currentPage/../@id)}"><xsl:value-of select="$currentPage/../@nodeName"/></a></h4>
            <xsl:call-template name="drawNodes">
                    <xsl:with-param name="parent" select="$currentPage/parent::*[@isDoc]"/>
            </xsl:call-template>
        </xsl:if>
        <xsl:if test="$currentPage [string(@level) = '4']">
            <h4><a href="{umbraco.library:NiceUrl($currentPage/../../@id)}"><xsl:value-of select="$currentPage/../../@nodeName"/></a></h4>
            <xsl:call-template name="drawNodes">
                    <xsl:with-param name="parent" select="$currentPage/parent::*[@isDoc]/parent::*[@isDoc]"/>
            </xsl:call-template>
        </xsl:if>
        <xsl:if test="$currentPage [string(@level) = '5']">
            <h4><a href="{umbraco.library:NiceUrl($currentPage/../../../@id)}"><xsl:value-of select="$currentPage/../../../@nodeName"/></a></h4>
            <xsl:call-template name="drawNodes">
                    <xsl:with-param name="parent" select="$currentPage/parent::*[@isDoc]/parent::*[@isDoc]/parent::*[@isDoc]"/>
            </xsl:call-template>
        </xsl:if>
    </li></ul>
    </xsl:template>

    <xsl:template name="drawNodes">
        <xsl:param name="parent"/>
        <xsl:if test="umbraco.library:IsProtected($parent/@id, $parent/@path) = 0 or (umbraco.library:IsProtected($parent/@id, $parent/@path) = 1 and umbraco.library:IsLoggedOn() = 1)">
            <ul class="lowest">
                <xsl:for-each select="$parent/* [@isDoc][string(./umbracoNaviHide) != '1']">
                    <li>
                        <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
                            <xsl:attribute name="class">selected</xsl:attribute>
                        </xsl:if>
                        <a href="{umbraco.library:NiceUrl(@id)}">
                        <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
                            <xsl:attribute name="class">current</xsl:attribute>
                        </xsl:if>
                        <xsl:value-of select="@nodeName"/></a>
                        <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
                          <xsl:if test="count(./node [string(./data [@alias='umbracoNaviHide']) != '1' and not(self::newsItem) and not(self::newsListing) and @level &lt;= $maxLevelForNav]) &gt; 0">
                            <xsl:call-template name="drawNodes">   
                              <xsl:with-param name="parent" select="."/>   
                            </xsl:call-template>
                          </xsl:if>
                        </xsl:if>
                    </li>
                </xsl:for-each>
            </ul>
        </xsl:if>
    </xsl:template>
    </xsl:stylesheet>
  • Lachlann 344 posts 626 karma points
    Oct 20, 2010 @ 18:22
    Lachlann
    0

    Hey Matt,

    When I create left navigation I tend to find the root node for section and then traverse from that with nested template calls:

        <xsl:template match="/">
    <!-- the root page for this traversal -->
    <xsl:variable name="rootPage" select="$currentPage/ancestor-or-self::*[@level=2]"/>
    <ul>
    <li>
    <xsl:attribute name="class">
    <xsl:if test="$currentPage/@id = $rootPage/@id">
    <xsl:text>active</xsl:text>
    </xsl:if>
    </xsl:attribute>
    <a href="{umbraco.library:NiceUrl($rootPage/@id)}">
    <xsl:value-of select="$rootPage/@nodeName" />
    </a>
    </li>

    <xsl:call-template name="listMenuItems">
    <xsl:with-param name="node" select="$rootPage/descendant::*" />
    <xsl:with-param name="nodeLevel" select="$rootPage/descendant::*/@level" />
    </xsl:call-template>
    </ul>

    </xsl:template>

    <xsl:template name="listMenuItems">
    <xsl:param name="node" />
    <xsl:param name="nodeLevel" />

    <xsl:for-each select="$node">
    <xsl:if test="(number(./@level) = number($nodeLevel)) and (./hideInAllNavigation!= 1)">
    <li>
    <xsl:attribute name="class">
    <xsl:if test="$currentPage/@id = current()/@id">
    <xsl:text>active </xsl:text>
    </xsl:if>
    </xsl:attribute>
    <a href="{umbraco.library:NiceUrl(./@id)}">
    <xsl:value-of select="./@nodeName" />
    </a>
    <xsl:if test="(count($currentPage/descendant::*[@level = number($nodeLevel+1) ]) &gt; 0) and $currentPage/@level &gt; 2 or ($currentPage/ancestor-or-self::*[@level=number($nodeLevel+1)])">
    <ul>
    <xsl:call-template name="listMenuItems">
    <xsl:with-param name="node" select="./descendant::*[@level = number($nodeLevel+1)] " />
    <xsl:with-param name="nodeLevel" select="number($nodeLevel+1)" />
    </xsl:call-template>
    </ul>
    </xsl:if>
    </li>
    </xsl:if>
    </xsl:for-each>

    </xsl:template>

     

     

    The code above basically finds the parent for this section outputs that parent node as the first item in the list and then calls a template which loops through the descendants of each node.

     

    I hope this helps

     

    L

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Oct 20, 2010 @ 18:32
    Dirk De Grave
    0

    Guess there's a test that'll fail in the new schema

     <xsl:if test="count(./node
     [string(./data [@alias='umbracoNaviHide']) != '1' and
    not(self::newsItem) and not(self::newsListing) and @level &lt;=
    $maxLevelForNav]) &gt; 0"
    >

    still using the old syntax for checking the property umbracoNaviHide, should be

     <xsl:if test="count(./node
     [string(./umbracoNaviHide) != '1' and
    not(self::newsItem) and not(self::newsListing) and @level &lt;=
    $maxLevelForNav]) &gt; 0"
    >

    Hope this helps.

     

    Regards,

    /Dirk

  • Matt 15 posts 45 karma points
    Oct 20, 2010 @ 23:49
    Matt
    0

    Thanks for the comments, all.  Had to make one change in addition to Dirk's change above, but all is good now!

     

     <xsl:if test="count(./*[@isDoc]
     [string(./umbracoNaviHide) != '1' and
    not(self::newsItem) and not(self::newsListing) and @level &lt;=
    $maxLevelForNav]) &gt; 0"
    >

Please Sign in or register to post replies

Write your reply to:

Draft