1

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 node 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.)

1
  • There are terms in your question that might be ambiguous in XML/XSLT/XPath. You should better provide all the necessary but reduce input sources and the desired output.
    – user357812
    Feb 11, 2011 at 18:44

2 Answers 2

2

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.

1
  • +1 interesting. I have a need to modify XML files, but preserve information that is not part of the XML infoset(e.g. whitespace inside of elements, order of attributes, etc). These "lenses" might be able to achieve the desired result. Feb 12, 2011 at 13:40
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.