Multilevel Navigation
This is an open discussion with 19 replies, filed under XSLT.
Search
Currently, the way of finding out of any of the elements children (or grand-grand-grand-grand-children) is the currently active element is a bit dirty:
<xsl:if test="page != '' and ($collapse = 'no' or substring-before($parent-path,@handle) != '' or @id = $current-page-id)" />
I was wondering if there was a cleaner way to find out if any of the childrens @id
equals $current-page-id
, no matter how deeply nested it may be.
Would something like this work?
<xsl:if test="//*[@id = $current-page-id]" />
Would something like this work?
It says “any child of the root node”.
<xsl:if test="descendant::node()[@id = $current-page-id]" />
did the trick. Thanks for trying to help though. :-)
Multilevel Navigation
updated to version 1.1
on 8th of December 2009
Sorry, I meant to say so long as you prefix it with your context node. So if you are in a for-each
loop you could use:
current()//*[@id = $current-page-id]
I believe //
is shorthand for descendant::node()
anyway.
But using the full axis reduces the risk of confusion :-)
I thought //
means “leave context and go to absolute root node”…
If it’s not prefixed with anything it is for sure.
Ah… current()//*
, I see. I was thinking about the example you mentioned before. :-)
Hey, this utility just saved me a bunch of time. Thanks!
yeah thx 4 that util :)
does anyone know a simple condition to prevent empty ul`s?
on my third level all pages are hidden but I always got an empty ul.. tried adding several conditions, but they do not work ;/
sth like that:
not(self::node()/types/type = 'hidden')
Hello,
Can I have a menu like this scheme: menu sheme
Roelof please do not double post. You have asked this in the Recursive Navigation thread which is sufficient.
Sorry for that. I try to find a way to make this work for over a week. And I thought this could be the way to go.
Roelof
Hey Nils, with regards to my suggestion to use Github for utilities - would you mind adding a repo/gist for this utility also?
Great Utility! thanks Nils, been playing with it. Adjustment needed in page match for path... need $root
in there if your using subfolders for the site.. i.e local dev.
Love the collapse feature too. loads of uses :) woop
Hey Nils, is there an easy way to specify how many levels deep to output in the utility? i.e an extra param of some sort?:
with-param name="levels" select="2"
Where would something like this actually fit into the Utility?
Depends, really.
Provided you want to combine it with $collapse = 'no'
(wich would display all your pages in a huge list) it's a simple as a separate template at the very bottom of the utility:
<xsl:template match="page/page/page" />
For the case of "I've just reached a <page>
inside a <page>
inside a <page>
" it simply overrides the default "any <page>
" template (wich displays the link and continues, no matter how deep down in your tree) with an empty template that also aborts the recursion.
If you wanted $collapse = 'yes'
(wich would collapse all nodes that don't lead to the currently active page) you'd have to specify it a bit more clearly. Meaning: Which part of the tree should be limited to 2? The nodes not leading to the active page? The nodes trailing the active page? Prematurely abort recursion on the branch that leads to the active page?
I guess I'm thinking the nodes trailing the active page.. so then I can place a sub nav elsewhere on the template that outputs the 3rd level deep of pages if needed.
I could use CSS to hide the UL's within UL's but don't want to do that if there's a means to negate the need to do so in the template.
Not sure how to read this line, is descendant::node a function or element?
descendant::node()[$id = $current-page-id]
Create an account or sign in to comment.
A new XSLT utility, “Multilevel Navigation” is now available for download. Comments and feedback can be left here but if you discover any issues, please post it on the issue tracker.