Possibly the easiest way to combine data from multiple XML files is by using XSLT (Extensible Stylesheet Language Transformation) files. XSLT files can be used to transform XML documents, often applying styling to data stored in XML format to produce pretty HTML / CSS based output. Combining xml documents using XSLT is done primarily through the use of the ‘document()’ function. To get a copy of an existing XML document, use:
This will load the set of nodes comprising the document. If you wish to simply append two XML files together, then just add references to all the files you want to combine. Of course, you may want to actually exclude or sort certain data. If so, you need to make use of the more advanced looping, sorting and conditional tests included in the XSL. The W3C Schools tutorial on XSL is a good place to start if you have no prior experience.
Join Across Files using XQuery
Inner and Outer joins are commonly used in relational data storage systems. Using XQuery it is possible to create similar joins between multiple XML files. Take, for example, two XML documents called ‘parts.xml’ and ‘supplier.xml’. In order to get a list of parts along with the people who supply them, it is necessary to perform an inner join. XQuery’s ‘fn:doc()’ function reads from external files, and places a node list into a variable. That variable can then be referenced later in the query, and hence be used to perform a join in a similar manner to an SQL query:
for $p in fn:doc(“parts.xml”)/parts/part,
$s in fn:doc(“suppliers.xml”)/suppliers/supplier[supplier_no = $p/supplier_no]
Results can then be sorted and output in the manner required. When combining data from multiple xml files an inner join will only show parts which have suppliers, and suppliers which have parts. By using an outer join it would be possible to get a listing of all parts or suppliers in both documents, regardless of whether the two are linked. Do note however that using this method to extract data relies on the two XML files having a shared key.
Code Example: Merging XML Files using .Net’s DOM API
In addition to using XSLT and XQuery, it is also possible to merge the data in XML documents through custom code. Both Java and C# offer a domain object model (DOM) based method for working with XML files. The code below shows how to merge XML documents using the C# .Net’s domain object model:
XmlDocument doc = new XmlDocument();
XmlNode rootNode= doc.CreateElement(“Container”);
XmlDocument tempDoc = new XmlDocument();
XmlNode imported = doc.ImportNode(tempDoc.DocumentElement, true);
imported = doc.ImportNode(tempDoc.DocumentElement, true);
XSLT, XQuery and custom written code can all be used to combine data from multiple XML files. Hopefully this article has given you a good overview of the options available to you when you want to merge xml documents, or at least a decent starting point for further research.