in reply to Re: How to generate XML file from XSDs ?
in thread How to generate XML file from XSDs ?

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.

Replies are listed 'Best First'.
Re^3: How to generate XML file from XSDs ?
by dHarry (Abbot) on Nov 13, 2008 at 12:40 UTC

    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):