2
votes

I am working with XSLT files in an Umbraco solution.

I want the entire ul tag to disappear if there is no elements to be shown:

<ul>
  <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/child::* [@isDoc and string(umbracoNaviHide) != '1']">
    <li>
      <a href="{umbraco.library:NiceUrl(@id)}" class="menuitem">
        <xsl:value-of select="@nodeName"/>
      </a>
    </li>
  </xsl:for-each>
</ul>

How can I make a IF statement that skips the entire codeblock if the foreach loop has no items?

1

1 Answers

3
votes

Try putting the nodes you are going to select in a variable first, and then wrapping the output of a ul element in an xsl:if where you test the number of nodes.

Something like this should do:

<xsl:variable name="children" select="umbraco.library:GetXmlNodeById($source)/child::* [@isDoc and string(umbracoNaviHide) != '1']" />
<xsl:if test="count($children) > 0">
   <ul>
     <xsl:for-each select="$children">
       <li>
         <a href="{umbraco.library:NiceUrl(@id)}" class="menuitem">
           <xsl:value-of select="@nodeName"/>
         </a>
       </li>
     </xsl:for-each>
  </ul>
</xsl:if>