7.7.Â Using Entities to Include Files

Both
general and
parameter
entities are particularly useful for including one file inside
another.

7.7.1.Â Using General Entities to Include Files

Consider some content for an XML book
organized into files, one file per chapter, called
chapter1.xml,
chapter2.xml, and so forth, with a
book.xml that will contain these
chapters.

In order to use the contents of these files as the values
for entities, they are declared with the
SYSTEM keyword. This directs the
XML parser to include the contents of the
named file as the value of the entity.

Warning:

When using general entities to include other files
within a document, the files being included
(chapter1.xml,
chapter2.xml, and so on)
must not start with a DOCTYPE
declaration. This is a syntax error because entities are
low-level constructs and they are resolved before any
parsing happens.

7.7.2.Â Using Parameter Entities to Include Files

Parameter entities can only be used inside an
XML context. Including a file in an
XML context can be used
to ensure that general entities are reusable.

Suppose that there are many chapters in the document, and
these chapters were reused in two different books, each book
organizing the chapters in a different fashion.

The entities could be listed at the top of each book, but
that quickly becomes cumbersome to manage.

Instead, place the general entity definitions inside one
file, and use a parameter entity to include that file within
the document.

ExampleÂ 7.11.Â Using Parameter Entities to Include Files

Place the entity definitions in a separate file
called chapters.ent and
containing this text:

Create a parameter entity to refer to the contents
of the file. Then use the parameter entity to load the file
into the document, which will then make all the general
entities available for use. Then use the general entities
as before: