XML technologies / DocBook / Generating (X)HTML / Formatting headings / Formatting sections

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

As with chapters and similar structures, numbering and titles are output together. The following procedure separates them and displays the number in white on a gray background. Proceed as follows:

  1. Create a customization file as shown in Creating a customization file for (X)HTML. In this file, set the parameter html.stylesheet to your CSS file (see Adding a reference to a CSS file).

  2. Copy the template section.heading to your customization file. Depending on the output format, it is in html/sections.xsl or xhtml/sections.xsl.

  3. Find the following code inside the template:

<xsl:copy-of select="$title"/>

  1. Replace the code from step 3 with these lines:

<xsl:choose> 
    <xsl:when test="$level = 1 and string($section.autolabel) != 0">
        <span class="label">
            <xsl:apply-templates select="$section" mode="label.markup"/> 
        </span> 
        <span class="title">
            <xsl:apply-templates select="$section" mode="title.markup"/> 
        </span> 
    </xsl:when> 
    <xsl:otherwise>
        <xsl:copy-of select="$title"/> 
    </xsl:otherwise> 
</xsl:choose>

Line 2: Checks whether this is a first-level section and whether the parameter section.autolabel is set to 1. Numbering and title are separated only if both conditions hold.
Line 3: Generates the number and inserts it into a span element with a class attribute. CSS can format the number.
Line 6: Like line 3, but for the title.
Line 10: If the conditions in line 2 do not hold, copy the title from the parameter title.

  1. Create a CSS file with the following rules. The figure below shows the result:

h2.title span.label { 
  color: white; 
  background-color:gray; 
  margin-right: 1ex; 
  padding-left: 0.25em; 
  padding-right: 0.25em; 
}

Separate numbering for a first-level section

Figure: Separate numbering for a first-level section

  

back to top