Return to doc.sitecore.com

Breadcrumb Example

The most important concept in generating the breadcrumb is an understanding of the XSL ancestor-or-self:: axis.  Sitecore’s XML repository is basically a hierarchy of <item> elements which contain data in addition to child <item> elements.  The default home item for a Sitecore installation is basically the item with name attribute set to “Home” in this abbreviated XML:

<item name="sitecore" …>

  <item name="content" …>

    <item name="Home" …>

The following statement:

<xsl:for-each select="ancestor-or-self::item">

Instructs the processor to evaluate a node list using the ancestor-or-self:: axis from the context node, matching any element named item.  The processor will iterate over the ancestors of the XSL context node, finally processing the context node itself.  If the XSL context node is this Home node (which occurs by default in the primary <xsl:template> of XSL Renderings processing the Home page), the processor will iterate over the ancestors of that Home node and finally the Home node itself.  Iteration will occur in document order – from the top of the XML document (/sitecore) to the context node (/sitecore/content/Home).  This makes it very easy to build a breadcrumb based on the context node’s location in the information architecture:

1.  Breadcrumb XSL Rendering

<xsl:template match="*" mode="main">
  
<xsl:for-each select="ancestor-or-self::item">
    
<xsl:if test="position() > 2 and @template != 'folder'">
      
<xsl:choose>
        
<xsl:when test="position() != last()">
          
<sc:link><xsl:call-template name="GetNavTitle" /></sc:link>
        
</xsl:when>
        
<xsl:otherwise>
          
<xsl:call-template name="GetNavTitle" />
        
</xsl:otherwise>
      
</xsl:choose>
      
<xsl:if test="position() != last()"><td>; » </td></xsl:if>
    
</xsl:if>
  
</xsl:for-each>
</xsl:template>

<xsl:template name="GetNavTitle">
  
<xsl:param name="item" select="." />
  
<xsl:choose>
    
<xsl:when test="sc:fld( 'navtitle', $item )">
      
<xsl:value-of select="sc:fld( 'navtitle', $item )" />
    
</xsl:when>
    
<xsl:otherwise>
      
<xsl:value-of select="$item/@name" />
    
</xsl:otherwise>
  
</xsl:choose>
</xsl:template>

The position() test in the first conditional causes the /sitecore, /sitecore/content and any nodes associated with the Folder template to be excluded from the breadcrumb. The choose tests if the iterator is processing the last entry in the node list, which would be the context node - in this case no link to the page is created. The text of the breadcrumb entry is set to the value returned by the GetNavTitle named template in a function library. If the iterator is not processing the last item in the node list, a separator is output between breadcrumb entries.