XML technologies / DocBook / The DocBook XSL stylesheets / Customizing cross-references / Customizing with the xrefstyle attribute

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

To change cross-reference formatting only at certain locations, use the attribute xrefstyle. It is allowed on the element xref as well as biblioref, link and olink.

The attribute xrefstyle accepts the following values:

template:

After template: text with placeholders is expected. Placeholders specify the title, number, page, and so on, and are replaced with their values.

select:

After select: keywords resolved by the stylesheets are expected.

Any invented name

Otherwise, the contents are interpreted as a style name defined in a language file.

 

The template keyword

If the value of the attribute xrefstyle starts with template:, the remaining string is treated as fixed text with placeholders. This is useful if the default generated text is unsuitable or does not fit smoothly into the surrounding text.

The following text contains an xrefstyle attribute:

Im <xref linkend="XPath" xrefstyle="template:Kapitel %n über %t"/> erfahren Sie mehr zu diesem Thema.

Resolving the text produces:

Im Kapitel 13 über XPath erfahren Sie mehr zu diesem Thema.

The following table describes the available placeholders:  

Table: Available placeholders

Placeholder

Description

%d

Direction, such as “above” or “below”

%n

Numbering

%o

Document name (only olinks)

%p

Page number, if applicable

%s

Subtitle, if available

%t

Title  

Note: Page numbers are not always meaningful
The problem with the placeholder %p is that some formats cannot provide a meaningful value. For example, template: Kapitel %n über %t auf Seite %p produces, for %p, an empty string in (X)HTML and confusing text in (X)HTML. If page numbers are still required, use the select method instead.

The select keyword

After select: specific keywords resolved by the stylesheets are expected. Free text is not allowed. Use this method to obtain reference formatting from the language files. The following example demonstrates this:

Im Kapitel <xref linkend="XPath" xrefstyle="select:labelnumber quotedtitle"/> erfahren Sie mehr zu diesem Thema.

Resolving the text produces:

Im Kapitel 13: "XPath" erfahren Sie mehr zu diesem Thema.

With the select method, the following components can be inserted:

  • Numbered element name (label)

  • Title

  • A page number, if possible

  • Document name for olink elements

Each component has keywords producing specific presentations, listed below:

Numbered name

selects a label identifying the element. Reference only elements with titles, since only they produce an unambiguous name.

Keyword

Example

Meaning

label

Section 16.6

full label and number

labelname

Section

label only

labelnumber

16.6

numbering only

  

Title

selects the referenced title, if available.

Keyword

Example

Meaning

title

Customizing cross-references

Title without quotation marks

quotedtitle

“Customizing cross-references”

Title in quotation marks

  

Page number

inserts different forms of a page number. This is useful only for XSL-FO, since online formats have no pages.

Keyword

Example

Meaning

unspecified

[389]

Default page format for all xref elements when insert.xref.page.number to yes is set. For other elements, this format is used if the parameter insert.link.page.number to yes is set.

page

(Page 389)

Page number in parentheses

Page

Page 389

capitalized page label

pageabbrev

(p. 389)

abbreviated label in parentheses

pagenumber

389

page number only

nopage

—

disables page numbers for this xref element if insert.xref.page.number to yes is set. For other elements, this applies if the parameter insert.link.page.number to yes is set.

  

Document name

inserts the document name. This is useful only for olink.

Keyword

Meaning

docname

inserts the document title referenced by olink (see also the same keyword in the language file)

docnamelong

inserts a longer form of the referenced document title (see also the same keyword in the language file)

nodocname

disables document-name insertion if the parameter olink.doctitle has the value yes.

 

Contents are a style name

This method is the easiest to write but requires language-file customizations. Its advantage is that schema customization can restrict the permitted values.

Proceed as follows:

  1. Create a language file, directly in the stylesheet or separately.

  2. Add your cross-reference style. Depending on the element, add further l:template elements:

<l:i18n xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0"> 
    <l:l10n language="de"> 
        <l:context name="xref-number-and-title"> 
            <l:template name="chapter" style="chapter-number-title" text="Kapitel %n: &#8220;%t&#8221;"/>
            <!-- ... --> 
        </l:context> 
    </l:l10n> 
</l:i18n> 

Line 4: style attribute: your style’s name; text attribute: contains replacement text including placeholders.

  1. Insert your style name wherever needed:

<xref linkend="XPath" xrefstyle="chapter-number-title"/> 

Note that this style is used only for a chapter reference. For a similar style on other elements, add an attribute for each element: style.

If you use different numbering, you may need an additional context for xref in your language file.

  

back to top