|
|
Rank: Newbie
Joined: 6/5/2008 Posts: 4 Location: Indiana, USA
|
So I am still learning xslt and its workings with xpath. I have a general question i cant seem to find an answer to regarding nested for each loops.
Tree: $currentpage -node --nodeitem1 --nodeitem2 -node --nodeitem3 ---etc
So I am at currentpage and want to list the node name, and then perform a getitem to snag the note items under that node. However my results:
node-nodeitem1-nodeitem2-nodeitem3 node-nodeitem1-nodeitem2-nodeitem3
How can I make the nested for each only grab that one node's child properties? I apologize if i am bad at explaining this.
It almost seems as xslt is more of a instantaneous language than a sequential one.
Ideas?
Regards, JohnJack
|
|
Rank: Newbie
Joined: 6/5/2008 Posts: 4 Location: Indiana, USA
|
Problem solved: Quote: <xsl:template match="/">
<!-- The fun starts here -->
<xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']"> yadayadayada
<xsl:for-each select="umbraco.library:GetXmlNodeById(@id)/node ">
yadayadayada
</xsl:for-each> </xsl:for-each>
</xsl:template>
Thats right, I yada yada yada'd xslt. Hope someone finds this useful (i just looked at the list sub from changeable source and set the changeable source to @id of the current node in the loop. Regards, JohnJack
|
|
 Rank: Fanatic
Joined: 11/24/2006 Posts: 320 Location: Stockholm, Sweden
|
Hi jalder! It seems like the best approach to solving this would be to use some form of recursion. Take a look at this example. In this example the template drawNodes makes calls to itself and in this way prints the entire node tree. XSLT is a functional programming language so instead of using loops and variables who's values are altered you have to use recursion and parameters. (Variables in XSLT are in fact what most people call constants in other programming languages). Here's a good article about recursion in XSLT. /Thomas
Web Developer at Kärnhuset - http://www.karnhuset.net - Stockholm, Sweden
|
|
 Rank: Enthusiast
Joined: 4/11/2008 Posts: 36 Location: Ottawa Canada
|
Your results indicated that your xpath expresion was somethin like:
$currentPage//node
//node indicates to enumerate all nodes under currentPage entitled node regardless of the depth. In other words not just direct children. To get direct children only then you use the xpath expression $currentPage/node
There are several ways to do what your after but tkahn is correct in that a recursive method would be the most effecient method to do this. Recursion can be accomplished within a template with either a match or name in combination with params but the most effecient is the match method. Basically:
<xsl:template match="node" mode="call-me"> <li><xsl:value-of select="@nodeName"/> <xsl:if test="current()/node"> <ul> <xsl:apply-templates select="current()/node" mode="call-me"/> </ul> </xsl:if> </li> </xsl:template>
If you asking why the if test current()/node (ergo does the current node have a child called node) its because if you do not you could end up with a dread <ul/> which really gets the browser a little up tight when it comes to rendering as it expect <ul></ul>
Hope this helps...
Cheers Keith
|
|
|
Guest |