Lably
Skip to content

DocBook

Markup language for documentation

  • 9 min read
  • 6 chapters

Related on Lably

Through Documentation, HTML and Abstract (summary)

As a semantic language, DocBook enables its users to create document content in a presentation-neutral form that captures the logical structure of the content; that content can then be published in a variety of formats, including HTML, XHTML, EPUB, PDF, man pages, WebHelp and HTML Help, without requiring users to make any changes to the source. In other words, when a document is written in DocBook format it becomes easily portable into other formats, rather than needing to be rewritten.

Design

DocBook is an XML language. In its current version (5.x), DocBook's language is formally defined by a RELAX NG schema with integrated Schematron rules. (There are also W3C XML Schema+Schematron and Document Type Definition (DTD) versions of the schema available, but these are considered non-standard.)

As a semantic language, DocBook documents do not describe what their contents "look like", but rather the meaning of those contents. For example, rather than explaining how the abstract for an article might be visually formatted, DocBook simply says that a particular section is an abstract. It is up to an external processing tool or application to decide where on a page the abstract should go and what it should look like or whether or not it should be included in the final output at all.

DocBook provides a vast number of semantic element tags. They are divided into three broad categories, namely structural, block-level, and inline.

Structural tags specify broad characteristics of their contents. The book element, for example, specifies that its child elements represent the parts of a book. This includes a title, chapters, glossaries, appendices, and so on. DocBook's structural tags include, but are not limited to:

  • set: Titled collection of one or more books or articles, can be nested with other sets
  • book: Titled collection of chapters, articles, and/or parts, with optional glossaries, appendices, etc.
  • part: Titled collection of one or more chapters—can be nested with other parts, and may have special introductory text
  • article: Titled, unnumbered collection of block-level elements
  • chapter: Titled, numbered collection of block-level elements—chapters don't require explicit numbers, a chapter number is the number of previous chapter elements in the XML document plus 1
  • appendix: Contains text that represents an appendix
  • dedication: Text represents the dedication of the contained structural element

Structural elements can contain other structural elements. Structural elements are the only permitted top-level elements in a DocBook document.

Block-level tags are elements like paragraph, lists, etc. Not all these elements can directly contain text. Sequential block-level elements render one "after" another. After, in this case, can differ depending on the language. In most Western languages, "after" means below: text paragraphs are printed down the page. Other languages' writing systems can have different directionality; for example, in Japanese, paragraphs are often printed in downward columns, with the columns running from right to left, so "after" in that case would be to the left. DocBook semantics are entirely neutral to these kinds of language-based concepts.

Inline-level tags are elements like emphasis, hyperlinks, etc. They wrap text within a block-level element. These elements do not cause the text to break when rendered in a paragraph format, but typically they cause the document processor to apply some kind of distinct typographical treatment to the enclosed text, by changing the font, size, or similar attributes. (The DocBook specification does say that it expects different typographical treatment, but it does not offer specific requirements as to what this treatment may be.) That is, a DocBook processor doesn't have to transform an emphasis tag into italics. A reader-based DocBook processor could increase the size of the words, or, a text-based processor could use bold instead of italics.

Sample document

<span id="mwUw"></span><span class="w" id="mwVA"> </span><span class="cp" id="mwVQ"><?xml version="1.0" encoding="UTF-8"?></span> <span class="w" id="mwVg"> </span><span class="nt" id="mwVw"><book</span><span class="w" id="mwWA"> </span><span class="na" id="mwWQ">xml:id=</span><span class="s" id="mwWg">"simple_book"</span><span class="w" id="mwWw"> </span><span class="na" id="mwXA">xmlns=</span><span class="s" id="mwXQ">"http://docbook.org/ns/docbook"</span><span class="w" id="mwXg"> </span><span class="na" id="mwXw">version=</span><span class="s" id="mwYA">"5.0"</span><span class="nt" id="mwYQ">></span> <span class="w" id="mwYg"> </span><span class="nt" id="mwYw"><title></span>Very<span class="w" id="mwZA"> </span>simple<span class="w" id="mwZQ"> </span>book<span class="nt" id="mwZg"></title></span> <span class="w" id="mwZw"> </span><span class="nt" id="mwaA"><chapter</span><span class="w" id="mwaQ"> </span><span class="na" id="mwag">xml:id=</span><span class="s" id="mwaw">"chapter_1"</span><span class="nt" id="mwbA">></span> <span class="w" id="mwbQ"> </span><span class="nt" id="mwbg"><title></span>Chapter<span class="w" id="mwbw"> </span>1<span class="nt" id="mwcA"></title></span> <span class="w" id="mwcQ"> </span><span class="nt" id="mwcg"><para></span>Hello<span class="w" id="mwcw"> </span>world!<span class="nt" id="mwdA"></para></span> <span class="w" id="mwdQ"> </span><span class="nt" id="mwdg"><para></span>I<span class="w" id="mwdw"> </span>hope<span class="w" id="mweA"> </span>that<span class="w" id="mweQ"> </span>your<span class="w" id="mweg"> </span>day<span class="w" id="mwew"> </span>is<span class="w" id="mwfA"> </span>proceeding<span class="w" id="mwfQ"> </span><span class="nt" id="mwfg"><emphasis></span>splendidly<span class="nt" id="mwfw"></emphasis></span>!<span class="nt" id="mwgA"></para></span> <span class="w" id="mwgQ"> </span><span class="nt" id="mwgg"></chapter></span> <span class="w" id="mwgw"> </span><span class="nt" id="mwhA"><chapter</span><span class="w" id="mwhQ"> </span><span class="na" id="mwhg">xml:id=</span><span class="s" id="mwhw">"chapter_2"</span><span class="nt" id="mwiA">></span> <span class="w" id="mwiQ"> </span><span class="nt" id="mwig"><title></span>Chapter<span class="w" id="mwiw"> </span>2<span class="nt" id="mwjA"></title></span> <span class="w" id="mwjQ"> </span><span class="nt" id="mwjg"><para></span>Hello<span class="w" id="mwjw"> </span>again,<span class="w" id="mwkA"> </span>world!<span class="nt" id="mwkQ"></para></span> <span class="w" id="mwkg"> </span><span class="nt" id="mwkw"></chapter></span> <span class="w" id="mwlA"> </span><span class="nt" id="mwlQ"></book></span>

Semantically, this document is a "book", with a "title", that contains two "chapters" each with their own "titles". Those "chapters" contain "paragraphs" that have text in them. The markup is fairly readable in English.

In more detail, the root element of the document is book. All DocBook elements are in an XML Namespace, so the root element has an xmlns attribute to set the current namespace. Also, the root element of a DocBook document must have a version that specifies the version of the format that the document is built on.

(XML documents can include elements from multiple namespaces at once, like the id attributes in the example.)

A book element must contain a title, or an info element containing a title. This must be before any child structural elements. Following the title are the structural children, in this case, two chapter elements. Each of these must have a title. They contain para block elements, which can contain free text and other inline elements like the emphasis in the second paragraph of the first chapter.

Schemas and validation

Rules are formally defined in the DocBook XML schema. Appropriate programming tools can validate an XML document (DocBook or otherwise), against its corresponding schema, to determine if (and where) the document fails to conform to that schema. XML editing tools can also use schema information to avoid creating non-conforming documents in the first place.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication