Developer forum

Forum » Templates » Creating XSLT menu

Creating XSLT menu


Reply
Hi,
I'm trying to create navigation menu using XSLT.
In the menu I'd like to always show all the nodes of the first and the second navigation level so I'm using foreach loop.

It works well for the first level but I cannot get access to the second level nodes.

<xsl:template match="/NavigationTree[Page]">

    <ul id = "navigation">
    <xsl:for-each select="Page">
          <li><xsl:value-of select="@MenuText" /></li>               

          <xsl:value-of select="count(Page)"/>             
                 count(Page) is always = 0 but i think it should return the number of the child nodes.

          <xsl:for-each select="child::*">
                - the loop does not execute since i have
                   no access to the child nodes.
            </xsl:for-each>

    </xsl:for-each>
     </ul>

GetNavigationXML.aspx:
<Page ID="1" />

<Page ID="2" >
    <Page ID="3" "/>
    <Page ID="4"/>
</Page>

<Page ID="5" >
    <Page ID="6" />
    <Page ID="7" />  
</Page>

<Page ID="8" />

I'm sure I'm missing something obvious since I'm new to XSLT.
Thank you for any help.

Piotr Jablonski




Replies

 
Reply
Hi Piotr

Try this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" encoding="utf-8" />
 
 <xsl:template match="/">
  <xsl:apply-templates select="NavigationTree[Page]" />
 </xsl:template>
 
 <xsl:template match="NavigationTree">
  <ul>
   <xsl:apply-templates select="Page" />
  </ul>
 </xsl:template>
 
 <xsl:template match="Page">
  <li>
   <a href="{@Href}">
    <xsl:value-of select="@MenuText" />
   </a>
   <xsl:if test="Page">
    <ul>
     <xsl:apply-templates select="Page" />
    </ul>
   </xsl:if>
  </li>
 </xsl:template>
</xsl:stylesheet>

// Dammark
 
Reply
Thank you for the answer but still, even using your template, I'm getting only the first level nods.

 
Reply
Can you throw me a link ?

// Dammark
 
Reply
The page is not online yet.. just on my local machine.
I attach a full result of GetNavigationXML file is it what you were thinking of seeing?
 
Reply
Could you rename the aspx file to xml please ?
 
Reply
sure
 
Reply
Hi again

Unzip the attached file and open the xml file in IE.
Then you will see how it should work.

If it doesn't, then there is a bug on your website and you need to register a case in the service desk.

// Dammark
 
Reply
Can you please attach the file ;)

 

You must be logged in to post in the forum