Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question on merging XML documents?!?!

Status
Not open for further replies.

tjacob

MIS
Jun 5, 2008
2
US
Can anyone please tell me how to merge 2 xml documents? Some of the documents may contain duplicate records...so I need to merge into a new file with duplicate entries only recorded once.

An example I've been working with is for the following code

file 1

<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
</book>

</book>
<book id="bk3">
<author>Jordan, Michael</author>
<title> computers</title>
<genre>Romance</genre>
<price>5.50</price>
</book>
</catalog>

File 2.

<catalog>
<book id="bk106">
<author>Randall, Cynthia</author>
<title>Lover Birds</title>
<genre>Romance</genre>
<price>4.95</price>
</book>

<book id="bk3">
<author>Jordan, Michael</author>
<title> computers</title>
<genre>Romance</genre>
<price>5.50</price>
</book>
</catalog>

Please Help!
 
This is a job for recursion. (Of course, many things in XSLT are a job for recursion!)

If the determining factor is the attribute id in the <book> element, then simple tail recursion on a union of the nodes of the two documents will do the trick. Have a look at the min (and max) example found here on TopXML. These examples show how to use recursion that maintains a parameter which contains the minimum temperature value found so far. At the end of the recursion (when all the elements have been examined), an <xsl:value> outputs the minimum.

Using the same basic tail recursion structure, have a parameter (let's call it uniqueIDs) that contains all the unique id values found so far, separated by some unique character (I usually use the vertical bar |). Start the parameter with a value of "|" and concatenate the value of id attribute and another "|" at each level of recursion. Then you can use a simple contains{$uniqueIDs,concat('|',@id,'|')) function to determine whether to skip or to output the node.

There are fancier ways, and certainly more obscure ways, of achieving the desired result, but this should work for you.

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top