XML technologies / XSLT / XSLT Cookbook / From XML to XML / Merging documents with identical schemas
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You have two or more identically structured documents and want to merge them into one.
Solution
If their contents differ, or duplicates do not matter, the solution is simple:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="doc2"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:copy-of select="* | document($doc2)/*/*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>If input documents contain duplicates but you want unique output entries, remove them using the technique in recipe Ignoring duplicate elements. Consider these two examples.
Example: Document 1.
<people which="MeAndMyFriends">
<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"/>
</people>Example: Document 2.
<people which="MeAndMyCoWorkers">
<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"/>
</people>This stylesheet merges and removes duplicate elements using xsl:sort and the exsl:node-set extensions:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common">
<xsl:import href="exsl.xsl" />
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="doc2"/>
<!-- Hier führen wir ein 'key'-Attribut ein, um das Entfernen von Duplikaten zu vereinfachen -->
<xsl:variable name="all">
<xsl:for-each select="/*/person | document($doc2)/*/person">
<xsl:sort select="concat(@lastname,@firstname)"/>
<person key="{concat(@lastname, @firstname)}">
<xsl:copy-of select="@* | node( )" />
</person>
</xsl:for-each>
</xsl:variable>
<xsl:template match="/">
<people>
<xsl:for-each select="exsl:node-set($all)/person[not(@key = preceding-sibling::person[1]/@key)]">
<xsl:copy-of select="."/>
</xsl:for-each>
</people>
</xsl:template>
</xsl:stylesheet>This has three drawbacks. First, it changes element order, which may be undesirable. Second, XSLT 1.0 requires the extension node-set. Third, it is not generic: each new duplicate-free merge requires rewriting the entire stylesheet.
xsl:key uses an approach addressing these problems:
<!-- Stylesheet: merge-simple-using-key.xslt -->
<!-- Importieren Sie dieses Stylesheet in ein anderes, das den Schlüssel definiert -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:merge="http:www.ora.com/XSLTCookbook/mnamespaces/merge">
<xsl:param name="doc2"/>
<xsl:template match="/*">
<!--Kopiert das äußerste Element des Quelldokuments -->
<xsl:copy>
<!-- Für jedes Kind in der Quelle wird festgestellt, ob es in das Ziel kopiert werden soll. Grundlage dieser Entscheidung ist die Frage, ob es in dem anderen Dokument existiert. -->
<xsl:for-each select="*">
<!-- Aufruf eines Templates, das einen eindeutigen Schlüsselwert für dieses Element ermittelt. Es muss im einschließenden Stylesheet definiert sein. -->
<xsl:variable name="key-value">
<xsl:call-template name="merge:key-value"/>
</xsl:variable>
<xsl:variable name="element" select="."/>
<!--Dieses for-each dient einfach dazu, den Kontext auf das zweite Dokument zu ändern -->
<xsl:for-each select="document($doc2)/*">
<!-- Der Schlüssel key wird als Mechanismus zum Testen, ob das Element im zweiten Dokument vorhanden ist, verwendet. Der Schlüssel muss durch das einschließende Stylesheet definiert sein. -->
<xsl:if test="not(key('merge:key', $key-value))">
<xsl:copy-of select="$element"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
<!--Kopiert alle Elemente im zweiten Dokument -->
<xsl:copy-of select="document($doc2)/*/*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>This stylesheet imports the previous one and defines the key and a template to obtain its value:
<!-- Dieses Stylesheet definiert die Eindeutigkeit von Elementen bezüglich eines Schlüssels. -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:merge="http:www.ora.com/XSLTCookbook/mnamespaces/merge">
<xsl:include href="merge-simple-using-key.xslt"/>
<!--Eine Person wird eindeutig durch die Verkettung des Nachnamens mit dem Vornamen definiert -->
<xsl:key name="merge:key" match="person" use="concat(@lastname,@firstname)"/>
<xsl:output method="xml" indent="yes"/>
<!-- Dieses Template bezieht den Schlüsselwert für ein Element -->
<xsl:template name="merge:key-value">
<xsl:value-of select="concat(@lastname,@firstname)"/>
</xsl:template>
</xsl:stylesheet>Another merging and deduplication approach uses value-based set operations described in recipe Performing set operations on node sets using value semantics. The solution is shown here; consult that recipe for details. The next two examples provide further stylesheets.
Example: A reusable stylesheet implementing merging as a union operation.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:vset="http:/www.ora.com/XSLTCookbook/namespaces/vset">
<xsl:import href="../query/vset.ops.xslt"/>
<xsl:output method="xml" indent="yes"/>
<xsl:param name="doc2"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:call-template name="vset:union">
<xsl:with-param name="nodes1" select="*"/>
<xsl:with-param name="nodes2" select="document($doc2)/*/*"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>Example: A stylesheet defining element equality.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:vset="http:/www.ora.com/XSLTCookbook/namespaces/vset">
<xsl:import href="merge-using-vset-union.xslt"/>
<xsl:template match="person" mode="vset:element-equality">
<xsl:param name="other"/>
<xsl:if test="concat(@lastname,@firstname) = concat($other/@lastname,$other/@firstname)">
<xsl:value-of select="true()"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>The solution using vset:union requires less new code than the key-based solution. For large documents, however, the solution using xsl:key is probably faster.
Discussion
Merging is often needed when different people or processes create parts of a document. It is also needed to reassemble a document split for parallel processing or because it was too unwieldy to handle as a whole.
These examples cover just two documents. Merging any number requires passing a document list to the stylesheet. One technique uses a parameter with space-separated filenames and a simple tokenizer, from recipe Tokenizing a string, to extract names. Another passes the filenames in the source document, as shown next.
Example: XML listing documents to merge.
<mergeDocs>
<doc path="people1.xml"/>
<doc path="people2.xml"/>
<doc path="people3.xml"/>
<doc path="people4.xml"/>
</mergeDocs>Example: A stylesheet merging the documents, assuming no duplicate content.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="docs" select="/*/doc"/>
<xsl:template match="mergeDocs">
<xsl:apply-templates select="doc[1]"/>
</xsl:template>
<!--Erfassen des ersten Dokuments, um das oberste Element zu erzeugen -->
<xsl:template match="doc">
<xsl:variable name="path" select="@path"/>
<xsl:for-each select="document($path)/*">
<xsl:copy>
<!-- Vereinigen der Kinder von Dokument 1 -->
<xsl:copy-of select="@* | *"/>
<!--Schleife über die verbleibenden Dokumente, um deren Kinder zu vereinigen -->
<xsl:for-each select="$docs[position() > 1]">
<xsl:copy-of select="document(@path)/*/*"/>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
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