Many of the issues you point out here with XML::Toolkit are a symptom of it's maturity and the conscious decision to punt on handling ambiguous cases. Take for example the collection interface you illustrate:

my $document = MyApp::Note->new( to_collection => [MyApp::To->new(text => 'Bob')], from_collection => [MyApp::From->new(text => 'Alice')], headings => [MyApp::Heading->new(text => 'Secret' )], body_collection => [MyApp::Body->new(text=>'Shh!')], )

XML::Toolkit has no way to know the number of to child elements, so it defaults to assuming "many". If you know that you're only ever going to have one to/from/heading/body then you can re-write the class to change these from ArrayRef types to Object types.

The second object invocation can be eliminated by adding a coercion.

coerce 'MyApp::To' => from 'Str' => via { MyApp::To->new(text => $_) }; coerce 'MyApp::From' => from 'Str' => via { MyApp::From->new(text => $_) }; coerce 'MyApp::Heading' => from 'Str' => via { MyApp::Heading->new(text => $_) }; coerce 'MyApp::Body' => from 'Str' => via { MyApp::Body->new(text => $_) };

Combing both of these should get you identical syntax to your XML::Element::Tolol example. Unfortunately this is currently a manual post-processing of the files that XML::Toolkit generates. I could conceptually be able to build the coercions for Text nodes from Str programmatically during compilation and then generate a Type Library that is included in the generated classes. I simply have never had this need in the projects I've used XML::Toolkit on.

Long term your compiler-compiler idea is similar to what I envision XML::Toolkit becoming. I think it should be possible to generate a Schema from XML::Toolkit generated Moose classes. This would allow you to bootstrap to get the kind of clarity you are wanting because you could generate an Schema from an example document, modify the schema to correctly reflect anything not properly reflected in the example document, then generate the final classes from the Schema.

The problem is I don't have a billable project to implement this. Is this something people would be willing to fund a grant for?


In reply to Re: generating XML with data structures and Perl classes - the XML::Element::Tolol approach by Anonymous Monk
in thread generating XML with data structures and Perl classes - the XML::Element::Tolol approach 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.