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

Hi Monks. Has anyone seen a tool that will generate Perl code from an XML Schema (ala Microsoft's xsd.exe tool?) It seems like something that would be very useful, but I can't seem to find any thing that generates Perl code (I've seen some that generate Java, and Microsoft's Generates C#). Thanks for the help.

Replies are listed 'Best First'.
Re: Generate Perl Code from XML Schema?
by ayvazj (Novice) on Dec 17, 2003 at 19:48 UTC
    I'll try to explain it better:

    Is there a tool that can generate Perl objects that can read and write XML files that conform to a given XML Schema?

    Given this (simplified example) XML Schema:
    <!-- W3C XML Schema --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefa +ult="qualified" attributeFormDefault="unqualified"> <xs:element name="Product"> <xs:complexType> <xs:all> <xs:element name="Owners"> <xs:complexType> <xs:sequence> <xs:element name="FirstName" type="xs:string" /> <xs:element name="LastName" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:all> </xs:complexType> </xs:element>

    The tool should generate all of the Perl Objects necessary so that I can access Read/Write XML files that conform to the Schema.
    Example Code:

    require AutoGeneratedCode; my $Product = new AutoGeneratedCode::Product->new('product.xml'); +# product.xml is an XML document that conforms to the Schema above $Product->Owners->FirstName('Mark'); $Product->Owners->LastName('Twain'); $Product->Write('modifiedproduct.xml'); # Writes it self to XML fi +le that conforms to Schema above
Re: Generate Perl Code from XML Schema?
by talexb (Chancellor) on Dec 17, 2003 at 18:38 UTC

    There is a review of xsd.exe here that explains that xsd.exe creates XML Schema documents (XSDs) .. This message contains the announcement (a year ago) about the tool, but all xsd appears to do is assist in validating XML.

    How does that relate to your question about something that generates Perl code? Would the Perl code be a factory object that would generate objects according to an XML schema? Do you have links to the Java and C# translations you've mentioned?

    We need more information before we can offer any solutions.

    --t. alex
    Life is short: get busy!
Re: Generate Perl Code from XML Schema?
by PodMaster (Abbot) on Dec 17, 2003 at 23:15 UTC
Re: Generate Perl Code from XML Schema?
by dragonchild (Archbishop) on Dec 17, 2003 at 18:22 UTC
    Forgive me, but I'm a little confused as to why you'd even want to do this. Please elaborate and maybe one of us can point you towards something ... I know of a few things that might do most of the trick, but they're kinda different ...

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Having done a bit of WSDL work in java w/ SOAP...

      While the author of a webservice can generate a WSDL file from java, the client may not have nor get a java file representing the soap call typeor return type. It's a matter of convenience and accuracy in recreating the class as it was intended. It's not the only way of using SOAP, but if you wish to use dynamic proxies, going from WSDL -> java (or perl or whatever) is a lot shorter to write.

      Typically, w/ dynamic proxy, you create and use objects that work remotely and passed back in fashion as if they were locally there. SOAP would create the objects for you via some factory, sometimes off of WSDL.

      example from SOAP::Lite

      use SOAP::Lite; $service = SOAP::Lite -> uri('urn:QuotationService') -> proxy('http://localhost:8080/soap/servlet/rpcrouter' +); $result = $service -> getAllQuotations() -> result(); $i = -1; while ( ref $result->[++$i] ) { print "$result->[$i]{'text'} ($result->[$i]{'author'})\n"; }

      If the author of SOAP::Lite, for perl6, wants to make hashes that cannot be assigned arbitrary keys, or for speed reasons, create classes from pre-existing object files, may need to pregenerate the perl. I'm still fuzzy on how perl6 works, so forgive if I use the wrong terminology :) I'm sure there can be a way of generating objects and later choosing to lock down their attributes.

      So anyway, having pre-generated code for wsdl is always useful for soap writters (toolkit or end user) who would need type clarification for knowing what's accessible and what doesn't exist. After all, what you pay for in dynamic-ness, you save by apriori knowledge especially in intense applications.


      Play that funky music white boy..
Re: Generate Perl Code from XML Schema?
by ayvazj (Novice) on Dec 17, 2003 at 19:53 UTC
    The reason I need a tool like this is that at work we're writing an cross platform app that stores data in XML files defined by a schema. My coworker using C# uses Xsd.exe to autogenerate his classes based on the schema, whereas I have to manually create classes that can read / write the XML files properly. Whenever there is a change to the schema my C# coworker updates his code in a matter of seconds, I have to manually go through all of my code making changes.
      Depending on exactly what you want to do with the generated "code" (all I can see in your example are some accessors) and provided your XML files are reasonably small, you might be able to get away with XML::Simple. This will NOT generate code or classes from schemas, but it WILL generate a nice Perl-y datastructure from XML files.

      From the docs:

      That seems to be what you want from where I'm standing, anyway :-)

      Joost.

      Hello ayvazj. From your posts and the replies of monks more experienced that I am, I'm starting to suspect that you're not getting the answers you need in part because you are asking for information about a solution you have decided is appropriate.

      It would be better to fully describe the problem and let the skills and imagination of the monks suggest solutions. (you'll probably get more than one!)

      -theo-
      (so many nodes and so little time ... )

Re: Generate Perl Code from XML Schema?
by Anonymous Monk on Dec 18, 2003 at 09:46 UTC

    Andy Wardley has implemented a seminal Module on this: XML::Schema. As far as i remember it really reads xml-snippets and validates against it.

    PS: As far as i know not all facets/types are implemented, but most. The code is very clean and i am sure that patches would be wellcome ;)

    Murat

    20031218 Edit by Corion: Removed PRE tags