Copied to clipboard

Flag this post as spam?

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


  • vaibhav 119 posts 139 karma points
    Mar 25, 2011 @ 07:55
    vaibhav
    0

    check if node has child nodes

    Hello all

    I want o check if node has child nodes or not ...i tried but not getting

    Here is my code...

    <?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" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary"
      exclude-result-prefixes="msxml
    umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes
    Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings
    Exslt.ExsltSets tagsLib BlogLibrary "
    >

    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <!-- The fun starts here -->
    <xsl:if test="count(./node) &gt; 0"> <!-- checking for child nodes -->
     Select <xsl:value-of select="$currentPage/@nodeName"/> :  
       <ul>
        <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']">
          <li>
            <a href="{umbraco.library:NiceUrl(@id)}">
            <xsl:value-of select="@nodeName"/>
            </a>
          </li>
        </xsl:for-each>
       </ul>
      </xsl:if>
    </xsl:template>

    </xsl:stylesheet>

    I am making some mistak ..plz solvei t ...

  • Pasang Tamang 258 posts 458 karma points
    Mar 25, 2011 @ 08:06
    Pasang Tamang
    0

    Hi,

    To check the child node you have to check counting of the child nodes. Here is sample code

    <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']">
    <xsl:variable name="child" select="umbraco.library:GetXmlNodeById(@id)/*[@isDoc]"/>
            <xsl:if test="count($child) > 0">
    <xsl:value-of select="@nodeName"/>
            </xsl:if>
    </xsl:for-each>

    Hope this helps

  • Daniel Bardi 927 posts 2562 karma points
    Mar 25, 2011 @ 08:19
    Daniel Bardi
    0
    <xsl:if test="count($currentPage/*) &gt; 0">
    
    ... do something ...
    </xsl:if>
  • Kishore kumar Kandavalli 2 posts 71 karma points
    Oct 18, 2018 @ 15:40
    Kishore kumar Kandavalli
    0

    hi daniel, can you please tell me how to display the count the nodes and its descendent nodes from xslt to html output. can you please tell me how to solve this ? thanks in advance

  • vaibhav 119 posts 139 karma points
    Mar 25, 2011 @ 08:26
    vaibhav
    0

    @ Pasang ..... i want to check whether current node has child node & if yes then it will go into for-each loop ....

    @Daniel .....I used that if statement but it is showing text even if there is no child node .....

  • Daniel Bardi 927 posts 2562 karma points
    Mar 25, 2011 @ 08:27
    Daniel Bardi
    0

    <xsl:if test="count($currentPage/self::*[@isDoc]) &gt; 0">

    ... do something ...

    </xsl:if>

  • vaibhav 119 posts 139 karma points
    Mar 25, 2011 @ 08:45
    vaibhav
    0

    I made some change to ur if statement Daniel & then it worked....

    <xsl:if test="$currentPage/* [@isDoc and string(umbracoNaviHide) != '0']">

    </xsl:if>
  • Daniel Bardi 927 posts 2562 karma points
    Mar 25, 2011 @ 08:48
    Daniel Bardi
    0

    that looks great... very late here in the US.. my brain is mush.. 

    Great job!

  • Dmitrij Jazel 86 posts 179 karma points
    Jun 22, 2012 @ 09:13
    Dmitrij Jazel
    0

    Hi guys,

    I actually just tryed to achieve the same here (check if current node I am iterating on has any children) and Vaibhavs suggestion just worked for me.

     

    <xsl:if test="current()/* [@isDoc and string(umbracoNaviHide) != '1']">    <!-- checking for child nodes  -->
                  <ul id="list-content">
                    <xsl:for-each select="current()/child::* [@isDoc and string(umbracoNaviHide) != '1']">
                      <li>
                        <a href="{umbraco.library:NiceUrl(@id)}" class="M2">
                          <xsl:value-of select="@nodeName"/>
                        </a>                      
                      </li>
                    </xsl:for-each> 
                  </ul>
        </xsl:if>
  • Steve 472 posts 1216 karma points
    Aug 28, 2012 @ 17:19
    Steve
    0

    I have  a similar need, but I need to check only the top level parents in a <ul> to see if they have children and if they don't apply a <href>. All subsuquent children should allways have a link applied to them though. Any ideas?

    Page I am woring with:

    https://edit-www.rose-hulman.edu/offices-services/alumni-affairs-2.aspx

  • nesttor 4 posts 24 karma points
    Nov 13, 2012 @ 06:18
    nesttor
    0
    Thanks Dmitrij for your code,, it worked for me :)
  • Devin Bost 37 posts 78 karma points c-trib
    Jan 19, 2014 @ 23:05
    Devin Bost
    0

    Here's an example of using this in a hierarchical list:

    <ul class="navClearLevel1Ul">
    <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
    <li class="navClearLevel1Li">
        <a href="{umbraco.library:NiceUrl(@id)}">
            <xsl:value-of select="@nodeName"/>
        </a>
        <xsl:variable name="child" select="umbraco.library:GetXmlNodeById(@id)/*[@isDoc]"/>
        <xsl:if test="count($child) &gt; 0">
        <ul class="navClearLevel2Ul">
            <xsl:for-each select="@nodeName/ancestor-or-self::* [@level=$level2]/* [@isDoc and string(umbracoNaviHide) != '1']">
            <li class="navClearLevel2Li">
                <a href="{umbraco.library:NiceUrl(@id)}">
                    <xsl:value-of select="@nodeName"/>
                </a>
            </li>
            </xsl:for-each>
        </ul>
        </xsl:if>
    </li>
    </xsl:for-each>
    </ul>
    
Please Sign in or register to post replies

Write your reply to:

Draft