Copied to clipboard

Flag this post as spam?

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


  • ruben 8 posts 28 karma points
    Apr 12, 2010 @ 12:05
    ruben
    0

    sitemap nested if problem

    I'm stil a newbie at Umbraco and XSLT so please forgive me if this is a dumb question. I'm using the following code in my xslt to create a sitemap:

    <xsl:variable name="maxLevelForSitemap" select="4"/>
    <xsl:variable name="startLevel" select="2"/>
    
    <xsl:template match="/">
        <div id="sitemap"> 
            <xsl:call-template name="drawNodes">  
                <xsl:with-param name="parent" select="$currentPage/ancestor-or-self::node [@level=1]"/>  
            </xsl:call-template>
        </div>
    </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>
                <xsl:for-each select="$parent/node [string(./data [@alias='umbracoNaviHide']) != '1' and @level &lt;= $maxLevelForSitemap]">                              
                <li>
                        <xsl:if test="current()/@level > $startLevel ">                                                
                                <a href="{umbraco.library:NiceUrl(@id)}">
                                    <xsl:value-of select="@nodeName"/>
                                </a>                                                  
                        </xsl:if>
                        <xsl:if test="count(./node [string(./data [@alias='umbracoNaviHide']) != '1'
                                    and @level &lt;= $maxLevelForSitemap]) &gt; 0   ">   
                            <xsl:call-template name="drawNodes">    
                                <xsl:with-param name="parent" select="."/>    
                            </xsl:call-template>  
                        </xsl:if>
                </li>
                    </xsl:for-each>
                </ul>
            </xsl:if>
    </xsl:template>

    The problem I have now is that the li is also shown for the root item which i obviously don't want.

    I tried to correct this by putting the <li> tag inside the if that tests the current level. but then I get the error that the <li> doesn't have a closing tag (quite normal of course). so I decided to put the <li>, </li> and the following if that tests for children inside the if that tests the level.

    like this:

    <xsl:if test="current()/@level > $startLevel ">       
        <li>                                      
            <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName"/>
            </a>                                                  
    
            <xsl:if test="count(./node [string(./data [@alias='umbracoNaviHide']) != '1'
                and @level &lt;= $maxLevelForSitemap]) &gt; 0   ">   
                <xsl:call-template name="drawNodes">    
                    <xsl:with-param name="parent" select="."/>    
                </xsl:call-template>  
            </xsl:if> 
        </li>
    </xsl:if>

    but this doesn't work, which is quite a mystery to me.

    So I need a way to add logic around the <li> tag to decide wether or not to show it. which brings me back to the error I get when the closing </li> tag is not in the same if. I tried to show <li> like this &lt;li&gt; and this <![CDATA<li>]]>, which also did not result in the wanted result.

    I hope I explained the problem correctly. any help would be greatly apreciated.

  • Tommy Poulsen 514 posts 708 karma points
    Apr 12, 2010 @ 12:40
    Tommy Poulsen
    0

    Hi Ruben

    How about moving your

    current()/@level > $startLevel

    test to e.g. the for-each selection instead - then you only iterate the nodes below your start level.

    >Tommy

     

  • Seth Niemuth 275 posts 397 karma points
    Apr 12, 2010 @ 12:43
    Seth Niemuth
    0

    I am not exactly sure what you want inside your topLevel and what you don't but you could use a choose statement:

     

                            <xsl:for-each select="$parent/node [string(./data [@alias='umbracoNaviHide']) != '1' and @level &lt;= $maxLevelForSitemap]">                                                            

                             <xsl:choose>

    <xsl:when test="current()/@level > $startLevel >

    <li>

     <a href="{umbraco.library:NiceUrl(@id)}">

                                                                    <xsl:value-of select="@nodeName"/>

                                                            </a>  

        <xsl:if test="count(./node [string(./data [@alias='umbracoNaviHide']) != '1'

                                                                    and @level &lt;= $maxLevelForSitemap]) &gt; 0   ">   

                                                    <xsl:call-template name="drawNodes">    

                                                            <xsl:with-param name="parent" select="."/>    

                                                    </xsl:call-template>  

                                            </xsl:if>

     </li>

    </xsl:when>

    <xsl:otherwise>

        <xsl:if test="count(./node [string(./data [@alias='umbracoNaviHide']) != '1'

                                                                    and @level &lt;= $maxLevelForSitemap]) &gt; 0   ">   

                                                    <xsl:call-template name="drawNodes">    

                                                            <xsl:with-param name="parent" select="."/>    

                                                    </xsl:call-template>  

                                            </xsl:if>

    </xsl:otherwise>

    </xsl:choose>

                                    </xsl:for-each>

     

     

  • ruben 8 posts 28 karma points
    Apr 12, 2010 @ 13:13
    ruben
    0

    @tommy: that was idea too at first, but then it didn't display anything.

    @seth: that did the trick. I tried something similar with an if statement but now I see I made a logical mistake. Guess I'm not yet used to using the choose statement instead of the if statement :)

     

    thanks for the help and super fast response guys!

     

  • Seth Niemuth 275 posts 397 karma points
    Apr 12, 2010 @ 14:56
    Seth Niemuth
    0

    ruben,

    the choose statement works like the classic if... else statement.

    you first declare the choose (<xsl:choose>)

    then if some condition is true we do that thing

    <xsl:when test="isConditionTrue">

    <!-- Some things to do -->

    </xsl:when>

    You can put in as many (<xsl:when test="isConditionTrue" >) as you want.

    Then, there is a catch all (<xsl:otherwise> <!-- Do this if nothing else is true --> </xsl:otherwise>), which will run if none of the other when conditional statments are true.

    Then, close your choose (</xsl:choose>).

    It is a very useful built in tool, which allows you to add a bit more logic to your XSLT.

  • ruben 8 posts 28 karma points
    Apr 21, 2010 @ 15:54
    ruben
    0

    Thanks for the explanation Seth. Guess I should really dive into some xslt and umbraco tutorials :)

Please Sign in or register to post replies

Write your reply to:

Draft