Keywords: titles, title.markup, subtitle.markup, titleabbrev.markup

Problem

You need to retrieve a title (or subtitle), but your current node is not a title.

Solution

The DocBook XSL stylesheets offer the title.markup mode for this purpose. Usually, insert the following code into the appropriate place:

<xsl:apply-templates select="." mode="title.markup"/>

The xsl:apply-templates recursively finds the correct title node, regardless where you are. It also looks into an info element, if necessary.

Discussion

DocBook contains three elements which are used for title markup:

title

The main title. According to the TDG“it identifies the titles of documents and parts of documents, and is the required caption on formal objects.”

subtitle

A optional subtitle is an alternative or explanatory title in addition to its main title. Usually a subtitle is printed below the main title on the same page.

titleabbrev

The optional, abbreviated title is used in case the main title is overly verbose. Usually this element is not printed with the main title on the same page. However, it is used in the table of contents, for example.

All of the above title elements contain a special mode to get the content of this node:

Table 2.1. Modes for Title Elements
ElementMode
titletitle.markup
subtitlesubtitle.markup
titleabbrevtitleabbrev.markup

To further elaborate why you should use one of the previous modes, let's assume you have this chapter title:

<chapter>
   <title>Programming in Python</title>
   <!-- further substructure pruned -->
</chapter>

Let's further assume, you have a template where you need the title from the above chapter. One simple, yet unfavorable, solution could be coded like this:

<xsl:template name="...">
   <xsl:value-of select="ancestor::d:title"/>
</xsl:template>

This has several disadvantages:

  • The XPath does not consider a title inside an info element.

  • The xsl:value-of returns the string value. In most cases this is correct. However, if you have a quote inside title, the quote is not processed and you will not get any quotation marks.

Replacing xsl:value-of with xsl:apply-templates as shown in the solution section, ensures correct processing of child elements inside title.

The same is true for subtitle and titleabbrev. Using the subtitle.markup or titleabbrev.markup mode ensures that you always get the correct content.

To see what combination results in what string, find an overview in Table 2.2, “Different Combinations and Their Results on *.markup Modes”.

Table 2.2. Different Combinations and Their Results on *.markup Modes
CodeResults
<title>Programming in Python</title>
  • title.markup: “Programming in Python”

  • subtitle.markup: “” (empty string)

  • titleabbrev.markup: “Programming in Python”

<title>Programming in Python</title>
<subtitle>With an IDE</subtitle>
  • title.markup: “Programming in Python”

  • subtitle.markup: “With an IDE”

  • titleabbrev.markup: “Programming in Python”

<title>Programming in Python</title>
<titleabbrev>Python</titleabbrev>
  • title.markup: “Programming in Python”

  • subtitle.markup: “” (empty string)

  • titleabbrev.markup: “Python”

<title>Programming in Python</title>
<subtitle>With an IDE</subtitle>
<titleabbrev>Python</titleabbrev>
  • title.markup: “Programming in Python”

  • subtitle.markup: “With an IDE”

  • titleabbrev.markup: “Python”

See Also


Project@GitHubIssue#7