XML reconciliation
Is there a design pattern for the following situation: I have a XML file generated that can be changed by the user in predefined ways (affecting attributes, not the overall structure). Upon regenerating the file, I want the changes performed by the user to be reapplied (where possible).
The dumb way (though not impossible to actually implement in my case) would be to persist all user changes as XSLT fragments (small templates changing the attribute of some n开发者_StackOverflow中文版ode identified by some XPath-expression) and use this to transform the newly generated file, but I have a feeling that there must be a more general way. (A textual diff is not what I want.)
Though it is not really a design pattern, there is active research in this area using the concept of 'lenses', which are "well-behaved bidirectional transformations" (definition from the Boomerang site).
The "Combinators for bidirectional tree transformations" paper describes the key ideas, and the older Harmony system has an archived web site (frozen circa January 2008).
Both sites have freely available downloads (LGPL v2.1) and a PDF version of the paper.
You know you're in brainy territory when generating XSLT fragments it the "dumb way" :). I would be inclined to store the changes only as a separate XML document and use an XSLT as the reconciliation mechanism, as opposed to storing them as fragments. Simple example:
<library>
<document id="5" title="test" />
</library>
User changes the "title" attribute, and generates this document (internally):
<libraryDelta>
<document id="5">
<title>test</title>
</document>
</libraryDelta>
And then you have your standard XSLT which applies all of the existing elements as attribute changes.
精彩评论