Minimally intrusive XML editing using Python

Thomas Lotze thomas at thomas-lotze.de
Mon Nov 23 17:45:24 CET 2009


Please consider this a reply to any unanswered messages I received in
response to my original post.

Dave Angel wrote:

> What's your real problem, or use case?  Are you just concerned with 
> diffing, or are others likely to read the xml, and want it formatted the 
> way it already is?

I'd like to put the XML under revision control along with other stuff.
Other people should be able to make sense of the diffs and I'd rather not
require them to configure their tools to use some XML differ.

> And how general do you need this tool to be?  For 
> example, if the only thing you're doing is modifying existing attributes 
> or existing tags, the "minimal change" would be pretty unambiguous.  But 
> if you're adding tags, or adding content on what was an empty element, 
> then the requirement gets fuzzy  And finding an existing library for 
> something "fuzzy" is unlikely.

Sure. I guess it's something like an 80/20 problem: Changing attributes in
a way that keeps the rest of the XML intact will go a long way and as
we're talking about XML that is supposed to be looked at by humans, I
would base any further requirements on the assumption that it's
pretty-printed in some way so that removing an element, for example, can
be defined by touching as few lines as possible, and adding one can be
restricted to adding a line in the appropriate place. If more complex
stuff isn't as well-defined, that would be entirely OK with me.

> Sample input, change list, and desired output would be very  useful.

I'd like to be able to reliably produce a diff like this using a program
that lets me change the value in some useful way, which might be dragging
a point across a map with the mouse in this example:

--- foo.gpx	2009-05-30 19:45:45.000000000 +0200
+++ bar.gpx	2009-11-23 17:41:36.000000000 +0100
@@ -11,7 +11,7 @@
   <speed>0.792244</speed>
   <fix>2d</fix>
 </trkpt>
-<trkpt lat="50.605995000" lon="10.709680000">
+<trkpt lat="50.605985000" lon="10.709680000">
   <ele>508.300000</ele>
 <time>2009-05-30T16:37:10Z</time>
   <course>15.150000</course>


-- 
Thomas





More information about the Python-list mailing list