Hey as the author of XML::Toolkit I thought I should reply to what you mentioned on RIC and what you mentioned here.

XML::Toolkit does parse a sample XML file and turn it into a bunch of classes. It is much
more typing to build XML than with XML::Generator and it does not appear to subclass the parsed XML into an inheritance tree mimicking the XML structure — it just makes a class per XML element.

So, any conditional rendering based on data would require tortuous conditionals.

My experience is that parsing XML into an object model is tricky. The XML InfoSet may look relatively straight forward but really it isn’t. You mention here that XML::Toolkit doesn’t create an inheritance tree mimicking the XML structure and this is intentional. It leads to very brittle parsers.

Take for example a document like the following:

<body> <div id="content"> <div id="bacon"> Bacon ipsum dolor sit amet pancetta jerky tail pork stri +p steak, t-bone meatloaf salami ham chicken drumstick ball tip short +loin ham hock jowl. </div> </div> </body>

How exactly do you model the &lt;div&gt; elements there? Do you have a single class that is a subclass of itself? Do you generate unique Div#Content and Div#Bacon classes?

XML::Toolkit decides to go with “neither” and rather than a isa-relationship it uses a has-a relationship. If you feed this snippet into XML::Toolkit you get back a single Div class that has-a text attribute and an optional div attribute. This is a closer mapping to the underlying idea of XML.

XML::Toolkit’s approach falls down however when it comes to being more restrictive. XML::Toolkit doesn’t have proper schema support (yet!) and so it can’t know which elements are required and which are optional. It can’t even known if you are only allowed only one of a given element or many. So it guesses, and it always guesses that you’re allowed 0 or more of any element it sees during the generate step.

Later when you go to create a new document from the generated classes, any attribute that is empty is suppressed in the output. So to generate the before mentioned example without the #bacon element

Body->new( div_collection => [ Div->new( id => 'content') ], );

Which is a bit more typing than you were looking for, but is still pretty straight forward.


In reply to Re: Moose, the tree structure of XML and object-oriented inheritance hiearchies by perigrin
in thread Moose, the tree structure of XML and object-oriented inheritance hiearchies by metaperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.