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".
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.
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.
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
Hi Uros,
You can do that in many ways, but here's one:
/Chriztian
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>
<a 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>
<a 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
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]">
<a 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>
<a 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.
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.
/Chriztian
is working on a reply...