98

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).

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Sumit
  • 2,932
  • 6
  • 32
  • 54

4 Answers4

105

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:

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • 1
    Hi Mads, I am stuck at this, what happening is When I view my main.xml file in IE then I am getting the text of my otherFile.xml properly but when when I use same concept in my application and load my xml into XMLDocument of .net then I not getting the text of otherFile.xml instead I am recieving whatever that is written in main.xml that is all ]> &otherFile; can you tell me If I need to do something else to load it into my xmldocument ??? – Sumit Mar 10 '11 at 07:47
  • 3
    I'm not very familiar with the .NET api. However, you may need to see what the `XMLResolver` property is set to. If the XMLResolver property is not set on your XMLDocument object, then it may not load external resources. http://msdn.microsoft.com/en-us/library/5fcwybb2.aspx – Mads Hansen Mar 10 '11 at 12:04
  • 2
    do browsers like chrome understand this? – Alp Mar 09 '13 at 17:27
  • 4
    browsers may disable this for security reasons for local files. use --allow-file-access-from-files switch for chrome or upload to the host – premek.v Feb 04 '14 at 10:30
  • XInclude (see Tomasz Nurkiewicz's answer and VoteCoffee's answer) is almost certainly a better choice in most cases. – CXJ Apr 15 '16 at 21:38
  • 1
    In .NET, set the XmlResolver (by creating a default XmlUriResolver) and ProhibitDTD (to false) in the XmlReaderSettings to get past permission problems. By default, it seems Windows/.NET security policy does not allow loading of external resources. – John Jesus Oct 27 '16 at 13:34
  • Is it possible to use tag? – Marci-man Jun 26 '17 at 08:18
  • @Marci-man if the XML is XSLT, then yes it is possible to use `` (as well as ``) to include other stylesheet fragments. However, for a general XML include feature, you would need to either use entities or ``. Specific XML vocabularies, such as ANT scripts, have an [``](https://ant.apache.org/manual/Tasks/import.html) element that can be used. – Mads Hansen Jun 26 '17 at 11:26
31

The other answers cover the 2 most common approaches, Xinclude and XML external entities. Microsoft has a really great writeup on why one should prefer Xinclude, as well as several example implementations. I've quoted the comparison below:

Per http://msdn.microsoft.com/en-us/library/aa302291.aspx

Why XInclude?

The first question one may ask is "Why use XInclude instead of XML external entities?" The answer is that XML external entities have a number of well-known limitations and inconvenient implications, which effectively prevent them from being a general-purpose inclusion facility. Specifically:

  • An XML external entity cannot be a full-blown independent XML document—neither standalone XML declaration nor Doctype declaration is allowed. That effectively means an XML external entity itself cannot include other external entities.
  • An XML external entity must be well formed XML (not so bad at first glance, but imagine you want to include sample C# code into your XML document).
  • Failure to load an external entity is a fatal error; any recovery is strictly forbidden.
  • Only the whole external entity may be included, there is no way to include only a portion of a document. -External entities must be declared in a DTD or an internal subset. This opens a Pandora's Box full of implications, such as the fact that the document element must be named in Doctype declaration and that validating readers may require that the full content model of the document be defined in DTD among others.

The deficiencies of using XML external entities as an inclusion mechanism have been known for some time and in fact spawned the submission of the XML Inclusion Proposal to the W3C in 1999 by Microsoft and IBM. The proposal defined a processing model and syntax for a general-purpose XML inclusion facility.

Four years later, version 1.0 of the XML Inclusions, also known as Xinclude, is a Candidate Recommendation, which means that the W3C believes that it has been widely reviewed and satisfies the basic technical problems it set out to solve, but is not yet a full recommendation.

Another good site which provides a variety of example implementations is https://www.xml.com/pub/a/2002/07/31/xinclude.html. Below is a common use case example from their site:

<book xmlns:xi="http://www.w3.org/2001/XInclude">

  <title>The Wit and Wisdom of George W. Bush</title>

  <xi:include href="malapropisms.xml"/>

  <xi:include href="mispronunciations.xml"/>

  <xi:include href="madeupwords.xml"/>

</book>
VoteCoffee
  • 4,692
  • 1
  • 41
  • 44
  • 1
    It is a comparison between Xinclude and XML external entities, both of which are the top 2 answers and already show or provide use examples. I felt it added to the discussion and was too long/complicated for a comment. Likewise the question asks 'can' and not 'how', so as a far as answering a generalized question, this goes towards clarifying directionality which I felt was really the intent of the question. Lots of other good references on specific implementation. That being, said, thank you for the feedback. I'm updating it with an example shortly. – VoteCoffee Mar 08 '17 at 19:20
  • I appreciate your response and respect that you tried to resolve the issue by adding an example, but I think your answer is still fundamentally flawed. Generally, when quoting text from elsewhere, you should include that text in a blockquote (so that it's obvious you're not trying to plagiarize) and, more importantly, provide your own discussion above / below the blockquote that explains very clearly why you quoted that particular text and how it helps answer the question. If do both of those things and comment again (so I get notified) I will gladly delete my comment and retract my downvote. – Dan Bechard Mar 09 '17 at 13:25
  • Updated with block quotes and comments – VoteCoffee Mar 09 '17 at 17:44
  • This is a significantly better answer! Upvoted and deleted my first comment which is no longer true. I will leave the other comment for context and to hopefully help future readers write better answers. – Dan Bechard Mar 09 '17 at 18:01
11

This feature is called XML Inclusions (XInclude). Some examples:

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • I did try out with http://msdn.microsoft.com/en-us/library/aa302291.aspx but with no use :( My Xmls are main.xml Imported.xml
    © Contoso Corp, 2003
    and the final xml I get is. am I doing something wrong ?
    – Sumit Feb 25 '11 at 19:29
  • In Java, support for XInclude must be explicitly enabled. Maybe your library requires the same preparation? – Tomasz Nurkiewicz Feb 26 '11 at 17:04
  • I was using .net to parse the XMl, I was able to import the XML by Mads Solution.. thanks for your time.. – Sumit Feb 28 '11 at 05:00
  • 1
    libxml does support this, awesome. – RishiD Mar 05 '13 at 15:24
  • 1
    For Java below lines need to be added- DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setXIncludeAware(true); – Rahul Sahu May 06 '16 at 09:02
2

Mads Hansen's solution is good but to succeed in reading the external file in .NET 4 took some time to figure out using hints in the comments about resolvers, ProhibitDTD and so on.

This is how it's done:

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.DtdProcessing = DtdProcessing.Parse;
        XmlUrlResolver resolver = new XmlUrlResolver();
        resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
        settings.XmlResolver = resolver;
        var reader = XmlReader.Create("logfile.xml", settings);
        XmlDocument doc = new XmlDocument();
        doc.Load(reader);
        foreach (XmlElement element in doc.SelectNodes("//event"))
        {
            var ch = element.ChildNodes;
            var count = ch.Count;
        }

logfile.xml:

<?xml version="1.0"?>
<!DOCTYPE logfile [
<!ENTITY events    
 SYSTEM "events.txt">
]>
<logfile>
&events;
</logfile>

events.txt:

<event>
    <item1>item1</item1>
    <item2>item2</item2>
</event>
user3717478
  • 863
  • 1
  • 8
  • 15