I'm new to XML, so this may be a fairly easy question to answer. I was wondering if there is a standard way of referencing external XML files from within other XML files. Let me give an example. Say you have a file which defines a single object that holds a large amount of data:
<person>
<name>John</name>
<age>18</age>
<hair>Brown</hair>
<eyes>Blue</eyes>
</person>
For the sake of this question, pretend that person holds loads of other information. Pretend the file is like 10 MB.
Now, let's say you have another XML file which defines a group:
<group>
<person>
<name>John</name>
<age>18</age>
<hair>Brown</hair>
<eyes>Blue</eyes>
</person>
<person>
<name>Kim</name>
<age>21</age>
<hair>Blue</hair>
<eyes>Green</eyes>
</person>
<person>
<name>Sean</name>
<age>22</age>
<hair>Black</hair>
<eyes>Brown</eyes>
</person>
</group>
As you can see, if Person's were very large, the Group file would be extremely large. So, if we have something like John.xml, is there a standard way to reference it in Group.xml without explicitly defining all of John's data? I'm sure this is a very broad topic, so feel free to link me to any relevant web pages. Thanks!
There is no standard (will work in every parser) for importing nodes like that. But you could save space by changing some of your elements in to attributes
There are a couple of "standard" ways to do what you want, namely XLink and XInclude (depending on what you want to do), though you have to make sure that you have a processor that can pull in the external references. Most XML libraries don't come with this functionality already enabled.
Then you'd be able to do something like:
However, you probably don't really need this. If you need a subset of information from a large document, you can easily use XSLT or XQuery to trim out the parts that you need. You can use this approach, along with SAX parsing - which is event based and doesn't have the whole document in memory - to scale you application to handle fairly large documents.
Even while using DOM, I didn't start to see problems with large documents until they were in the tens of megabytes range.
Um, there are no size limitations on xml files. you shouldn't worry about extremely large sizes. But remember; Xml is a data exchange format, not a database format. You use xml to swap data between different applications/services.
Standards
XInclude is the only standard with any level of support.
There are some good examples of use in the Wikipedia article on XInclude.
XLink is a tangentially-related standard, not really for including documents, but more for citing portions within other documents. It's not well supported.
Alternatives
If you are worried about size, there are several ways to go:
doc()
function and processes. (Unless your processor is streaming or has a way to dispose of documents it is finished with, as DDXQ or Saxon do, you will still run into the same size problem through.)Here is the XML specification for DTD, in which you can declare entity references.
A simple document like:
And file:///C:/test.txt being:
will expand the original document to:
I do believe non-validating XML parsers are not required to expand out the references, so be cautious there.
Also, don't forget to put standalone="no" in the XMLDecl. (Not having the standalone attribute assumes it equals "no", but its still better to put it there...)