You could come pretty darned close to this ... no, you could get this ... by using “XSLT stylesheets” and an open-source tool such as Saxon. In this case, you would write no program at all ... not in Perl, and not in anything else. (Although Perl also knows how to do XSLT transformatiions.)
Now, this use of the word “stylesheet” is another one of the abuses of human language that are so common in the data processing world: they have absolutely nothing to do with, say, CSS.
The transform would read one (version of the) file as input, and generate a new (version of the) file to replace it. Thus, the process is non-destructive and therefore repeatable. You would, presumably, archive the old version in some useful way and then replace it with the latest one.
A critical idea behind XSLT is XPath expressions, which are vaguely like “queries for XML documents.” So, basically, you will write an expression that says what you want to get (not how to get it ... no code-writing here), and then, what you want to produce from each subtree that XSLT finds. In your original post, you already say that in human terms, and the XSLT will simply say the same thing in computer terms.