Reputation: 357
There should be a fairly simple solution but I cant get it to work.
This is my code:
<xsl:value-of select="$currentPage/ancestor-or-self::* [@isDoc and backgroundImage != '' and string(umbracoNaviHide) != '1']/backgroundImage"/>
What I want is if the node has a background image attached to it by the property backgroundImage, then use that image. If it does not have an background image, check the parent node for the background image and use that.
What happends for me is that it always checks for the parents background image, even if the node itself has a background image.
Any suggestions on how I could resolve this?
Update
<site id="1000" ...>
<backgroundImage>/media/image.jpg</backgroundImage>
<textPage id="1001" ...>
<backgroundImage>/media/image2.jpg</backgroundImage>
</textPage>
<textPage id="1002" ...>
<backgroundImage />
</textPage>
</site>
If I'm on <textPage id="1002">
where backgroundImage is null I would like to get image.jpg from <site id="1000">
.
If I'm on <textPage id="1001">
where backgroundImage is not null, I would like to get image2.jpg.
Upvotes: 2
Views: 393
Reputation: 4393
Numeric predicates applied to an XPath address are processed in proximity order, while the resulting nodes from the addressed are handed to an XSLT processor in document order.
In XSLT 1.0, <xsl:value-of>
returns the first of the nodes returned, so the first of the nodes in document order, so if you are addressing all of the ancestors or self with given properties, you will get the farthest one, not the closest one. In XSLT 2.0 you get all of them.
Since predicates are applied in proximity order, just use:
<xsl:value-of select="$currentPage/ancestor-or-self::*[...whatever...][1]"/>
... to get the closest of those with the properties indicated by the "whatever" predicate.
The reason [last()]
works in Frank's solution is that the parentheses return the enclosed nodes in document order, so the closest one of those will be the last.
But from a semantics perspective, I think it reads better to simply address the first in proximity order.
Upvotes: 4
Reputation: 13315
Try
<xsl:value-of select="($currentPage/ancestor-or-self::* [@isDoc and backgroundImage != '' and string(umbracoNaviHide) != '1'])[last()]/backgroundImage"/>
You get the sequence of all ancestors including the current element that fulfill the conditions with your code. If you add the last()
function, you will get the last item of this sequence that fulfills the condition.
Upvotes: 0