I think below code will useful to you. In my project i write the below code in xslt and create macro and put that macro in my main template then its working fine for me.
Paul, the GetXmlNodeById method only gives you that one node and not it's subnodes, so that's the problem. I believe that if you do apply-template, it already does a for-each for you. Try this:
First make sure that the node you're selecting with the $dateNodeId is the right one (it's selected as a childnode of $currentPage - so every page has one of these, right?) - The way you're using a variable/parameter here, suggest to me that you might have a single node with that id in your tree, and that you mean to select the childnodes of that particular node.
If you only have one node with the id of $dateNodeId, select it by going back to the <root> element and select it from there:
Secondly, assuming you've selected the right node - apply-templates will find the template that best matches it which will most likely be the template you've supplied - when inside that template, you have something called the current node which is the node you're processing, and this node will be the starting point for XPaths inside the template, e.g. you can apply templates to all childnodes just by saying:
<xsl:apply-templates select="node" />
- but if all you want to do is do something for all the childnodes of that particular node, you can say that in the first apply-templates statement — I'd do it like this:
...
<xsl:param name="currentPage" />
<xsl:variable name="root" select="$currentPage/ancestor-or-self::root" />
<!-- Don't know where you get this - fill in the real id -->
<xsl:variable name="dateNodeId" select="1234" />
<xsl:template match="/">
<xsl:apply-templates select="$root//node[@id = $dateNodeId]/node" />
<!-- OR use this one this one, if below $currentPage: -->
<!-- <xsl:apply-templates select="$currentPage/node[@id = $dateNodeId]/node" /> -->
</xsl:template>
<!-- Template for node output -->
<xsl:template match="node">
<p>
<xsl:value-of select="@nodeName" />
</p>
</xsl:template>
...
Chriztian: The problem with going all the way to the root is that this won't work if you have multiple sites in one Umbraco install. You should really only use ::root if you want data from all of your sites. Even if you are only running one site now, who knows what will happen in the future. Besides, it's a little faster as well. So instead try using:
$currentPage/ancestor-or-self::node
Also, for performance reasons, try to avoid using this at all, and also selecting using "//node". This will cause the XSLT parser to walk through ALL of the nodes in your XML, which will start getting slower when your XML file gets larger.
I would recommend going directly to some node first and then selecting underlying nodes, whenever you can, especially if you are trying to use the underlying nodes of $currentPage (why walk all the way up the tree, only to end up at the current page tree again).
Paul, when I don't get any results, I try outputting the raw XML of whatever I'm trying to show in my page, like so for example:
It is a matter of selecting subnodes of a given node as Chriztian mentioned. I call the page with either an id of the 'main' node and then select all the subnodes or call it without an id in which case I take the 'first' main node. I have chosen the fastest one as both of you suggested.
Iterating over sub-nodes
Hi
I try to execute a template on the subpages of a specific node given by $dateNodeId. I have a problem iterating over the subnodes in my template.
Anyone knows how it should be ?
Here's my attempt
<xsl:apply-templates select="$currentPage/node [@id = $dateNodeId]"> </xsl:apply-templates> <xsl:template match="node">
<xsl:for-each select="umbraco.library:GetXmlNodeById(@id)/node"> -- this is the problem <xsl:value-of select="@nodeName"/> ..... </xsl:for-each>
</xsl:template>
Hi paul,
I think below code will useful to you. In my project i write the below code in xslt and create macro and put that macro in my main template then its working fine for me.
Hope this helps to you.
<xsl:for-each select="$currentPage/node">
<xsl:value-of select="./@nodeName"/>
</xsl:for-each>
----vijay.
Paul, the GetXmlNodeById method only gives you that one node and not it's subnodes, so that's the problem. I believe that if you do apply-template, it already does a for-each for you. Try this:
<xsl:apply-templates select="$currentPage/node [@id = $dateNodeId]/node"> </xsl:apply-templates> <xsl:template match="node"> <xsl:value-of select="@nodeName"/> </xsl:template>
Hi Sebastiaan
Sounds right and I've tried it but it returns nothing
/Paul S
Hi Paul,
First make sure that the node you're selecting with the $dateNodeId is the right one (it's selected as a childnode of $currentPage - so every page has one of these, right?) - The way you're using a variable/parameter here, suggest to me that you might have a single node with that id in your tree, and that you mean to select the childnodes of that particular node.
If you only have one node with the id of $dateNodeId, select it by going back to the <root> element and select it from there:
Secondly, assuming you've selected the right node - apply-templates will find the template that best matches it which will most likely be the template you've supplied - when inside that template, you have something called the current node which is the node you're processing, and this node will be the starting point for XPaths inside the template, e.g. you can apply templates to all childnodes just by saying:
- but if all you want to do is do something for all the childnodes of that particular node, you can say that in the first apply-templates statement — I'd do it like this:
/Chriztian
Chriztian: The problem with going all the way to the root is that this won't work if you have multiple sites in one Umbraco install. You should really only use ::root if you want data from all of your sites. Even if you are only running one site now, who knows what will happen in the future. Besides, it's a little faster as well. So instead try using:
Also, for performance reasons, try to avoid using this at all, and also selecting using "//node". This will cause the XSLT parser to walk through ALL of the nodes in your XML, which will start getting slower when your XML file gets larger.
I would recommend going directly to some node first and then selecting underlying nodes, whenever you can, especially if you are trying to use the underlying nodes of $currentPage (why walk all the way up the tree, only to end up at the current page tree again).
Paul, when I don't get any results, I try outputting the raw XML of whatever I'm trying to show in my page, like so for example:
If that doesn't show anything, find out if your $dateNodeId variable actually even contains something.
Thanks guys I solved it
It is a matter of selecting subnodes of a given node as Chriztian mentioned. I call the page with either an id of the 'main' node and then select all the subnodes or call it without an id in which case I take the 'first' main node. I have chosen the fastest one as both of you suggested.
Thanks again
Paul S
is working on a reply...