Return to doc.sitecore.com

Determine Node Depth

The depth of a node in the content tree can be determined by counting its element ancestors, or all ancestors of element type item.  This can be accomplished with the following code:

<xsl:variable name="depth" select="count( ancestor-or-self::item )" />

The same technique can be used to determine the distance between two nodes in the hierarchy:

<xsl:variable name="difference" select="count( ancestor-or-self::item )

  - count( $home/ancestor-or-self::item )" />