in

Platinum Bay

Peace, Love, and...

This Blog

Syndication


.NETicated

Recursive Fields in Umbraco with XSLT

I’ve been working with Umbraco quite a bit lately and I’m really loving it as a content management framework. I’ve already converted GeekGive.org, GeekRoadTrip.com, and I’m working on a few others. Being a developer though I often find myself outside the box and today is no different.

I’m building a site where the homepage will actually reflect content from a subpage including the title. This means I have to get rid of my standard title block in the template and use XSLT.

My standard title template block:

<title><asp:placeholder runat="server"><umbraco:Item field="pageName" runat="server"></umbraco:Item> - <umbraco:Item field="siteName" recursive="true" runat="server"></umbraco:Item></asp:placeholder></title>

I created a macro and set about to define the title text. Then it hit me, how do I recursively get the siteName field I defined on the homepage? It turns out this is really simple to do using XSLTs ancestor-or-self function in the value-of select:

$currentPage/ancestor-or-self::node [string(data[@alias='siteName'])!=''] [1] /data[@alias='siteName']

The full macro is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="
http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<xsl:template match="/">
    <xsl:if test="$currentPage/data [@alias = 'pageTitle'] != ''">
        <xsl:value-of select="$currentPage/data [@alias = 'pageTitle']"/>
        -
    </xsl:if>
    <xsl:value-of select="$currentPage/ancestor-or-self::node [string(data[@alias='siteName'])!=''] [1] /data[@alias='siteName']" />
</xsl:template>
</xsl:stylesheet>

Hope this helps!

Published Sep 09 2010, 05:12 AM by Steve
Filed under: ,

Comments

September 9, 2010 12:26 PM

Pingback from  Dew Drop &ndash; September 9, 2010 | Alvin Ashcraft&#039;s Morning Dew

 

September 13, 2010 11:05 PM

My favorite part is:

"="$currentPage/data [@alias = 'pageTitle'] != "

Phil A.
 

September 17, 2010 8:20 AM

Pingback from  Recursive Fields in Umbraco with XSLT &#8211; .NETicated &laquo;  xslt

 

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews is an independent consultant, INETA speaker, and Microsoft MVP for Visual Studio ALM. He has been working in technology for over ten years focusing on custom application development and Application Lifecycle Management. Steve is also Microsoft and IBM certified and a community fanatic having led sessions at nearly 100 events across North America. When he's not developing software solutions or engaging with the community about software technology, Steve is a closet singer and songwriter and plays the guitar and keys. Occasionally, Steve even gets to sleep. Occasionally.
Powered by Community Server (Commercial Edition), by Telligent Systems
© Platinum Bay | Some Rights Reserved Creative Commons License

Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or annonymous comments) - so keep it polite, please.