Copied to clipboard

Flag this post as spam?

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


  • Uros Sankovic 107 posts 134 karma points
    Oct 01, 2011 @ 22:07
    Uros Sankovic
    0

    If parent node has a certain name, then display all child nodes...

    Hi,

    please help me with XSLT. I want that if a node on level 1 has a ceratin name (let say "CASE"), then I want that to display all node names on level 2 of that node "CASE".

    THX for your help

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 01, 2011 @ 22:15
    Chriztian Steinmeier
    0

    Hi Uros,

    You can do that in many ways, but here's one:

    <xsl:template match="/">
        <xsl:apply-templates select="$currentPage[@level = 1][@nodeName = 'CASE']/*[@isDoc][not(umbracoNaviHide = 1)]" />
    </xsl:template>
    
    <!-- Generic display template - prints the name of the page -->
    <xsl:template match="*[@isDoc]">
        <p>
            <xsl:value-of select="@nodeName" />
        </p>
    </xsl:template>

    /Chriztian

  • Uros Sankovic 107 posts 134 karma points
    Oct 02, 2011 @ 07:53
    Uros Sankovic
    0

    Hi, Thx for your help but I can't get it working. What I'm trying to do is if a parent node has a ceratin name,then display all node names of the second level. Here's the code so far. Let me point out that I'm very new to XSLT.

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

        <xsl:template match="/">
        <!-- The fun starts here -->
        <xsl:for-each select="$currentPage/ancestor-or-self::* [@level = $level]/* [string(mainNavHide) != '1' and @isDoc]">
          <xsl:choose>
            <xsl:when test="$currentPage/parent::* [@level = 1]/* [@nodeName != 'CASE' and @isDoc]">
            </xsl:when>
    <xsl:otherwise>
            
              <li>
                <href="{umbraco.library:NiceUrl(@id)}">
                  <xsl:value-of select="@nodeName"/>
                </a>
                <xsl:if test="(count(current()/* [string(mainNavHide) != '1' and  @isDoc]) > 0)">
                 <ul>
                           <xsl:for-each select="current()/* [@isDoc]">
                      <li>
                     <href="{umbraco.library:NiceUrl(@id)}">
                               <xsl:value-of select="@nodeName"/></a>
                      </li>
                    </xsl:for-each>
                  </ul>
                </xsl:if>
              </li>
            </xsl:otherwise>
              
          </xsl:choose>
        </xsl:for-each>
      </xsl:template>
     </xsl:stylesheet>

    THX. Uros

  • Uros Sankovic 107 posts 134 karma points
    Oct 02, 2011 @ 09:10
    Uros Sankovic
    0

    Ok, here's the code which seems to do the work. Would you be so kind and let me know, if this could be done better?

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

        <xsl:template match="/">
        <!-- The fun starts here -->
        <xsl:for-each select="$currentPage/ancestor-or-self::* [@level = $level]/* [string(mainNavHide) != '1' and @nodeName = 'Dermatologija' and @isDoc]">
          
            <xsl:for-each select="$currentPage/ancestor-or-self::* [@level = 2]/* [@isDoc]">
              
                <href="{umbraco.library:NiceUrl(@id)}">
                  <xsl:value-of select="@nodeName"/>
                </a>
                <xsl:if test="(count(current()/* [string(mainNavHide) != '1' and  @isDoc]) > 0)">
                 <ul>
                           <xsl:for-each select="current()/* [@isDoc]">
                      <li>
                     <href="{umbraco.library:NiceUrl(@id)}">
                               <xsl:value-of select="@nodeName"/></a>
                      </li>
                    </xsl:for-each>
                  </ul>
                </xsl:if>
              
            
              
             </xsl:for-each>
              
        </xsl:for-each>

     

    THX for your help.

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 02, 2011 @ 22:37
    Chriztian Steinmeier
    0

    Hi Uros,

    You're welcome - since you asked, here goes some refactoring:

    1. The outermost for-each is really redundant – you're only "iterating" a single node, so you should incorporate that requirement in the main selection (see #2).

    2. To make it easier to understand what's going on, set a variable to the starting point of your nav.  

    3. The generated links are strong contenders for using a separate template to remove duplication - substitute an apply-templates instruction in place of the <a href=""> element.

    4. You don't need to count to check if a node has subnodes, simply selecting them will yield the desired boolean result. Also: If you're testing for something before doing stuff, the "doing" and "test" expressions should be the same to eliminate surprises in the result (your test checks for the mainNavHide property, but the for-each doesn't use it, so in effect a single unhidden node will trigger the display of all its hidden siblings).

    5. The current() function is unnecessary at the beginning of an expression, since an expression is evaluated starting from the current context node.

    <xsl:variable name="navRoot" select="$currentPage/ancestor-or-self::*[@level = 2][../@nodeName = 'Dermatologija']" /><!-- [2] -->
    
    <xsl:for-each select="$navRoot/*[@isDoc]"><!-- [2] -->
    
        <xsl:apply-templates select="." /><!-- [3] -->
    
        <xsl:if test="*[@isDoc][not(mainNavHide = 1)]"><!-- [4] -->
            <ul>
                <xsl:for-each select="*[@isDoc][not(mainNavHide = 1)]"><!-- [5] -->
                    <li>
                        <xsl:apply-templates select="." /><!-- [3] -->
                    </li>
                </xsl:for-each>
            </ul>
        </xsl:if>
    </xsl:for-each>   
    

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft