I have an XML like:
<menu>
<node id="home" url="url1.php">
<label>Homepage</label>
<node id="user" url="url2.php"><label>User profile</label></node>
<node id="help" url="url3.php"><label>Help page</label></node>
...
</node>
</menu>
Its used to generate a menu, the XML has node tags nested at any level under the first “home” node.
I pass a parameter called $id with PHP which gives the current active menu item.
(The <label> is in a separate tag and not as attribute because I’ve many labels for localization, actual xml is like <label lang='en'>...</label><label lang='it'>...</label>)
The idea is to use various XSLs to generate main menu, breadcrumbs, section titles (top menu).
For the main menu I’ve managed to do this:
<xsl:template match="menu">
<xsl:apply-templates select="node" />
</xsl:template>
<xsl:template match="//node">
<ul>
<li>
<a>
<xsl:if test="@id=$id">
<xsl:attribute name='class'>active</xsl:attribute>
</xsl:if>
<xsl:attribute name='href'>
<xsl:value-of select="@url" />
</xsl:attribute>
<xsl:value-of select="label"/>
</a>
<xsl:if test="count(child::*)>0">
<xsl:apply-templates select="node" />
</xsl:if>
</li>
</ul>
</xsl:template>
</xsl:stylesheet>
And it works. But I’m stuck with the breadcrumbs.
How can I isolate only a specific node with @id=$id and his ancestors, to build breadcrumbs up to current page from home?
Resulting html should be, for a node ad the third nested level:
<ul>
<li><a href="url1.php">Home</a></li>
<li><a href="urla.php">Some child of home</a></li>
<li><a href="urlb.php">Some grandchild of home</a></li>
<li><a class='active' href="urlc.php">Current page which is child of the above</a></li>
</url>
The breadcrumb you can do like this, by selecting the active node and then walking the ancestors, like so:
Sample Menu Xml:
Edit Update – You’ve updated the Q to display the breadcrumb as a list – here’s an updated template just in case 🙂