Can we import XML file into another XML file?

file, import, merge, xml

Solution

You could use an external (parsed) general entity.

You declare the entity like this:

<!ENTITY otherFile SYSTEM "otherFile.xml">

Then you reference it like this:

&otherFile;

A complete example:

<?xml version="1.0" standalone="no" ?>
<!DOCTYPE doc [
<!ENTITY otherFile SYSTEM "otherFile.xml">
]>
<doc>
  <foo>
    <bar>&otherFile;</bar>
  </foo>
</doc>

When the XML parser reads the file, it will expand the entity reference and include the referenced XML file as part of the content.

If the "otherFile.xml" contained: `<baz>this is my content</baz>`

Then the XML would be evaluated and "seen" by an XML parser as:

<?xml version="1.0" standalone="no" ?>
<doc>
  <foo>
    <bar><baz>this is my content</baz></bar>
  </foo>
</doc>

A few references that might be helpful:

- http://www.xml.com/pub/a/98/08/xmlqna2.html

- http://xmlwriter.net/xml_guide/entity_declaration.shtml

- http://www.javacommerce.com/displaypage.jsp?name=entities.sql&id=18238

Problem

Can we import an XML file into another XML file? I mean is there any import tag in XML that takes XML path as parameter and imports XML (for which path is provided).

Original source