CodeGarden 10: The sixth annual Umbraco Developer Conference
June 23-25th 2010 - free ASP.NET MVC pre-conference. Register today!

Settings in root node and value from recursive search

I find these small xslt-snippets very useful

First, it's nice to put global settings as properties in the site root node document type. And get them from there whenever needed like this:

<xsl:variable name="siteRootNode" select="$currentPage/ancestor-or-self::node [@level=1]"/>
Value from root node:<xsl:value-of select="$siteRootNode/data[@alias='EditorEmailAddress']"/>

(If site root node is at level 1).

Another nice thing is to set reference names for nodes so that one does not need to hard code the node id in xslt. I do this by adding a extra property to my nodes, property name xsltNodeName. In that node I set a node name that should not be changed by editors or writers. And when I need it I call it using:

<xsl:value-of select="umbraco.library:GetXmlAll()//node [string(data[@alias='xsltNodeName'])='TheName'] /@id"/>

This code is used to get a property value recursively, "if the value is not found on this node, go up one step at a time until it's found"

Value from recursive search:<xsl:value-of select="$currentPage/ancestor-or-self::node [string(data[@alias='EditorEmailAddress'])!=''] [position()=last()] /data[@alias='EditorEmailAddress']"/>,
Found at:<xsl:value-of select="$currentPage/ancestor-or-self::node [string(data[@alias='EditorEmailAddress'])!=''] [position()=last()] /@nodeName"/>

Also, have a look at this nice post about more ways to do recursive search: forum.umbraco.org/...play-a-field-recursively.aspx (from Casey Neehouse).

Note; in a template a property can be sought recursively just like this:

<umbraco:Item field="EditorEmailAddress" recursive="true" runat="server"></umbraco:Item>