[xsl] How Do I Generate A Set-Difference With Context - Part A

Subject: [xsl] How Do I Generate A Set-Difference With Context - Part A
From: "Aron Bock" <aronbock@xxxxxxxxxxx>
Date: Sat, 05 Mar 2005 09:25:07 +0000
Hello, I'm coming up against the edges of my journeyman usage of XSLT, and concurrent with my huddling down with some books I'm hoping to get pointers on the following.

I have 2 XML documents with the same schema, and I want to determine the set difference (not symmetric set difference) between them. True, this has had much written about it, but all the solutions I've seen simply show a collection of "differenced" nodes, in a format that bears little resemblence to the source document. Thus my twist is, I'd like to generate a set difference, and retain enough context so I end up with a document [tree] similar to the source--just pruned of unchanged nodes.

The "enough context" bears elaboration, but for now lets say I'd be happy if the end result preserves tree branches that lead from the changed node, all the way to the root. Each branch would imply at minimum (and show) a changed leaf and its attributes, all the way through the root and its attributes. In future I hope to be able to dynamically affect the meaning of "enough context", but that for later.

Now some sample data (adapted from Sal Mangano's "XSLT Cookbook"):

doc1.xml
======
<doc>
   <chapter n="1">
       <section n="1">
           <p n="1">Once upon a time...</p>
       </section>
   </chapter>
   <chapter n="2">
       <note n="1">I am still waiting for my $100000 advance.</note>
       <section n="1">
           <p n="1">... and they lived happily ever after.</p>
       </section>
   </chapter>
</doc>


doc2.xml ====== <doc> <chapter n="1"> <section n="1.A"> <p n="1">Once upon a time...</p> </section> </chapter> <chapter n="2.A"> <note n="1">I am still waiting for my $100000 advance.</note> <section n="1"> <p n="1">... and they lived happily ever after.</p> </section> </chapter> </doc>


And I'd like doc3.xml = doc2.xml \ doc1.xml to yield: ====== <doc> <chapter n="1"> <section n="1.A"> </section> </chapter> <chapter n="2.A"> </chapter> </doc>

Here I'm using "value semantics" for node equality--meaning 2 nodes are considered equal, even if from different documents, if they have the same namespace, local-name, and number and values of attributes. Let's assume the same namespace(s).

Of course, one could make a case that the criteria for node equality should be extended to mandate that descendent nodes of each node also compare equal respectively. I suppose this would be a more orthogonal view, since then doc3.xml above really shows all "changed" nodes: <doc> is present not just as context; it's present because <doc> in doc2.xml is changed from <doc> in doc1.xml by virtue of having changed descendent(s).

Thus far I've been able to determine the changed leaf nodes (this is pretty much straight from "XSLT Cookbook", Chapter 7), and to write a template to "print the branch" for each changed node. However I haven't been able to "superimpose" or "union" these separate branches to get 1 output doc. I'm possibly going about it all wrong, and hence the need to hunker down with books.

But if somebody could suggest how I may tackle this, please do.

And if seeing any templates I've written would help I'll be glad to post them. This is my first post to this (or any) XSLT list, and I don't want to lenghthen an already long note.

Thanx

--A

_________________________________________________________________
Dont just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/


Current Thread