Your support for our advertisers helps cover the cost of hosting, research, and maintenance of this FAQ
The XML FAQ — Frequently-Asked Questions about the Extensible Markup Language
Section 3: Authors
Q 3.20: How do I include one XML file in another?
Use a general entity, same as for SGML, or use XInclude.
One method is to use Document Entities, which work exactly the same as for SGML, but require a DTD or internal subset. First you declare the entity you want to include, and then you refer to it by name as an Entity Reference:
<?xml version="1.0"?> <!DOCTYPE novel SYSTEM "/dtd/novel.dtd" [ <!ENTITY chap1 SYSTEM "mydocs/chapter1.xml"> <!ENTITY chap2 SYSTEM "mydocs/chapter2.xml"> <!ENTITY chap3 SYSTEM "mydocs/chapter3.xml"> <!ENTITY chap4 SYSTEM "mydocs/chapter4.xml"> <!ENTITY chap5 SYSTEM "mydocs/chapter5.xml"> ]> <novel> <header> ...blah blah... </header> &chap1; &chap2; &chap3; &chap4; &chap5; </novel>
The difference between this method and the one used for including a DTD fragment (see How do I include one DTD (or fragment) in another?) is that this uses an external general (file) entity which is referenced in the same way as for a character entity (with an ampersand).
The one thing to make sure of is that the included file must not have an XML or DOCTYPE Declaration on it. If you've been using one for editing the fragment, remove it before using the file in this way. Yes, this is a pain in the butt, but if you have lots of inclusions like this, write a script to strip off the declaration (and paste it back on again for editing).
Schemas do not support entities, so the alternative is to use XInclude. This is a W3C specification for including one XML document (or fragment) inside another.
<?xml version="1.0"?> ... <article xmlns="http://docbook.org/ns/docbook" xmlns:xi="http://www.w3.org/2001/XInclude"> <info> <xi:include href="metadata.xml" parse="xml" xpointer="title"/> </info> <sect1> ... </sect1> </article>
Your processing software must be able to handle XInclude for this to work. The XPointer syntax can direct the parser to a specific location within the document, unlike entities, where the entire document is included.