XML technologies / DocBook / Generating (X)HTML / Editing headers and footers / Adding breadcrumbs

(Excerpt from "DocBook-XML: Media-neutral and platform-independent publishing" by Thomas Schraitle)

Breadcrumb navigation shows the path to the current element, helping readers navigate deeply nested structures. Its name comes from Hansel and Gretel and their trail of breadcrumbs through the forest.

  1. First create a customization file as shown in Creating a customization file for (X)HTML.

  2. Add a parameter to enable or disable breadcrumb navigation as needed. It is enabled by default:

<xsl:param name="generate.breadcrumbs" select="1"/> 

  1. Add another parameter for the separator between path components. Choose any separator you like:

<xsl:param name="breadcrumbs.separator"> 
    <xsl:text> » </xsl:text> 
</xsl:param>

  1. Add the call to the normally empty template user.header.content. The parameter generate.breadcrumbs checks whether breadcrumbs are wanted, creates a div element and inserts the navigation:

<xsl:template name="user.header.content"> 
    <xsl:if test="$generate.breadcrumbs != 0"> 
        <div class="userheadercontent"> 
            <xsl:call-template name="generate-breadcrumb"/> 
        </div> 
    </xsl:if> 
</xsl:template>

  1. Create the template generate-breadcrumb:

<xsl:template name="generate-breadcrumb"> 
    <div class="breadcrumb">
        <p>
            <xsl:for-each select="ancestor::*">
                <xsl:element name="a">
                    <xsl:attribute name="href">
                        <xsl:call-template name="href.target">
                            <xsl:with-param name="object" select="."/> 
                            <xsl:with-param name="context" select="."/> 
                        </xsl:call-template> 
                    </xsl:attribute> 
                    <xsl:apply-templates select="." mode="title.markup"/>
                </xsl:element> 
                <xsl:copy-of select="$breadcrumbs.separator"/>
            </xsl:for-each> 
            <xsl:if test="self::* != /*">
                <strong> 
                    <xsl:apply-templates select="." mode="title.markup"/> 
                </strong> 
            </xsl:if> 
        </p> 
    </div> 
</xsl:template>

Line 2: The complete breadcrumb navigation is wrapped in a div element with a corresponding class attribute for CSS customization.
Line 3: The navigation itself is in a paragraph element.
Line 4: Iterates over all ancestors.
Line 5: Creates an a element linking to the current ancestor.
Line 6: Creates an href for the preceding a element.
Line 7: Inserts the current ancestor’s filename by calling the template href.target (especially important for chunked output).
Line 12: Inserts the current ancestor’s title.
Line 14: Copies the separator between breadcrumb items.
Line 16: Checks that the current element is not a root node; if so, inserts the current page’s title.

  1. The following CSS rule illustrates inline alignment:

div.breadcrumbs > p { 
  display: inline; 
}

DocBook-XML » Transformationen » Transformieren nach (X)HTML

Display: Breadcrumb navigation

  

back to top