I’m trying to build a multi-level dropdrown CSS menu for a website I’m doing on the umbraco content management system.
I need to build it to have the following structure:
<ul id='nav'> <li><a href='..'>Page #1</a></li> <li> <a href='..'>Page #2</a> <ul> <li><a href='..'>Subpage #1</a></li> <li><a href='..'>Subpage #2</a></li> </ul> </li> </ul>
So now I’m trying to figure out how to do the nesting using XSLT. This is what I have so far:
<xsl:output method='xml' omit-xml-declaration='yes'/> <xsl:param name='currentPage'/> <!-- update this variable on how deep your menu should be --> <xsl:variable name='maxLevelForMenu' select='4'/> <xsl:template match='/'> <ul id='nav'> <xsl:call-template name='drawNodes'> <xsl:with-param name='parent' select='$currentPage/ancestor-or-self::node [@level=1]' /> </xsl:call-template> </ul> </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)'> <xsl:for-each select='$parent/node [string(./data [@alias='umbracoNaviHide']) != '1' and @level <= $maxLevelForMenu]'> <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 <= $maxLevelForMenu]) > 0'> <xsl:call-template name='drawNodes'> <xsl:with-param name='parent' select='.'/> </xsl:call-template> </xsl:if> </li> </xsl:for-each> </xsl:if> </xsl:template>
What I can’t seem to figure out is how to check if the first level (here Page #1 and Page #2) has any children, and if they do add the extra <ul> to contain the <li> children.
Anyone out there to point me in the right direction?
First off, no need pass the a
parentparameter around. The context will transport this information.Here is the XSL stylesheet that should solve your problem:
This is the XML I tested it on (I don’t know much about Umbraco, but after looking at some samples I hope I got close to an Umbraco document):
This is the output: