Chapter 1
EPUB Defined
image    Learn EPUB 2 standards for text and text e-readers
image    See how to form files within the EPUB
image    Learn some basic XML
image    Understand what files are necessary for EPUB
image    Discover what usable HTML tags to use in an EPUB
image    Be able to view the contents of an EPUB
image
The world of reading has forever changed. Paper books, magazines, and other such items have been evolving to a digital format. This affects not only readers, but writers as well. Electronic reading devices are popular, and with them, so are electronic books (e-books).
As a reader, you want to be able to enjoy a book visually. Little enhancements can make an e-book more pleasing. Changing fonts, adding images, and adjusting the text layout can be a few tricks you can use to “fix” an e-book. As a writer, you want to make the reader’s experience more enjoyable. To be able to keep the reader entertained and want to read more of your material is a definite plus.
In this chapter, you will learn about the EPUB version 2 standard, referred to as EPUB 2. You will understand where the standard is, as well as where it is going. Some standards are mainly for the black-and-white e-readers, while others are for the color e-readers. For example, some standards are compliant for the color e-readers, which allows for the use of video, text, and audio. Other e-readers may only allow for text. EPUB 2 is mainly for the black-and-white e-readers, but can be viewed on color devices as well.
EPUB Version 2 (EPUB 2)
Three basic parts comprise the EPUB 2 standard, as shown in Figure 1-1.
image
image
Figure 1-1   Three parts of EPUB
The three parts are as follows:
image    OEBPS Container Format (OCF)   Container holding all the files making up the publication. Collection of the individual files within a ZIP file, renamed to EPUB.
image    Open Packaging Format (OPF)   Describes and references the components within the publication. Includes content information, content list, content order, and the document parts.
image    Open Publication Structure (OPS)   Contents of the publication. Made up of XHTML/HTML, CSS, fonts, and images.
NOTE
image
Files within the EPUB can have an extension of HTML or XHTML. From now on, I will refer to both as XHTML unless otherwise needed.
Each part has specific requirements that you should be aware of when dealing with EPUB files. Be sure you understand the fundamentals for each section. Details follow for each section, with some discussed in greater depth in later chapters.
Open Container Format
From what you’ve seen in Figure 1-1, you may be wondering how all of this information is put into one file. It is actually very simple, but, of course, with many details and requirements. Don’t worry; most of the information is not used in most EPUB files.
The answer to this question is a ZIP file. A ZIP file is a single file that contains one or more files that are either stored or compressed. Stored files within the ZIP are “as-is” with no file size change, while the compressed files are written in such a way that they have a smaller file size.
The International Digital Publishing Forum (IDPF) states that the OCF is an abstract collection of files—the abstract container, which is the collection of various files and folders. This abstract container is inside a ZIP file, which is the physical container.
The abstract container has the following requirements:
image    The file system within the archive must have a single root directory.
image    All embedded files other than those required must be in a directory off the root.
image    Special files required by the OCF must be in the \META-INF directory (discussed later in this chapter).
NOTE
image
An EPUB file without a META-INF directory is corrupted. Each EPUB must have a META-INF directory to hold specific files required for the e-reader device. The only necessary file is container.xml, which specifies the OPF file location.
Key Word Usage
Many of the standards used by the IDPF and other organizations use words like “required,” “must,” “should,” etc. Sometimes these meanings can be a bit overwhelming when seeing them over and over. The following table designates their meanings:
Key Word Meaning
Must, Required, Shall Absolute requirement
Must Not Absolutely prohibited
Should, Recommend Should be followed, but can be ignored only with consideration
Should Not, Not Recommended Should not be followed, but can be implemented with consideration
May, Optional Can or cannot be implemented with discretion
The physical container has the following requirement:
image    The file system structure must be a one-to-one mapping just like on a hard disk or other storage media.
ZIP Requirements
Imagine that a ZIP file is a physical hard disk. When we copy files to it and view those files in a directory structure, we see the root directory and the files and folders. This listing is our abstract container. The ZIP file on the storage media is then our physical container.
Since our ZIP file is similar to a hard disk, it has one single root directory. There is a mandatory directory in the root called META-INF. Other files in the EPUB needed for display are in another directory from the root. Just like a hard disk, all files and directories are mapped one-to-one. This means there is only one META-INF directory. Each filename is used by only one file within the same folder.
Programs that produce ZIP files allow for the ZIP file to be split over various media. For instance, if you want to save a ZIP file to a DVD and the total file size requires more than one DVD, you can split the ZIP into multiple parts that would fill a DVD, with the rest on the next disk. EPUB files cannot be split in this manner and must be one complete ZIP file renamed with an EPUB extension.
EPUB files must be Flate compressed or Stored. Flate compression is the same as Deflate and is a lossless compression method. Lossless compression means that no information is lost from the original file and all data still exists. The Flate/Deflate method works best for text files and black-and-white/grayscale images. Stored data is when the files cannot or will not be compressed and are therefore only placed within the ZIP file in their original state. Stored files are not reduced in size, even though other files within the ZIP may be compressed. Other compression methods should generate an error by the reading device.
If the reading device or system supports ZIP64 extensions, you can use the ZIP64 compression format. The extension removes the limits of regular ZIP files on file sizes and the number of files within the archive, as shown in Table 1-1. This can drastically increase the number of files in a ZIP, as well as the sizes of the files being compressed.
image
Table 1-1   Differences in Standard and ZIP64 Formats
ZIP files can be encrypted from the archive program, requiring a password to access the contents. EPUB does not allow for encryption to be managed by the ZIP file. To use encryption, see a later section entitled “Encryption.XML.”
A ZIP file must encode filenames in the archive using UTF-8. UTF (Unicode Transformation Format) is a way of using 1 to 4 bytes to represent Unicode characters. The set of Unicode characters designates different text and script characters. Unicode not only supports text and script, but also punctuation, mathematical symbols, numbers, and so on. Each UTF character represents a value that corresponds to a Unicode value. The Unicode values represent a character from the world’s written languages. Unicode values can represent English letters, Greek letters, Hebrew script, and many more. For a more comprehensive list of languages, go to www.unicode.org/charts/.
The EPUB standard requires the Version Needed To Extract field in the ZIP header be 10, 20, or 45. The location of the 2-byte field is in the local header at offset 04 in the ZIP header. The values are shown with the version feature in Table 1-2. The EPUB file example shown in Figure 1-2 is a Deflate file.
image
Table 1-2   Version Needed Field
image
image
Figure 1-2    ZIP header value from hex editor
The compression method must be either 0 or 8. Any other value causes the EPUB to be in error. As you can see in Figure 1-3, the sample EPUB file shown has compression method 08 at offset 8. Value 0 means the files have been Stored in the archive, while 8 means the files have been “Deflated.” For reference purposes, other compression values are shown in Table 1-3. Remember that only the values 00 and 08 are valid.
image
Table 1-3    Compression Methods and Their Values
image
image
Figure 1-3    EPUB hex editor showing a deflated compression method
An EPUB with an archive decryption header or archive extra data record is invalid. As previously stated, the ZIP file must not control encryption. When a ZIP manages its encryption, the archive decryption header and archive extra data record contain the information about the encryption.
The first file placed in the ZIP root must be the mimetype file, with no extra spacing or changes made to the contents. The mimetype file must not be compressed or encrypted, and there should not be an extra field in the ZIP header. The extra field is used to store extra information for special application or platform requirements. If these items are not present, then the information from Table 1-4 will appear at the specified location of the EPUB, as shown in Figure 1-4. When this occurs, it is the “magic number” defined in RFC 2048. Of course, this may not always be the case, since not all EPUB files produce the “magic number,” as you can see looking back at Figures 1-2 and 1-3. All ZIP files should start with “PK” no matter what other settings have been changed.
image
Table 1-4   “Magic Number” Values and Placement
image
image
Figure 1-4    “Magic number” in EPUB
Now that we’ve covered the ZIP file as a whole, let’s look at the rules for inside the archive—namely, the directories and files. Directories are like folders, and files can be thought of as the papers within them. Of course, the ZIP file is the container in which the folders and papers are placed.
The root starts with a backslash (\), and all folders are separated with backslashes as well. Filenames also are separated from the directory names with a backslash. For example, if we were to note the path starting at the ROOT to the container.xml file, we would write it like this:
image
All files in the archive must be UTF-8 encoded and not exceed 255 bytes or characters. Paths must not exceed 65,535 bytes or characters.
Directory and filenames must not include any of the following:
image
Also, the filename cannot end with a period.
Some ZIP programs may support case sensitivity on filenames. This means the file container.xml is a different file from Container.xml. All files within a single folder must not have the same name after the filenames have all been lowercased or uppercased (this includes directory names as well).
NOTE
image
E-reader devices that support external memory cards, usually Secure Digital cards, support FAT-16 or FAT-32. SD cards are typically preformatted as FAT-32. The name limitations are based on this file system.
Depending on the language used (other than English), accent marks may be used. These must also be removed so no two files have the same name in the same directory. For example, it would see “Résumé” as “Resume.”
In most cases, it may be best to stick with ASCII codes for filenames and directory names. Otherwise, names may not be retained after they are uncompressed from the archive.
NOTE
image
The ASCII characters are those represented on a keyboard.
META-INF Requirements
We previously discussed the META-INF directory, which must be included in the EPUB file. Without it, the file is considered corrupted.
Within the META-INF directory, there are five optional files and one required file. We’ll cover the purpose of these files now, but the contents of the files will be explained later in this chapter and some other chapters.
manifest.xml   The manifest.xml file is an optional file only needed for backwards compatibility with OpenDocument Format (ODF).
The ODF is an open XML-based document format that is nonproprietary, allowing for easier sharing and conversion of data. An example application suite is LibreOffice.
If the manifest.xml file exists, it lists the contents of the EPUB file. The file must not be encrypted.
The manifest list is part of the OPF file and will be covered in the “Open Packaging Format (OPF)” section.
metadata.xml   The metadata.xml file is an optional file that contains the metadata about the publication. The XML file uses Dublin Core extensions to describe the title, author, publisher, etc. The data specified is for the container level, since it is part of the OCF. Metadata in the OPF is publication level.
NOTE
image
Dublin Core extensions will be covered a little later in this chapter.
If the metadata.xml file exists, it cannot be encrypted.
The metadata.xml file is part of the OPF file and will be covered in the “Open Packaging Format (OPF)” section.
signatures.xml   The signatures.xml file contains a list of files signed using Secure Hash Algorithm 1 (SHA-1). The algorithm is a fancy way to perform a calculation to generate a string of data called a hash. The value produced will be identically created each time the file is put through the algorithm. This allows a user to verify the files have not been changed or been corrupted.
If each file within the EPUB is signed individually, then each hash must be in signatures .xml. By having so many different hash values, the file will be larger. Multiple files may be signed together making the signatures.xml file smaller.
To add, remove, or change files in an EPUB without causing the file to appear corrupted, do not sign the signatures.xml file. If no files are to be added, removed, or changed, the signatures.xml file should be signed. If the signatures.xml file exists, it may not be encrypted.
Signatures.xml refers to EPUB security and will be covered more in Chapter 8.
encryption.xml   Some EPUB files are purchased on the Internet from various vendors, usually the vendor of your reading device. The books you buy are not supposed to be readily shared with others. To prevent this, the contents of the EPUB file are encrypted. As you have seen so far, no files within the META-INF directory can be encrypted, including encryption .xml. If any publications are encrypted, then the encryption.xml file specifies how to decrypt those files. If the file is deleted from an encrypted EPUB, the EPUB is corrupted.
Files are encrypted individually, but the whole container, excluding the META-INF folder, can be encrypted. When a file is encrypted, the whole file must be encrypted, not just a portion. If the files within the ZIP are compressed before they are encrypted, they must be Stored, not Flated.
Care needs to be taken when including fonts within an EPUB, as discussed in Chapter 4. If the font is to be protected from being freely shared, the font file should be encrypted as well. The procedure of encrypting the font is called font mangling.
Signatures.xml will be present when rights.xml exists and may not be encrypted.
rights.xml   The rights.xml file is an optional file that is used to specify licensing rights by encrypting the data. This procedure refers to Digital Rights Management (DRM), covered in Chapter 8. If the EPUB is DRM protected and the rights.xml file is removed, the EPUB is corrupted. The rights.xml file cannot be encrypted.
container.xml   The container.xml file is a required file. If the file is missing or encrypted, the EPUB is considered corrupted. The contents of the container.xml file point to the OPF file location that will be discussed in the next section.
Open Packaging Format (OPF)
The OPF is made up of two files. The first file is the .OPF file, which contains the description and references to the files within the OPS portion of the publication. It also fixes the order of the viewable files to ensure that everything is displayed in the proper order. The second file, the global navigation structure (NCX), provides a structure similar to a table of contents. It gives quick access to pages without requiring you to go through a publication page by page.
The OPF file is covered in detail in Chapter 6, while the NCX file is explained in Chapter 5.
Open Publication Structure (OPS)
Before we go on too far, let us discuss the devices. Available e-readers include the Barnes and Noble Nook, Kobo, the Apple iPad, etc. There are also PC apps for Barnes and Noble Nook, Kobo, etc. So an e-reader is any device that allows a reader to read an electronic book (e-book).
What About the Kindle?
The Amazon Kindle is a popular e-reader that, unfortunately, does not support the EPUB standard. Do not worry about this, though. There are easy ways to convert an EPUB to the Kindle format, known as AZW, covered in Chapter 7. For most people, it is easier to create and manipulate an EPUB file and then convert it to AZW.
The OPS is made up of files containing the viewable content of the publication. Basically, it is what you will see on the e-reader’s screen. With EPUB files, this content comes from Extensible Hypertext Markup Language (XHTML) files. The content can be visually manipulated with one or more Cascading Style Sheets (CSS). Finally, the last part of the OPS is image files and fonts. The images can be in PNG, JPG (JPEG), GIF, and SVG formats. Fonts may be in TTF, OTF, or SVG formats (some reading systems may support other formats, such as WOFF or EOT).
What Is SVG?
Scalable Vector Graphics (SVG) is a standard created by the World Wide Web Consortium (W3C). SVG can be fonts, images, and animation. Fonts can be smoother since they are made up of shapes rather than points. Animation can be controlled by JavaScript in EPUB 3 only.
All SVG files are based on XML and can, therefore, be viewed in a text editor. Since they are text-based files, they can be compressed a lot in the ZIP file. By compressing SVG files, the EPUB files are smaller than when using other fonts or image types (examples are given in Chapter 4).
EPUB 2 supports SVG fonts and images. SVG images that have embedded text can be searched with the publication text. SVG images can be also be used to contain links for navigation purposes.
Be aware that some e-readers may not support SVG even though it is part of the EPUB standard.
All the OPS files work together to create the viewable material comprising the publication or e-book. By using XHTML, even though not all elements and attributes are supported, most have CSS values to replace them. The main thing to remember is the text is supposed to flow and will be viewable on various devices. Having the text and images flow is similar to a webpage, which can be viewed in various screen sizes and resolutions.
NOTE
image
Device screen size may vary from a larger device such as a Nook or iPad to a smartphone, but the screen resolution is a minimum of 600 × 800 pixels.
There are various requirements for OPS and XHTML documents:
image    It is a well-formed XML document.
image    Encoded in UTF-8 or UTF-16.
image    It is a valid XML document according to the Namespace-based Validation Dispatching Language (NVDL) schema.
image    MIMETYPE of application/xhtml+xml or text/x-oebl-document (deprecated).
image    XHTML elements and attributes not contained in the inline XML island are drawn from the XHTML subset
Requirements for the DTBook are as follows:
image    It is a well-formed XML document.
image    Encoded in UTF-8 or UTF-16.
image    It is a valid XML document.
image    MIMETYPE of application/x-dtbook+xml.
The out-of-line XML document requirements are as follows:
image    It is a well-formed XML document.
image    Encoded in UTF-8 or UTF-16.
image    MIMETYPE of:
image    application/xhtml+xml (not an extended module)
image    text/x-oebl-document
image    application/x-dtbook+xml
CSS requirements for the OPS are as follows:
image    The external CSS style sheet must be referenced by the OPS document.
image    Encoded in UTF-8 or UTF-16.
image    UTF-16 requires a byte order mark (BOM).
image    UTF-8 has an optional BOM.
The reading system requirements are as follows:
image    Correctly process XML as required by XML 1.0 specifications, including handling errors
image    Recognize permitted markups and process them consistently
image    Must not render img or object elements of unsupported media types in the absence of fallbacks
image    Verify existence of appropriate namespace specifications
image    Correctly process CSS style sheets
What Is Namespace-based Validation Dispatching Language (NVDL)?
NVDL is a set of rules used to allow an XML file to be validated to assure that the XML tags are correct according to the namespace being used.
What Is DAISY or DTBook?
Before EPUB, there was the DAISY Consortium. Digital Accessible Information SYstem (DAISY) created the DTBook, or DAISY Digital Talking Book, for people with accessibility issues. The Open eBook (OEB), formerly the Open eBook Publication Structure (OEBPS) 1.2, became EPUB 2.0, and now the DAISY Consortium has joined EPUB to create EPUB 3.
The OPS standard requires the use of XHTML processing of the elements listed in Table 1-5. The items in the table will be detailed in Chapter 2.
image
Table 1-5   OPS XHTML Elements
NOTE
image
If you do not know XHTML, then Chapter 2 will cover those details and get you up to speed. For now, just realize that these are the elements used to create the text body of the book as well as to manipulate images.
Since the OPS was taken from OEB (later OEBPS), there is a resemblance to DAISY DTBooks. Navigation to chapters and individual pages can allow a reader to move around in the book easily. DTBook content must be validated to the DTBook XML vocabulary. Any elements dealing with Synchronized Multimedia Integration Language (SMIL) are not allowed in EPUB 2. SMIL is the multimedia component of DAISY, but EPUB 2 does not handle multimedia, and therefore is not allowed. For more information on multimedia support, see Chapters 8 and 9.
Some basic changes from OEBPS 1.2 to OPS 2.0 meant that XML namespace processing is now required. The support for SVG has been added, as well as embedded fonts. In addition, some MIME types were changed.
NOTE
image
Any MIME types that were dropped from DTBooks are not mentioned in this book. If the element was deprecated, then there is no need to learn it for EPUB.
OPS supports Cascading Style Sheets (CSS), an external file to store styles for XHTML code, but not all CSS items are supported on all reading systems. For example, devices that do not have color screens cannot support color elements. The color elements can be rendered in shades of gray, however, to emulate color differences.
OPS supports various MIME types. MIME, or Multipurpose Internet Mail Extensions, was originally used to designate file formats over the Internet. It was specifically used by SMTP (Simple Mail Transport Protocol) for e-mail attachments. After being used with protocols such as HTTP and others, it has expanded to other protocols.
MIMETYPE is usually referred to as Media Type now and exists for different types (see Table 1-6).
image
Table 1-6   MIME Types
There is a whole list of subtypes, some of which are supported within an EPUB. Table 1-6 lists types and subtypes that are supported in EPUB 2. The items in Table 1-7 give you an idea of what file types can be used to create EPUB, which are covered in detail in Chapter 6.
image
Table 1-7   OPS MIMETYPE and Usage
I know you are probably thinking that this is a lot of information to throw out all at once. Believe me, it isn’t as bad as it seems. Through the rest of this chapter and those that follow, we will cover these standards in greater detail. Be patient with it, and go over each chapter and learn the subjects. As you go through various EPUB files, these things will become second nature to you.
What Is an EPUB?
An EPUB file is a collection of files within a ZIP file renamed to EPUB. This means that any EPUB file can be opened with a ZIP archival application. Depending on the program, you may need to rename the file’s extension from EPUB to ZIP. I will give a shortcut momentarily for Windows XP systems that allows you to open an EPUB file without renaming it.
Various chapters of this book cover the different files within the EPUB. Some you may need to edit at times, and others you may never need to change at all. It is best to understand what these files do so you can be sure not to modify or delete them, which can cause problems.
Preparing to View the Contents of an EPUB File
Before we get too far into the standards, let’s look at the contents of an EPUB file. This will help illustrate what we are covering as we go along.
At this point, you need to make sure you have a ZIP compression utility. I suggest 7-Zip since it is freeware. I will cover some basics to set things up in the Windows context menu, which is what appears when you right-click an EPUB file. As noted in the introduction, you can download 7-Zip from www.7-zip.org. Download and install the application, and then perform the following steps to create the context menu on a Windows system.
NOTE
image
Be sure you have installed Sigil as well. You can find it at http://code.google.com/p/sigil/. For Linux, go to the address for your specific distribution of Linux:
Debian-Based Linux   https://launchpad.net/~rgibert/+archive/ebook/+packages/
Fedora   http://rpms.limbasan.ro/fedora/
ArchLinux   https://www.archlinux.org/packages/?q=sigil
Slackware   http://connie.slackware.com/~alien/slackbuilds/sigil/
OpenSuse   https://build.opensuse.org/package/repositories?package=sigil&project=home%3Axwaver
Gentoo   http://gentoo-portage.com/app-text/sigil
Windows 7
  1.  Open Control Panel, select Programs, and then select Default Programs.
  2.  Select Associate A File Type Or Protocol With A Program.
  3.  Select EPUB (if EPUB is not in the list, close the open windows, install SIGIL, and then restart from step 1).
  4.  Select Change Program.
  5.  Browse for 7-Zip and then click OK (unless you use a different archive program).
  6.  Click Close.
  7.  Find an EPUB file and right-click it.
  8.  From the menu that opened, select Open With.
  9.  You can choose Sigil to edit the file or 7-Zip to open the archive (or your archive program instead of 7-Zip).
Windows XP
  1.  Open My Computer.
  2.  Click Tools and select Folder Options.
  3.  Once the Registered File Types list is populated, scroll down to find ZIP.
  4.  Highlight ZIP and select the Advanced button.
  5.  Highlight Open under Actions and then click the Edit button.
  6.  Under Application To Perform Action, you should see C:\Program files\7-Zip\7zFM.exe%1. Make a note of the exact line (the drive letter may be different depending on where you installed it).
  7.  Select Cancel twice to get back to the Folder Options screen.
  8.  Scroll and select EPUB and then click the Advanced button (if EPUB is not in the list, close the open windows, install SIGIL, and then restart from step 1).
  9.  In the Actions section, you should see an entry for Open. Select the New button.
10.  Under Actions, type View Contents.
11.  Under Application To Perform Action, enter the information you noted in step 6.
12.  Check the option to use DDE, and the box should expand.
13.  Under Application, type in the 7-Zip filename: 7zFM.exe.
NOTE
image
If you use a different ZIP program, make sure that in step 13 you use the correct filename for your application.
14.  Under Topic, type System.
15.  Select OK and close all windows you have opened for these steps.
Linux
For Linux systems, you want to use either Ark or Xarchiver as your ZIP viewer. Follow the instructions next for your specific file manager.
Nautilus
  1.  First, right-click the EPUB and select Properties.
  2.  Next, click the Open With tab.
  3.  Finally, choose an EPUB-compatible archiving program.
Dolphin and Konqueror
  1.  First, right-click the EPUB and select Properties.
  2.  Then, click the only icon by the Type property.
  3.  Next, set the application preference order for opening files with the EPUB extension.
Thunar
  1.  First, right-click the EPUB file and select Properties.
  2.  Finally, choose the default program to use when opening EPUB files.
X File Explorer (XFE)
  1.  Before beginning, open a command line and type which name of preferred archive manager. This will return the file path of the executable. Write this down, or copy it to the clipboard. For example:
image    User input: which ark
image    System output: /usr/bin/ark
  2.  After that, right-click the EPUB and click Properties.
  3.  Then, click the File Associations tab.
  4.  Last, paste or type the system output file path into the Open line, and finish by clicking Accept.
Viewing the Contents of an EPUB File
Now, you need to download an EPUB file to test this new feature. Use your browser and go to www.ManyBooks.net. On the first page, type Household Tales in the Search box. Download the EPUB file when you have the option and save it to an accessible folder. For Windows, right-click the file and you should see an option toward the top of the list— View Contents. Select View Contents and 7-Zip should open. This context menu should now work on all EPUB files. If it doesn’t work, go back through the steps and try again. For Linux systems, right-click, select Open With, and choose the archiver you installed.
The archiver program you installed should display a folder and 21 objects (unless it is an updated file); these are the contents of the EPUB file.
You should see something similar to Figure 1-5, depending on your archiver program. As you can see, most of the files are .html; in some cases, .xhtml. These are the heart of the text displayed on the e-reader. Image files make up the cover image, book illustrations, pictures, etc. The .css file is the Cascading Style Sheet, which enhances the XHTML pages. Everything else is used to create the EPUB file and determine the reading order of the files, table of contents, and much more.
image
image
Figure 1-5   Contents of Household Tales EPUB
XML Basics
Most of these items are covered in later chapters. Here I will discuss the container.xml, manifest.xml, metadata.xml, and mimetype files. In Chapter 7 we will cover the other XML files: signatures.xml, rights.xml, and encryption.xml. First, we need to cover some basics of XML.
EPUB uses a few XML files, and a basic knowledge of XML is required to manipulate some of the files. Sigil and other programs can do this for you, but sometimes it is easier to manage the files yourself, or at least is better to understand them.
Extensible Markup Language (XML) is used to describe data. XML is used in various places within the EPUB file. Tags are placed within chevrons (< >), and most tags have a beginning tag to designate the beginning of a section. Each section ends with a closing tag, which starts with a forward slash before the tag name (</>). XML tags are case-sensitive, can contain no spaces, and typically are lowercase.
For example, let’s suppose we had a database we wanted to create for a book catalog. Of course, there would be more information, but this is just a basic example.
image
As you can see, there is an overall tag for <catalog> and a matching end tag. This designates the catalog as a whole, and everything between the two tags are within the <catalog>. Within the main tag are nested tags—tags that are inside other tags. In this case, the <book> tags are nested within <catalog>. Each <book> has nested information about a single book. Here the first book is King Solomon’s Mines and the second is Journey to the Center of the Earth. Each of these books includes <title>, <author>, and <isbn>. Notice the information within the tag itself (< >) is all lowercased. Tag names are case-sensitive, so the start tag and end tag must be identical in case. Data included between the tags can be in any required case needed, as shown in the example.
Nested tags are called children. <book> is a child tag while <catalog> is a parent. <title>, <author>, and <isbn> are siblings of one another and children of <book>.
Now to access this XML file from an application, the application must understand what the tags mean. To specify the tag meanings, or XML vocabulary, we use an XML namespace (xmlns).
Now that you should have a basic understanding of XML, we can look at the container .xml file that is part of the OCF.
container.xml
The container.xml file is required to show the OPF file location—in this case, content.opf. Chapter 6 covers the OPF file.
When using Sigil to create or edit EPUB files, the program manages the container.xml file. If you should change something manually, you need to know how it all works.
The container.xml file must be contained in a directory named META-INF. If this directory or file does not exist, the EPUB is considered corrupted and cannot be viewed on any device or edited in Sigil. To fix the problem, you will need to re-create the file manually.
The content of the container.xml for Household Tales is as follows:
image
The file can be the same from any EPUB, except for the fourth line. The full-path points to the OPF file location, which can have any valid filename, but must have the OPF extension.
The beginning of the first line, <?xml version="1.0", is standard for all XML files. The first line declares to any program using it that it is XML and version 1.
The second part of the first line, encoding="UTF-8"?>, shows the file uses UTF-8.
EPUB requires UTF-8 or UTF-16, but some e-reader devices may not support UTF-16. It is best to stick with UTF-8 as often as you can. If you need to use special characters, then you will want to include the suitable font file. Chapter 4 covers adding font files.
The second line shows the container version is “1.0,” which is mandatory. Then the next part, xmlns="urn:oasis:names:tc:opendocument:xmlns:container", shows the namespace used by XML, which will never need to be changed for the container.xml file. The only thing that may ever change in the file is the line <rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>, which shows the OPF file location.
The <rootfiles> section must contain one <rootfile> element that specifies the OPF location for the publication. It is possible to include more than one <rootfile> element, but this is for fallback purposes only. For example:
image
If, for some reason, the reading system cannot manage the OPF correctly, it should display the PDF, if possible, as a fallback. Be aware that in this case, the reading device must be able to handle EPUB to be able to get this far into the container.xml.
manifest.xml
The manifest.xml file is optional, as most of its content has been moved to the OPF. Following is a sample manifest.xml file using basic information for the manifest list:
image
As we noted before about a fallback, we can include this in the manifest.xml file as well. If, for some reason, we have a nonviewable file—say it is missing or corrupted—we can specify a fallback. Say we have each section of XHTML files saved as a PDF file; we can then call the PDF in case the XHTML fails. The following example has two sections for the first XHTML file. The first has an ID of “section1,” which is for the HTML file. The section also shows the file’s media type as application/xhtml+xml. Also included is a fallback attribute for a section ID called “section1pdf.” The second section, the fallback, has an ID of “section1pdf” as noted from the fallback of the previous section. Here is a reference to the PDF file with the appropriate mimetype. Since there is no fallback for this section, that attribute is missing. Of course, we should have a section for each file listed in the previous sample of the manifest.xml with a fallback for each. I hope you can see how this works. Remember that if a fallback is listed for the PDF file, it cannot point back to the first section. The fallbacks may not be circular in nature. If all sections fail, then the reading device must be able to gracefully handle this without failure.
image
Keep in mind that the manifest.xml file is for backwards compatibility; this information will be repeated in Chapter 6 for the OPF file.
Any files within the EPUB not listed in the manifest.xml (or the manifest of the OPF) cannot be displayed as part of the publication.
metadata.xml
Like the manifest.xml file, this is also an optional file that is for backwards compatibility since it is now contained in the OPF file.
Metadata is data about the publication. It is a set of 15 properties created by the Dublin Core Metadata Initiative (DCMI). It was named Dublin since it met at an invitational workshop in Dublin, Ohio. The “Core” is based on the fact the references are for a broad variety of resources. So Dublin Core is abbreviated as DC, as you will see in the examples.
Table 1-8 shows the 15 properties.
image
Table 1-8   Dublin Core Properties
For example, you can specify the language of the e-book as English by using the following line:
image
The following example of the metadata.xml file shows the properties for language, creator, and title:
image
The Dublin Core properties will be covered in greater detail in Chapter 6 since this data appears in the OPF file.
mimetype
The mimetype file must be in the root of the EPUB, and its content is application/epub+zip. The first part shows the EPUB file is a media type of application with a subtype of EPUB and ZIP. The contents must not be changed or added to with spaces or any other text or symbols.
Please note that even though the IDPF requires the mimetype file in the root with no changes, some reading systems may still show the EPUB anyway. It is best to leave this file as it should be and not try to modify it in case some systems may not handle it properly.
The Internet Assigned Numbers Authority (IANA) controls all media types. For more information, visit www.iana.org/assignments/media-types/index.html. You can also get a comprehensive list of subtypes for each media type.