merging documents

Questions about XML that are not covered by the other forums should go here.
aya

merging documents

Post by aya » Thu Sep 23, 2004 12:43 pm

Hi,
I'm new with oxygen and would need a function with which you can merge two documents.

I have two xml docs: in one doc are just a few entries and an other underlaying document contains something like template entries. These two documents should result in a third one which contains the "template" -entries and the modified ones of the first document.

hope you understand what I mean and would be thankful for any hint

kr
aya

george
Site Admin
Posts: 2101
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Thu Sep 23, 2004 5:29 pm

Hi Aya,

You can use XSLT for merging two documents. See below a complete example.

Let's consider the master document master.xml:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<entries>
<template name="t0">value0</template>
<template name="t1">value1</template>
<template name="t2">value2</template>
<template name="t3">value3</template>
<template name="t4">value4</template>
<template name="t5">value5</template>
<template name="t6">value6</template>
<template name="t7">value7</template>
<template name="t8">value8</template>
<template name="t9">value9</template>
</entries>
and the small update document update.xml:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<entries>
<template name="t3">value3modified</template>
<template name="t7">value7modified</template>
<template name="t10">value10</template>
</entries>
The expected output will be:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<entries>
<template name="t0">value0</template>
<template name="t1">value1</template>
<template name="t2">value2</template>

<template name="t4">value4</template>
<template name="t5">value5</template>
<template name="t6">value6</template>

<template name="t8">value8</template>
<template name="t9">value9</template>

<template name="t3">value3modified</template>
<template name="t7">value7modified</template>
<template name="t10">value10</template>
</entries>
All templates from the master document that do not appear in the update document and all the templates in the update document will be in the result.

The following stylesheet will create the output:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- The update document location -->
<xsl:param name="updateDoc" select="'update.xml'"/>
<!-- Copy only the templates that are not found in the update document -->
<xsl:template match="template">
<xsl:if test="not(document($updateDoc)/entries/template[@name=current()/@name])">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
<!-- Process the master document and then the update document -->
<xsl:template match="entries">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:apply-templates select="document($updateDoc)/entries/node()" mode="update"/>
</xsl:copy>
</xsl:template>
<!-- Copy template for the master document -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Copy template for the update document -->
<xsl:template match="@* | node()" mode="update">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="update"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Best Regards,
George

joeandersen
Posts: 4
Joined: Tue Jun 08, 2004 5:17 pm

mergimg tool

Post by joeandersen » Wed Oct 27, 2004 9:58 am

Hello,

Please try a visual tool ExamXML at [http://www.a7soft.com]
This tool can compare and merge XML documents.

Joe.

Post Reply