Interview Questions

How do I include one XML file in another?

XML Interview Questions and Answers


(Continued from previous question...)

46. How do I include one XML file in another?

This works exactly the same as for SGML. First you declare the entity you want to include, and then you reference it by name:
<?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 question D.15, ‘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).

(Continued on next question...)

Other Interview Questions