Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Moose, the tree structure of XML and object-oriented inheritance hiearchies

by perigrin (Sexton)
on Jun 20, 2011 at 20:26 UTC ( [id://910620]=note: print w/replies, xml ) Need Help??


in reply to Moose, the tree structure of XML and object-oriented inheritance hiearchies

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-24 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found