XML technologies / XSLT / XSLT Cookbook / From XML to XML / Merging documents with different schemas
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You have two or more differently structured documents to merge into one.
Solution
Merging dissimilar data varies by application, so no single generic solution suffices. Instead, here are common ways to combine two documents, with solutions for each.
Incorporating a document as a child of a parent document
The simplest interpretation is incorporating one document as a child. Use xsl:copy-of to copy a document or fragment into the appropriate part of another. This example merges two documents in a container document whose element names identify the files to merge:
<MyNoteBook>
<friends></friends>
<coworkers></coworkers>
<projects>
<project>Ersetzen von mapML durch XSLT-Engine mittels Xalan C++</project>
<project>Sinn des Lebens herausfinden.</project>
<project>Ermitteln, wo der Wäschetrockner die ganzen fehlenden Socken versteckt</project>
</projects>
</MyNoteBook>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="copy.xslt"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="friends | coworkers">
<xsl:copy>
<xsl:variable name="file" select="concat(local-name( ),'.xml')"/>
<xsl:copy-of select="document($file)/*/*"/>
</xsl:copy>
</xsl:template>
...
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<MyNoteBook>
<friends>
<person firstname="Sal" lastname="Mangano" age="38" height="5.75"/>
<person firstname="Mike" lastname="Palmieri" age="28" height="5.10"/>
<person firstname="Vito" lastname="Palmieri" age="38" height="6.0"/>
<person firstname="Vinny" lastname="Mari" age="37" height="5.8"/>
</friends>
<coworkers>
<person firstname="Sal" lastname="Mangano" age="38" height="5.75"/>
<person firstname="Al" lastname="Zehtooney" age="33" height="5.3"/>
<person firstname="Brad" lastname="York" age="38" height="6.0"/>
<person firstname="Charles" lastname="Xavier" age="32" height="5.8"/>
</coworkers>
<projects>
<project>Ersetzen von mapML durch XSLT-Engine mittels Xalan C++</project>
<project>Sinn des Lebens herausfinden.</project>
<project>Ermitteln, wo der Wäschetrockner die ganzen fehlenden Socken versteckt</project>
</projects>
</MyNoteBook>An interesting variation is a document indicating inline inclusion of another. The W3C standard method is XInclude. Implement a general XInclude processor in XSLT by extending copy.xslt:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="copy.xslt"/>
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="xi:include" xmlns:xi="http://www.w3.org/2001/XInclude">
<xsl:for-each select="document(@href)">
<xsl:apply-templates/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>The xsl:for-each only changes context to the included document. Then use xsl:apply-templates to continue copying its contents.
Combining two documents
A variation combines elements sharing parent-element types. Suppose two biologists independently collected animal data. To begin a unified database, they could combine data according to structural similarities.
Biologist 1 has this file:
<animals>
<mammals>
<animal common="Schimpanse" species="Pan troglodytes" order="Primates"/>
<animal common="Mensch" species="Homo Sapiens" family="Primates"/>
</mammals>
<reptiles>
<animal common="Boa Constrictor" species="Boa constrictor" order="Squamata"/>
<animal common="Gecko" species="Gekko gecko" order="Squamata"/>
</reptiles>
<birds>
<animal common="Möwe" species="Larus occidentalis" order="Charadriiformes"/>
<animal common="Schwarzrückenspecht" species="Picoides arcticus" order="Piciformes"/>
</birds>
</animals>Biologist 2 has this file:
<animals>
<mammals>
<animal common="Flusspferd" species="Hippopotamus amphibius" family=" Hippopotamidae"/>
<animal common="Dromedar" species="Camelus dromedarius" family="Camelidae"/>
</mammals>
<insects>
<animal common="Zweipunkt-Marienkäfer" species="Adalia bipunctata" family="Coccinellidae"/>
<animal common="Mistkäfer" species=" Onthophagus australis" family="Scarabaeidae"/>
</insects>
<amphibians>
<animal common="Grüne Meeresschildkröte" species="Chelonia mydas" family="Cheloniidae"/>
<animal common="Grüner Laubfrosch" species=" Hyla cinerea" family="Hylidae "/>
</amphibians>
</animals>The schemas are similar but different. Both include class Mammalia, but differ at other levels. Below animal, one biologist recorded animal orders, the other families. This stylesheet combines the documents at animal-class level, the second structural level:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*
<xsl:param name="doc2file"/>
<xsl:variable name="doc2" select="document($doc2file)"/>
<xsl:variable name="thisDocsClasses" select="/*/*"/>
<xsl:template match="/*">
<xsl:copy>
<!-- Vereinigen gemeinsamer Abschnitte zwischen Quelldokument und Dokument 2. Enthält auch Abschnitte, die nur im Quelldokument enthalten sind. -->
<xsl:for-each select="*">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:copy-of select="$doc2/*/*[name( ) = name(current( ))]/*"/>
</xsl:copy>
</xsl:for-each>
<!-- Vereinigen von Abschnitten, die nur in Dokument 2 enthalten sind -->
<xsl:for-each select="$doc2/*/*">
<xsl:if test="not($thisDocsClasses[name( ) = name(current( ))])">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>The result can be normalized further manually or automatically:
<animals>
<mammals>
<animal common="Schimpanse" species="Pan troglodytes" order="Primates"/>
<animal common="Mensch" species="Homo Sapiens" order="Primates"/>
<animal common="Flusspferd" species="Hippopotamus amphibius" family=" Hippopotamidae"/>
<animal common="Dromedar" species="Camelus dromedarius" family="Camelidae"/>
</mammals>
<reptiles>
<animal common="Boa Constrictor" species="Boa constrictor" order="Squamata"/>
<animal common="Gecko" species="Gekko gecko" order="Squamata"/>
</reptiles>
<birds>
<animal common="Möwe" species="Larus occidentalis" order="Charadriiformes"/>
<animal common="Schwarzrückenspecht" species="Picoides arcticus" order="Piciformes"/>
</birds>
<insects>
<animal common="Zweipunkt-Marienkäfer" species="Adalia bipunctata" family="Coccinellidae"/>
<animal common="Mistkäfer" species=" Onthophagus australis" family="Scarabaeidae"/>
</insects>
<amphibians>
<animal common="Grüne Meeresschildkröte" species="Chelonia mydas" family="Cheloniidae"/>
<animal common="Grüner Laubfrosch" species=" Hyla cinerea" family="Hylidae "/>
</amphibians>
</animals>Joining elements to create new elements
A less trivial merge puts two documents on equal footing or makes elements of one into children based on a characteristic. Consider merging these documents with different information about people:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="copy.xslt"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="doc2file"/>
<xsl:variable name="doc2" select="document($doc2file)"/>
<xsl:template match="person">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:element name="{local-name( )}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:variable name="matching-person" select="$doc2/*/person[@name=concat(current( )/@firstname,' ',current( )/@lastname)]"/>
<xsl:element name="smoker">
<xsl:value-of select="$matching-person/@smoker"/>
</xsl:element>
<xsl:element name="sex">
<xsl:value-of select="$matching-person/@sex"/>
</xsl:element>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>This stylesheet does two things: it converts attribute-encoded information in the input stylesheets to elements, and adds information from $doc2 missing from the source.
Discussion
Merging XML with different schemas is less well defined than merging identical schemas. This chapter explores three interpretations, but more complex ones exist, including combinations of all three. A generic XSLT merge tool handling every special case would be difficult. These examples at least provide a starting point for more ambitious merges.
See also
These examples focused on one-to-one element relationships. Recipe Joins shows merging different XML from a database-query perspective. Those techniques also apply to one-to-many merges.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de