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

Brethern

Is there a standard way of generating cgi-bin pages to edit data structures that are stored in XML, using a DTD or XMLSchema or similar? Suppose I have a DTD that declares

<!ENTITY Inspection (Reviewer)*> <!ATTLIST Inspection InName ID #REQUIRED> <!ENTITY Reviewer> <!ATTLIST Reviewer ReviewerName ID #REQUIRED phoneNo CDATA #REQUIRED>
and an XML structure with the fragment
<Inspection InName="Build 7"> <Reviewer ReviewerName="George Metesky" phoneNumber="x3-5392" /> </Inspection>
I want a system that says "Hmmm, there's one reviewer now, but it can be deleted, or another one can be added" and produces the HTML fragment of a table something like
<tr><td> Inspection: </td> <td colspan="2"><input type="text" name="inspection" value="Build 7"></td></tr> <tr><td colspan="2">Reviwers</td> <td><input type="submit" value="Add another Reviewer"> </td></tr> <tr><td> Reviewer: </td> <td><input type="text" name="reviewer1" value="George Metesky"> </td> <td><input type="submit" name="deleteReviewer1" value="Delete this Reviewer"></td></tr>
In other words, I want a program that looks at the current XML and the DTD that describes it, and produces HTML forms that are smart about offering to add and delete elements. Similarly, I want to generate HTML with radio-buttons for attributes which the DTD enumerates. And so on.

I'm not hard over on using DTD; I'll learn XMLSchema or whatever if it saves me writing a lot of code.

I spent an afternoon and a morning mostly searching CPAN and didn't find what I was looking for. But maybe these kinds of programs are known by a term of which I'm unaware.

Pointers? Suggestions? or am I approaching the whole problem the wrong way?
Thanks,
throop

Replies are listed 'Best First'.
Re: Going between XML and CGi by way of DTD
by j1n3l0 (Friar) on Aug 25, 2007 at 20:17 UTC
    Yes, very interesting idea. Came across these CPAN modules. They seem to be related to what your trying to do.

    * CGI::ToXML
    * XML::CGI

    Good luck with it :)


    Nelo

    Smoothie, smoothie, hundre prosent naturlig!
Re: Going between XML and CGi by way of DTD
by pajout (Curate) on Aug 25, 2007 at 18:52 UTC
    Very interesting question! I thought about it yesterday :>) Just one comment: contain RelaxNG in your investigations.
Re: Going between XML and CGi by way of DTD
by Cody Pendant (Prior) on Aug 26, 2007 at 13:19 UTC
    Although it might seem logical at first glance, it's not possible to map between the two systems.

    To be more specific, while it's possible to map every possible output from a CGI form to an XML DTD, it is not possible the other way around.

    You could write a mapping between a particular DTD/Schema and the relevant CGI script/form (which would have to include validation), but you couldn't generalise. And the DTD/Schema would have to have certain characteristics in order for it to work.



    Nobody says perl looks like line-noise any more
    kids today don't know what line-noise IS ...
      What specs in a DTD do you see as being particular problems?

      I'd settle for a restricted subset - either demanding that DTD/Schema conform to some limitations or having the code ignore the problematic parts of the DTD.

      Here's the background: the customer said "Write a set of CGI scripts in support of performing code inspections. Some of the information that you'll need is already sitting in a read-only 'wild' XML file (i.e. no DTD or equiv). Store the information you gather into another XML file. Periodically, that XML file will be checked into a configuration management system." The customer supplied an XML fragment (annotated) as a rough spec of what he had in mind. There are existing scripts in the system that offer futher guidance. They work, but they are old and somewhat crufty (eg, they generated ill-formed HTML that loads, nonetheless.)

      I generated a few scripts that did part of the job - essentially an editor for a CodeInspection. I wrote subroutines producing HTML to add/deleted sub-entities from entities ("Add or a delete a Reviewer", "Add or delete a line-which-exemplifies-a-bug.") I initially cobbled together a bastardized HoH to store the meta-data about the variables. Realized that was silly—why make up a formalism for storing meta-data when DTD/Schema/RelaxNG all do that already? So I stored the meta-data in a .dtd.

      I've already written a few small functions that look at the DTD and which generate the code for for editing/adding/deleting attributes and indentured entities. As I was doing the work, I thought "Wait, somebody's probably done this already." So I went searching in CPAN and asking here.

      Since I haven't found anything, I'm willing to believe that nobody's done this–or at least not released it. But I don't understand your warning that it would be generally impossible to do.

      thanks
      throop

        OK, I should have known I'd be challenged.

        Here's my thinking. Although XML is often used for traditional two-dimensional row-and-column data, it doesn't have to be. It can have arbitrary levels of nestedness, extra dimensions.

        So if you take this XML

        <items> <item> <foo>a</foo> <bar>b</bar> <baz>c</baz> </item> <item> <foo>x</foo> <bar>y</bar> <baz>z</baz> </item> </items>
        which follows this DTD:
        <!ELEMENT bar (#PCDATA)> <!ELEMENT baz (#PCDATA)> <!ELEMENT foo (#PCDATA)> <!ELEMENT item (foo, bar, baz)> <!ELEMENT items (item+)>
        Yes, it's pretty much plain sailing to generate the HTML.

        But what about this?

        <items> <item> <foo>a</foo> <bar>b</bar> <baz>c</baz> <item> <!-- items can contain sub-items --> <foo>d</foo> <bar>e</bar> <baz>f</baz> </item> </item> <item> <foo>x</foo> <bar>y</bar> <baz>z</baz> </item> </items>
        Which matches the DTD with one small change:
        <!ELEMENT bar (#PCDATA)> <!ELEMENT baz (#PCDATA)> <!ELEMENT foo (#PCDATA)> <!ELEMENT item (foo, bar, baz, item?)> <!ELEMENT items (item+)>

        What's my HTML form going to look like when any given <item> element can contain an arbitrary number of other <item> elements?



        Nobody says perl looks like line-noise any more
        kids today don't know what line-noise IS ...
Re: Going between XML and CGi by way of DTD
by pajout (Curate) on Aug 28, 2007 at 10:35 UTC
    I have explored net, but without bingo. There are links, which may help, not assorted:
    http://perl-xml.sourceforge.net/faq/
    http://www.geocities.com/tlhome2000/form6/
    http://ausweb.scu.edu.au/aw02/papers/refereed/fitch/paper.html
    http://search.cpan.org/~jenda/XML-DTDParser-2.01/DTDParser.pm
    http://search.cpan.org/~morni/XML-ParseDTD-0.1.4/ParseDTD.pm
    http://xml.ascc.net/resource/schematron/
    http://search.cpan.org/~abw/XML-Schema-0.07/lib/XML/Schema.pm