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 |
| Direction, such as “above” or “below” |
| Numbering |
| Document name (only |
| Page number, if applicable |
| Subtitle, if available |
| 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
olinkelements
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
labelSection 16.6
full label and number
labelnameSection
label only
labelnumber16.6
numbering only
- Title
selects the referenced title, if available.
Keyword
Example
Meaning
titleCustomizing 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
xrefelements wheninsert.xref.page.numbertoyesis set. For other elements, this format is used if the parameterinsert.link.page.numbertoyesis set.page(Page 389)
Page number in parentheses
PagePage 389
capitalized page label
pageabbrev(p. 389)
abbreviated label in parentheses
pagenumber389
page number only
nopage—
disables page numbers for this
xrefelement ifinsert.xref.page.numbertoyesis set. For other elements, this applies if the parameterinsert.link.page.numbertoyesis set.
- Document name
inserts the document name. This is useful only for
olink.Keyword
Meaning
docnameinserts the document title referenced by
olink(see also the same keyword in the language file)docnamelonginserts a longer form of the referenced document title (see also the same keyword in the language file)
nodocnamedisables document-name insertion if the parameter
olink.doctitlehas the valueyes.
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:
Create a language file, directly in the stylesheet or separately.
Add your cross-reference style. Depending on the element, add further
l:templateelements:
<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: “%t”"/>
<!-- ... -->
</l:context>
</l:l10n>
</l:i18n> Line 4:
styleattribute: your style’s name;textattribute: contains replacement text including placeholders.
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.
Copyright © 2009 Millin Verlag
You may print the online version for your private use.
Otherwise, this chapter from "DocBook-XML: Medienneutrales und plattformunabhängiges Publizieren" is subject to the same provisions as the printed edition: the work, including all its parts, is protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Millin Verlag, Siebengebirgsring 36, 53797 Lohmar, info(at)millin.de