FictionBook describes the structure of a text, not its look: a reader decides how
titles, poems and quotations appear. The elements below are those of the FictionBook 2
schema; each must appear in the order given. Most of them may carry
xml:lang (an xs:language value such as ru); the
ones that cannot are epigraph, image, binary,
stylesheet, table, tr, a,
empty-line and author itself.
Most carry an id that links can point to. An id is an
xs:ID: an XML name (it starts with a letter or an underscore, with no spaces) and
unique in the whole file. The attribute xlink:type, where an element has it,
is optional and fixed to simple: any other value is invalid.
Where an element appears in two places (image as a block and inline,
publisher in document-info and in publish-info) the two
have different content, as the tables below show.
body
| Child | Number | Content |
|---|---|---|
image | 0–1 | A picture shown before the text. |
title | 0–1 | The title of the body, often the book’s title and author. |
epigraph | Any | Epigraphs for the whole book. |
section | 1 or more | The text. |
The first body is the main text. A second, with
name="notes", holds footnotes and endnotes, one section with
an id per note; links in the text point to them with
type="note". Older files sometimes have further bodies (for example
name="comments"), which most readers accept and show after the text.
section
A chapter or other division. Sections nest to any depth.
| Child | Number | Content |
|---|---|---|
title | 0–1 | The heading. |
epigraph | Any | Epigraphs. |
image | 0–1 | A picture at the start. |
annotation | 0–1 | A summary of the section. |
either section | 1 or more | Subsections only, or… |
| or block content | 1 or more | …text: p, poem, subtitle, cite, empty-line, table, and, after the first block, image. |
A section holds either subsections or text, never both. Attributes: id,
xml:lang. The schema makes the whole of this content optional as one
group (xs:sequence minOccurs="0"), so an empty
<section/> is valid. A section with only a title (or
only an image or annotation) is not: once any child is
present, subsections or block content must follow.
Block elements
| Element | Content | Attributes | Meaning |
|---|---|---|---|
p | text and inline elements | id, style, xml:lang | A paragraph. The style attribute is any string: the spec’s description speaks of a style for paragraphs (a name); calibre copies the value into the HTML style attribute, as CSS declarations. |
empty-line | empty | Extra vertical space, such as a scene break. The description says it has no content; the schema gives it no type, so the schema would accept content. calibre writes a line break and ignores any content. | |
subtitle | as p | as p | A heading inside a section that does not start a new section. |
title | p and empty-line, any number | xml:lang | A heading of a body, section, poem or stanza; each line is a p. |
epigraph | p, poem, cite, empty-line, then text-author elements | id | An epigraph and who wrote it. |
annotation | p, poem, cite, subtitle, table, empty-line | id, xml:lang | A summary. |
cite | p, poem, empty-line, subtitle, table, then text-author elements | id, xml:lang | A quotation, such as a letter or document quoted in the text. |
text-author | as p | as p | The author of an epigraph, quotation or poem. |
poem | title (0–1), epigraph (any), subtitle or stanza (1 or more), text-author (any), date (0–1) | id, xml:lang | A poem or song. |
stanza | title (0–1), subtitle (0–1), v (1 or more) | xml:lang | A stanza. |
v | as p | as p | A line of verse. |
date | text | value, xml:lang | When the poem was written. |
image | empty | xlink:href, xlink:type, alt, title, id | A picture as a block of its own. |
Tables
| Element | Content | Attributes |
|---|---|---|
table | tr, 1 or more | id, style |
tr | th or td, 1 or more | align: left (default), right or center |
th, td | as p | id, style, colspan, rowspan, align (as above), valign: top (default), middle or bottom, xml:lang |
colspan and rowspan are xs:integer: the schema sets
no range, so a value of 0 or a negative one is valid FictionBook, though a reader has
nothing sensible to do with it. A viewer that follows the schema (as this site’s does)
reports only a value that is not an integer. In HTML a span must be a positive whole number, so the explorer draws a span only when the value is 2 or more.
Programs differ: the schema gives th and td a valign (in 2.1 and later) and the tr an
align; calibre keeps align, style, colspan and rowspan on cells and align on rows
but drops valign; the legacy FBReader has no table element at all. See Divergence from spec.
Inline elements
Paragraph-like elements (p, v, subtitle,
text-author, th, td) hold text mixed with:
| Element | Attributes | Meaning |
|---|---|---|
strong | Strong emphasis, usually bold. | |
emphasis | Emphasis, usually italic. | |
strikethrough | Struck-out text. | |
sub, sup | Subscript and superscript. | |
code | Computer code, in a fixed-width font. | |
style | name (required) | Text with a named style from a stylesheet. The spec says: a style name. calibre makes it a span with that class, so the stylesheet’s CSS applies; the legacy FBReader ignores the element and shows its text. |
a | xlink:href (required), xlink:type, type | A link: #id inside the book, or a URL. With type="note" it is a note reference, and readers show the note’s section, usually as a pop-up. Links cannot nest. |
image | xlink:href, xlink:type, alt | A picture inside the line. |
The style elements nest inside one another and inside a. In the schema
all of strong, emphasis, strikethrough,
sub, sup and code have the same type
(styleType), and carry one optional attribute, xml:lang;
style adds the required name. The cells td and
th are paragraph-like and carry xml:lang too. a
has no xml:lang.
binary
Images are stored inside the file, after the bodies, one binary element
each:
| Attribute | Status | Meaning |
|---|---|---|
id | Required | The name images refer to, such as cover.jpg. An xs:ID, so an XML name, unique in the file. |
content-type | Required | The media type of the data. The schema types it xs:string: any string is valid, and the data is opaque Base64 (xs:base64Binary). In practice it is image/jpeg or image/png, the types readers show; image/gif and others occur, and readers differ in what they accept. The author’s 2.0 comments page says the programs of the time handled only PNG and JPG files, while a file of any kind may be embedded; Foliate’s fb2.js (line 89) builds a data: URL from the binary’s content-type whatever it is and leaves the display to the browser. |
The content is the data in Base64, usually broken into lines; in practice, a picture. An image element
refers to it with a local link: <image l:href="#cover.jpg"/>. Links
to files outside the book are allowed by the schema but rarely supported.
Example
<body>
<section id="ch1">
<title><p>Chapter One</p></title>
<epigraph>
<p>All happy families are alike.</p>
<text-author>Leo Tolstoy</text-author>
</epigraph>
<p>It was <emphasis>very</emphasis> cold.<a l:href="#n1" type="note">[1]</a></p>
<empty-line/>
<poem>
<stanza>
<v>The snow fell soft</v>
<v>on every roof.</v>
</stanza>
</poem>
</section>
</body>
<body name="notes">
<section id="n1"><title><p>1</p></title><p>Minus twenty degrees.</p></section>
</body>
<binary id="cover.jpg" content-type="image/jpeg">/9j/4AAQSkZJRgABAQ…</binary>