http://www.perlmonks.org?node_id=1049259


in reply to extract xml data and insert new xml tag into same file

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.

Web browsers, for example, already know how to do XSL, and you can readily find very impressive examples of what can be done.   An interactive Periodic Table of the Elements, for example, written entirely with XSLT and a smidgen of JavaScript.   Many word-processors and statistical analysis programs also know how to use both XSLT and XPath.   The DocBook electronic publishing format (the source of all those O’Reilly books) is also built entirely with XSLT.