下面的例子通过外部实体引用,将文件order1.xml与文件order2.xml合并为一个文件directory.xml
清单1. 要合并的xml文档order1.xml:
<?xml version="1.0" encoding="GB2312"?>
<order>
<item>
<itemNumber>ksu-0384</itemNumber>
<description>LCD液晶显示器</description>
<quantity>1</quantity>
<price>358.00</price>
</item>
<item>
<itemNumber>ksu-0511</itemNumber>
<description>音箱</description>
<quantity>1</quantity>
<price>16.50</price>
</item>
<item>
<itemNumber>ksu-7710</itemNumber>
<description>鼠标</description>
<quantity>1</quantity>
<price>8.50</price>
</item>
</order>
清单2. 要合并的xml文档order2.xml:
<?xml version="1.0" encoding="GB2312"?>
<order>
<item>
<itemNumber>ksu-0652</itemNumber>
<description>双飞燕键盘</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>迈拓硬盘</description>
<quantity>1</quantity>
<price>850.00</price>
</item>
</order>
清单3. 输出文档directory.xml,黑体字部分为实体声明及其引用:
<?xml version="1.0"?>
<!DOCTYPE directory [
<!ENTITY disclaimer1 SYSTEM "./order1.xml">
<!ENTITY disclaimer2 SYSTEM "./order2.xml">
]>
<directory>
&disclaimer1;
&disclaimer2;
</directory>
当符合 XML 1.0 的处理器读取文档时,就会扩展实体,用它的内容代替引用,因此完整的文档如下所示。
清单4. 带有扩展实体的文档显示(IE6中):
<?xml version="1.0" ?>
<!DOCTYPE directory (View Source for full doctype...)>
- <directory>
- <order>
- <item>
<itemNumber>ksu-0384</itemNumber>
<description>LCD液晶显示器</description>
<quantity>1</quantity>
<price>358.00</price>
</item>
- <item>
<itemNumber>ksu-0511</itemNumber>
<description>音箱</description>
<quantity>1</quantity>
<price>16.50</price>
</item>
- <item>
<itemNumber>ksu-7710</itemNumber>
<description>鼠标</description>
<quantity>1</quantity>
<price>8.50</price>
</item>
</order>
- <order>
- <item>
<itemNumber>ksu-0652</itemNumber>
<description>双飞燕键盘</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>迈拓硬盘</description>
<quantity>1</quantity>
<price>850.00</price>
</item>
</order>
</directory>