# How Do I: Generate Schema from an XML Instance Document?

You can use a third-party XML authoring tool like Altova's XMLSpy to generate XML schema from an XML instance document.

This procedure describes a simple way to generate schema from an instance, but is just a beginning. As with many wizard-style conversion tools, the results of this one are a "best guess" at getting the results you want. Keep in mind the following:

- The options provided by XMLSpy for generating schema affect the sort of schema you get. You may want to try generating with different options.
- You will likely want to edit the generated schema to make sure it efficiently describes all the possible instances expected to conform to it. Try generating, editing, then validating XML against the new schema.
- Finally, try compiling the generated schema to see if the resulting API meets your needs.

To Generate Schema from an Instance Document in XMLSpy

1. Open your XML file in XMLSpy.
2. From the DTD/Schema menu, click Generate DTD/Schema.
3. In the Generate DTD/Schema dialog, select the options that suit your needs. Here are suggestions:
   - Under DTD/Schema file format, select W3C Schema.
   - Under List of values, select Only if not more than 20.
   - Under Attribute/Element type detection, select Best possible.
   - Under Represent complex elements as, select Complex types.
   - Under Elements which were used once, select Make local definition.
4. Click OK.
5. When prompted to assign the schema to your XML document, choose:
   - Yes, if you want to immediately save the schema XSD file and assign it to your XML document. With the schema assigned to the XML, you can validate the XML against the schema as you edit the schema. Note that XMLSpy will write a schema assignment declaration into your XML file.
   - No, if you want to edit the XSD file before saving it, or if you don't want XMLSpy to edit your XML file by adding a schema declaration.
6. Edit the generated schema to meet your needs.

Once you have finished your schema, you can compile it to generated Java types. See How Do I: Use XML Schema in WebLogic Workshop? for more information.

Related Topics

Getting Start with XMLBeans