The code below works, but is bloated and full of redundancy. How can I rewrite it, perhaps making the for-each statement (the only difference between the blocks) conditional?
That's the same way I originally tried to do it, it makes sense when you look at it. However, it saves but throws a parsing error when you run it. My fear with templates is that again, I'll have to duplicate several dozen lines of code when only one line varies between them.
Ok, I decided to try the template approach as it allows for passing in a paramater (learned something new). I've almost got it, but still have two issues. First, I can't seem to get a reference to a specific node (currently shown as //root). Second, my test for child nodes appears hit and miss with the various methods I've tried. Right now, if there are no children the check is not finding that.
The reason your "//root" doesn't work is because you're selecting it within the root ("/") template - in there, the / refers to a small <macro> XML document - you need to build the XPath off of $currentPage to do it - I usually do this right after the currentPage param:
Conditional for-each?
The code below works, but is bloated and full of redundancy. How can I rewrite it, perhaps making the for-each statement (the only difference between the blocks) conditional?
Hi Connie,
One way would be to store the for-each xpath in a variable, not tested but something like this should work:
And hopefully Chriztian will pop in and tell us the right way to do it with templates :)
-Tom
That's the same way I originally tried to do it, it makes sense when you look at it. However, it saves but throws a parsing error when you run it. My fear with templates is that again, I'll have to duplicate several dozen lines of code when only one line varies between them.
Just tried it, you might need to add this to the for-each (wouldn't save until I did)
Also is this block inside another for-each loop? I assume it is since you reference current() in the first condition
-Tom
Yeah, that saves but still throws a parsing error.
I found this reference which actually works, although a bit ugly.
Problem now is I want to limit it to one level. Gotta restrict my results / replace my for-each.
Ok, I decided to try the template approach as it allows for passing in a paramater (learned something new). I've almost got it, but still have two issues. First, I can't seem to get a reference to a specific node (currently shown as //root). Second, my test for child nodes appears hit and miss with the various methods I've tried. Right now, if there are no children the check is not finding that.
Hi Connie,
The reason your "//root" doesn't work is because you're selecting it within the root ("/") template - in there, the / refers to a small <macro> XML document - you need to build the XPath off of $currentPage to do it - I usually do this right after the currentPage param:
I'll take a look at the rest and give you some hints to reduce it even further...
/Chriztian
Ok, a bit closer. Almost there...
Got this to work, finally, for the child nodes:
Now, I am using this temporarily, but need a correct solution to reference a specific node:
<xsl:with-param name="startNode" select="$currentPage/../../../.."/>
is working on a reply...