Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: extract xml data and insert new xml tag into same file

by sundialsvc4 (Monsignor)
on Aug 13, 2013 at 12:05 UTC ( #1049259=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: extract xml data and insert new xml tag into same file
Re^2: extract xml data and insert new xml tag into same file
by Anonymous Monk on Aug 13, 2013 at 12:16 UTC

    In this case, you would write no program at all ... not in Perl, and not in anything else.

    XSLT is programming, deal with it

      Anonymity does not become you.   Nevertheless, there is a huge difference between writing a custom Perl program to extract and to re-form data from an XML data source, and leveraging an existing technology to do the same thing.   There is an entire world of endeavor ... particularly in the world of electronic publishing and education ... which relies completely on XSLT, and a single standardized tool-chain, with nary a line of “source code” to be found.   Perl is sometimes used to drive that tool chain ... at Apple, for example, until quite recently ... but the extraction of data and the subsequent compiling of all manner of deliverables occurs entirely without custom code.   The OP’s requirement appears to me to be a classic use-case.   And, for the curious, I myself have spent years working in exactly this space.

        ... repeat ...

        programming, by any other name, is programming, you can call it XSLT transformatiions but its still programming

Re^2: extract xml data and insert new xml tag into same file
by Jenda (Abbot) on Aug 14, 2013 at 14:54 UTC

    XSLT, also known as XML in XML that XMLs XML to XML XML in XML for XML xml XML XML? No thanks!

    By your definition writing SQL is not programming either. You write an expression that says what you want to get, not how to get it. Heck, Prolog is no programming either then, you write what do you want to hold true for the result, not how to compute the result.

    Writing XSLT IS programming. The syntax is insane, the readability for all nontrivial "stylesheets" incredibly low, but it is programming.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1049259]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (15)
As of 2014-09-18 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (116 votes), past polls