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

Hi all,

I have been generating XML in a way without consulting the XSDs so during validation they fail, am sure there is a way to generate XML using the XSDs itself

Could somebody please help me with that?

Here is the part of the XSD
<xsd:element name="first_element"> <xsd:complexType> <xsd:sequence> <xsd:element name="val"/> </xsd:sequence> <xsd:attribute name="is_added" type="yes_or_no" use="optional"/> </xsd:complexType> </xsd:element>
and here is the sample XML file that am looking for
<first_element yes_or_no="yes"> <val> This is a sample text - auto generated </val> </first_element>
Can somebody provide me with some pointers on how to do that? Many thanks in advance.

Replies are listed 'Best First'.
Re: How to generate XML file from XSDs ?
by Cody Pendant (Prior) on Nov 13, 2008 at 05:32 UTC
    I don't really know much about XSDs but I think your problem is, you're generating an attribute called "yes_or_no". The attribute needs to be called "is_added" and the value of it should be "yes" or "no".

    But as you haven't shown us the code which generates that, we can't fix it.



    Nobody says perl looks like line-noise any more
    kids today don't know what line-noise IS ...
      Many thanks for the reply.

      I think am correct in that. For the sample XSD I had given that should be the proper XML output.

      is_added is an attribute to the element - first_element.
Re: How to generate XML file from XSDs ?
by poolpi (Hermit) on Nov 13, 2008 at 12:06 UTC
      But the module XML::Validator::Schema is for validating XML against schema and not for generating XML using XSDs.

      However, I found an interesting link from that page which generates XML ( but not using XSDs or schemas )
      XML::Generator
        Hi, I was having the same question as you.

        After some searching I found XML::Compile, more specifically XML::Compile::Translate::Writer, which might do the right thing.

        Since I don't have my coding env. available right now, I am not able to test it. But you may give it a try.

        --Nils

Re: How to generate XML file from XSDs ?
by dHarry (Abbot) on Nov 13, 2008 at 09:13 UTC

    Are you sure your schema itself is valid? Please post the entire schema, the fragment is not sufficient, i.e. I assume you define a "yes-or-no" type somewhere in the schema. Like brother Cody Pendant already stated you use the wrong attribute name.

      Thanks again for the reply

      Much appreciated !

      I have taken another simple XSD and XML file for sample to avoid confusions that I have might have made

      Here it is,

      XSD

      <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:books" xmlns:bks="urn:books"> <xsd:element name="books" type="bks:BooksForm"/> <xsd:complexType name="BooksForm"> <xsd:sequence> <xsd:element name="book" type="bks:BookForm" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="BookForm"> <xsd:sequence> <xsd:element name="author" type="xsd:string"/> <xsd:element name="title" type="xsd:string"/> <xsd:element name="genre" type="xsd:string"/> <xsd:element name="price" type="xsd:float" /> <xsd:element name="pub_date" type="xsd:date" /> <xsd:element name="review" type="xsd:string"/> </xsd:sequence> <xsd:attribute name="id" type="xsd:string"/> </xsd:complexType> </xsd:schema>
      XML file

      <?xml version="1.0"?> <x:books xmlns:x="urn:books"> <book id="bk001"> <author>Writer</author> <title>The First Book</title> <genre>Fiction</genre> <price>44.95</price> <pub_date>2000-10-01</pub_date> <review>An amazing story of nothing.</review> </book> <book id="bk002"> <author>Poet</author> <title>The Poet's First Poem</title> <genre>Poem</genre> <price>24.95</price> <review>Least poetic poems.</review> </book> </x:books>
      With this, how to generate a XML file with XSD. The reason behind the question is, I have a perl datastructure and with that I need to generate XML file that validates the XSD.

        I know of no Perl module that generates an XML document based on an input XSD. There are tools available that do it however (Google;-). I myself use XMLSpy for that. See below for an example generated with XMLSpy (runs under Windows only). Look for oXygen for a platform independent solution, it's cheap and good and has an "XML Instance Generator" too.

        <?xml version="1.0" encoding="UTF-8"?> <!--Sample XML file generated by XMLSPY v2004 rel. 4 U (http://www.xml +spy.com)--> <bks:books xmlns:bks="urn:books" xmlns:xsi="http://www.w3.org/2001/XML +Schema-instance" xsi:schemaLocation="urn:books theSchema.xsd"> <book id="String"> <author>String</author> <title>String</title> <genre>String</genre> <price>3.14159</price> <pub_date>1967-08-13</pub_date> <review>String</review> </book> </bks:books>

        The XML file you give is not valid by the way. Error message: "Mandatory ‘pub_date’ element expected in place of ‘review’."

        There are many Perl modules to work with XML. With some effort you can write a Perl script that generates a valid XML file out of an XSD.

        HTH

        UPDATE
        Some XIGs (XML Instance Generators):