The desired end result is for the content editor to be able to set the headerLogoURL on the homepage and then if needed overide the parameter on a child page.
I want to place the macro in my top level template.
<!-- Process the first ancestor (or self) that has a value in headerLogoURL -->
<xsl:apply-templates select="$currentPage/ancestor-or-self::*[normalize-space(headerLogoURL)][1]/headerLogoURL" />
<!-- Template for the headerLogoURL -->
<xsl:template match="headerLogoURL">
<xsl:value-of select="url-picker/url" />
<xsl:value-of select="url-picker/node-id" />
<xsl:value-of select="url-picker/new-window" />
<xsl:value-of select="url-picker/link-title" />
</xsl:template>
The XPath starts from $currentPage, using the ancestor-or-self:: axis it grabs all nodes that have a value in the headerLogoURL field, takes the first of those and selects the headerLogoURL field.
When you add square brackets to a selection step, its called a "predicate" and it works like a filter - you can loosely translate them to a WHERE clause.
normalize-space() is a handy function that trims leading and trailing as well as repeated white-space from an element, so if the value is all spaces og tabs etc. it will return an empty string.
[1] is a shortcut for [position() = 1] - in case of the ancestor-or-self:: axis, this means the first ancestor encountered when traveling upwards.
Yet another question... I am using uComponents url picker to set headerLogoURL and it appears that even if nothing is selected or entered in the url picker it still returns a node on every Doc node:
Figured out what I was doing wrong! I can now set a default logo for my site at the top level but it can be overriden on lower level documents. So now my content editors can easily co-brand a page within the site either with a logo from the media library or an image url hosted on another site.
Just in case anyone else is trying to do something similar here is what I ended up with:
<xsl:template match="/"> <!-- Process the first ancestor (or self) that has a value in headerLogoURL/url-picker/url and work with headerLogoURL in the next template --> <xsl:apply-templates select="$currentPage/ancestor-or-self::*[normalize-space(headerLogoURL/url-picker/url)][1]/headerLogoURL" /> </xsl:template>
<!-- Template for the headerLogoURL --> <xsl:template match="headerLogoURL"> <a href="/"><xsl:attribute name="style">display:block; width:349px; height:200px; background:transparent url('<xsl:value-of select="url-picker/url"/>') no-repeat center;</xsl:attribute>Home</a> </xsl:template>
Great to see you arrived at something that works :-)
Just to clarify your questions regarding the ancestor:: axis: You're not quite right on that - it gives you the parent, its parent, that one's parent etc., so you would never get any siblings of the current node (which is what you're guessing at).
The preceding:: axis will give you those - but you should really give the XPath Axes Visualizer a spin to really get the hang of them :-)
Recursive uComponents url picker XSLT values
Hi! I am horrible at xslt and am trying to figure out how to retrieve the equivelent of:
In an xslt macro
from ancestor-or-self
The desired end result is for the content editor to be able to set the headerLogoURL on the homepage and then if needed overide the parameter on a child page.
I want to place the macro in my top level template.
Hi cchehn,
This is how to lookup recursively in XSLT:
/Chriztian
Thanks Chriztian,
Can you give me a little detail on what *[][1] is actually doing in the select?
Yes - of course :-)
The XPath starts from $currentPage, using the ancestor-or-self:: axis it grabs all nodes that have a value in the headerLogoURL field, takes the first of those and selects the headerLogoURL field.
When you add square brackets to a selection step, its called a "predicate" and it works like a filter - you can loosely translate them to a WHERE clause.
normalize-space() is a handy function that trims leading and trailing as well as repeated white-space from an element, so if the value is all spaces og tabs etc. it will return an empty string.
[1] is a shortcut for [position() = 1] - in case of the ancestor-or-self:: axis, this means the first ancestor encountered when traveling upwards.
Hope that helps you understand what's going on.
/Chriztian
Yes this is very helpful, thanks very much! I have another followup question if it is not too much trouble.
If I specify [1] for the first ancestor then in a structure:
<n1>
<val>a</val>
<n2>
<val>b</val>
<n3>
<val></val>
<n3>
</n2>
<n4>
<val></val>
</n4>
</n1>
On n3 I would get val = b ?
On n4 I would get val = a ?
Am I grasping this correctly?
Thanks for your help Chriztian,
Yet another question... I am using uComponents url picker to set headerLogoURL and it appears that even if nothing is selected or entered in the url picker it still returns a node on every Doc node:
How do I account for this since in my original example headerLogoURL will always be set but url-picker/url might be blank?
I tried:
But then I have no idea what to select in the template.
Thanks very much in advance!
-C.
Figured out what I was doing wrong! I can now set a default logo for my site at the top level but it can be overriden on lower level documents. So now my content editors can easily co-brand a page within the site either with a logo from the media library or an image url hosted on another site.
Just in case anyone else is trying to do something similar here is what I ended up with:
Thanks again Chriztian
Hi cchehn,
Great to see you arrived at something that works :-)
Just to clarify your questions regarding the ancestor:: axis: You're not quite right on that - it gives you the parent, its parent, that one's parent etc., so you would never get any siblings of the current node (which is what you're guessing at).
The preceding:: axis will give you those - but you should really give the XPath Axes Visualizer a spin to really get the hang of them :-)
/Chriztian
is working on a reply...