Useful XSLT-snippets for Umbraco

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:–Display-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>

One thought on “Useful XSLT-snippets for Umbraco

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s