The following example merges the file Order1.xml with the file order2.xml into a single file through an external entity reference Directory.xml
Listing 1. XML document Order1.xml to be merged:
<?xml version= "1.0" encoding= "GB2312"?>
<order>
<item>
<itemnumber>ksu-0384</itemnumber>
<DESCRIPTION>LCD LCD monitor </description>
<quantity>1</ Quantity>
<price>358.00</price>
</item>
<item>
<itemnumber>ksu-0511</itemnumber>
<description> speaker </description>
<quantity>1</quantity>
<price>16.50</price>
</item>
<item>
<itemnumber>ksu-7710</itemnumber>
<description> Mouse < /description>
<quantity>1</quantity>
<price> 8.50</price>
</item>
</order>
Listing 2. XML document to be merged order2.xml:
<?xml version= "1.0" encoding= "GB2312"?>
<order>
<item>
<itemnumber>ksu-0652</itemnumber>
<description> threesome keyboard </description>
<quantity>1</ Quantity>
<price>35.00</price>
</item>
<item>
<itemnumber>ksu-7720</itemnumber>
<description>intel cpu</description>
<quantity>1</quantity>
<price>1650.00</price>
</item>
<item>
<itemnumber>ksu-7010</itemnumber>
<description> Maxtor HDD </description>
<quantity>1</quantity>
< Price>850.00</price>
</item>
</order>
Listing 3. Output document Directory.xml, the boldface section is the entity declaration and its reference:
<?xml version= "1.0"?>
<! DOCTYPE Directory [
<! ENTITY disclaimer1 SYSTEM "./order1.xml" >
<! ENTITY disclaimer2 SYSTEM "./order2.xml" >
]>
<directory>
&disclaimer1;
&disclaimer2;
</directory>
When an XML 1.0-compliant processor reads a document, it expands the entity to replace the reference with its contents, so the complete document looks like the following.
Listing 4. Document display with extended entities (in IE6):
<?xml version= "1.0"?>
<! DOCTYPE directory (View Source for full DOCTYPE ...) >
-<directory>
-<order>
-<item>
<itemNumber>ksu-0384</itemNumber>
<DESCRIPTION>LCD LCD Monitor </description>
<quantity>1</quantity>
<price>358.00</price>
</item>
-<item>
<itemNumber>ksu-0511</itemNumber>
<description> Speaker </description>
<quantity>1</quantity>
<price>16.50</price>
</item>
-<item>
<itemNumber>ksu-7710</itemNumber>
<description> Mouse </description>
<quantity>1</quantity>
<price>8.50</price>
</item>
</order>
-<order>
-<item>
<itemNumber>ksu-0652</itemNumber>
<description> Threesome Keyboard </description>
<quantity>1</quantity>
<price>35.00</price>
</item>
-<item>
<itemNumber>ksu-7720</itemNumber>
<description>intel cpu</description>
<quantity>1</quantity>
<price>1650.00</price>
</item>
-<item>
<itemNumber>ksu-7010</itemNumber>
<description> Maxtor HDD </description>
<quantity>1</quantity>
<price>850.00</price>
</item>
</order>
</directory>