johnnywang has asked for the wisdom of the Perl Monks concerning the following question:

Seeking enlightment: Given an XML document, can one create a generic web application (let's say, just one page) to edit the XML document? Let me give an example, say the XML is (I'm mixing up attributes and sub-elements just for demo, not necessarily making sense):
<person name="Jack" age="35"> <phone type="home" value="5551112222"/> <phone type="cell" value="5552223333"/> </person>
Now I'd like a web page that allows changing the attribute/elements. If fancy, I may also like to allow adding/deleting elements. It's not hard to develop a web page for this particular XML. My question is whether there is a general way, i.e., one that works for most XML documents. Of couse, we still need to design the html page, but can one do the following (assuming the two-step process: display, then submit an update):
  1. In the html page, name the <input> fields in a consistent way, e.g., using Xpath, or simply "0:2:1@name" (0th child of the root, 2nd child of that one, and 1st of its children, and the "name" attribute), etc.
  2. For display, the script will load the document, and extract the correct values, this seems to be easier if using xpath for the first part.
  3. For updates, the script will take the input parametes, change the corresponding elements/attributes (XQuery is supposed to provide a language for doing this kind of updates, sort of like SQL for updating database, there is something called XUpdate implemented in java that provides something simpler)
I've implemented something like this in java, but not complete, where I used XSLT for displaying (but the name is hard coded), and another xslt to generate xupdate instructions, works ok, but not general enough for my current task. Are there such things in perl? Thanks.

Replies are listed 'Best First'.
Re: A generic web app to edit XML
by Cody Pendant (Prior) on Feb 15, 2005 at 09:25 UTC
    I'd use XML::Simple to grab the file to a data structure, then print it out as a form, then save it back again when you submit. You'll have issues with the naming convention for the fields, but you seem to have various ideas about that yourself.


    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
    =~y~b-v~a-z~s; print