XML Schema Generator XML Schema Generator

Home > Products > Liquid XML Studio > Infer XML Schema

The XML Schema Generator is a tool which creates an XML Schema (XSD) from XML documents.

The advantages of having an XML Schema include:

  • Providing a formal unambiguous description of your data.
  • An aid to XML development as it provides intellisense and validation.
  • Reducing the amount of validation code needed in client applications.

The XML Schema Generator tool creates an XML Schema in an instant by inferring data of XML documents. Simply add all the sample XML files to the easy to use wizard and a compliant XML Schema is generated. Configuration options allow you take a strict or lax approach to validation depending on the quality of your sample XML documents.

Why use XML Schema?

XSDs can be a real benefit to most projects involving XML, as you can use an XSD to validate your data. This can potentially cut down on required processing stages in client applications, reducing and in some cases removing the need for error handling to validate incoming data. Rather than having to design your data structures at an abstract level, then build compliant XML, inferring an XML Schema from existing XML data allows you to automate the process, at least in part.

When you use the Liquid XML Studio wizard to infer a Schema from XML data, you can configure how the data will be interpreted to form the resulting XSD. You can carry out additional amendments to your XSDs as necessary in the XML Schema editor, which includes refactoring tools. Once you have an XML Schema defined, you can then use it to validate any data you work with as part of the same project, allowing you to develop for robust, reliable applications. Having a formalized definition of the data structures within your projects is particularly useful where more than one developer may be involved.

Inferring an XSD using the Wizard

You can infer an XML Schema from any XML file you have open in the Liquid XML Studio editor. Clicking the "Infer XSD Schema" button or choosing from the Tools menu lets you open the wizard. Here you can choose a name and location for your Schema. You can also select character encoding for the generated XSD.

With the Generation Options, you can also configure how the wizard should interpret the elements and attributes it encounters in the source data - in terms of occurrence and type. When the wizard infers a Schema from XML data, there will often be more than one potential interpretation. Depending on the amount of source data, it may not be clear whether there is a minimum or maximum number of times an element must appear, or whether a particular attribute is required or optional. Similarly, sample data may not provide sufficient information to infer the data type either an element or an attribute should have. To address these potential issues, you can opt for relaxed or restricted inferring of both occurrence and type.

Occurrence

If you choose to infer your XML Schema with the relaxed occurrence option, the XSD will assume a minimum occurrence of zero for any elements encountered in the source data, and any attributes will be interpreted as optional. If you opt for restricted occurrence inference, all elements in the source will be assumed to have a minimum occurrence of one, and all attributes will be listed as required. NB: Within an XSD, the default value for minimum occurrence of an element is one, in which case no "minOccurs" attribute needs to be listed.

Type

If you select relaxed type inference, your generated XSD will indicate a string data type for all simple type elements and attribute values. If you select restricted type inference, the wizard will attempt to infer more data types, including number types, dates etc.

Whichever options you choose for inferring your XML Schemas, it is advisable to check the results and make any amendments necessary - in many cases the inferred XSD will be adequate for your project, but for more complex data structures it should be treated more as a starting point than a standalone solution to formalize your data structures.

Sample Data

Once you have your inference options selected, clicking Next will take you to the sample files screen. Here you can add any documents with additional sample data you want to use for inferring the XML Schema. In general, the more data you feed into the process, the more accurate the resulting XSD is likely to be. Clicking Finish will prompt the Schema to be inferred and opened in the XSD editor.

Inferred XML Schema

With your generated XSD open, you can see how the elements, attributes and structures have been inferred. At this point you should check through the Schema and ensure it matches your requirements for the project. You can carry out edits to the code directly, or use the design view by right-clicking elements and attributes. In this menu you can choose to amend details of the XSD such as cardinality, as well as utilizing refactoring options if necessary.

We will use the following sample XML to demonstrate the effect of the Generation Options:

<clients>
    <department name="retail">
        <customer type="corporate">
            <cust_name>Global Solutions Inc</cust_name>
            <cust_ref>021125</cust_ref>
            <cust_since>2009-10-20</cust_since>
        </customer>
        <customer type="individual">
            <cust_name>Mark Jones</cust_name>
            <cust_ref>015547</cust_ref>
            <cust_since>2011-09-18</cust_since>
        </customer>
        <customer type="corporate">
            <cust_name>Excellent Products Ltd</cust_name>
            <cust_ref>032201</cust_ref>
            <cust_since>2010-06-22</cust_since>
        </customer>
    </department>
    <department name="consulting">
        <customer type="corporate">
            <cust_name>Quality Services UK</cust_name>
            <cust_ref>121778</cust_ref>
            <cust_since>2012-04-12</cust_since>
        </customer>
        <customer type="corporate">
            <cust_name>Premier Stockists Ltd</cust_name>
            <cust_ref>149852</cust_ref>
            <cust_since>2011-03-15</cust_since>
        </customer>
    </department>
</clients>

The following XSD is inferred from the sample XML data above when both occurrence and type inference are set to relaxed:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="clients">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="department">
          <xs:complexType>
            <xs:sequence>
              <xs:element minOccurs="0" maxOccurs="unbounded" name="customer">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element minOccurs="0" name="cust_name" type="xs:string" />
                    <xs:element minOccurs="0" name="cust_ref" type="xs:string" />
                    <xs:element minOccurs="0" name="cust_since" type="xs:string" />
                  </xs:sequence>
                  <xs:attribute name="type" type="xs:string" use="optional" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" use="optional" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Notice that each element includes a "minOccurs" attribute of zero and each attribute is "optional". All simple type elements and attribute values in the XSD are listed as having a string data-type.

The following XSD results from choosing restricted for both occurrence and type inference:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="clients">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="department">
          <xs:complexType>
            <xs:sequence>
              <xs:element maxOccurs="unbounded" name="customer">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="cust_name" type="xs:string" />
                    <xs:element name="cust_ref" type="xs:unsignedInt" />
                    <xs:element name="cust_since" type="xs:date" />
                  </xs:sequence>
                  <xs:attribute name="type" type="xs:string" use="required" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Notice that this time, the elements do not have a "minOccurs" attribute - this is because the default value is one, so there is no need to list the attribute directly, where in the relaxed version, the attribute was set to zero. Notice also that this time each attribute is listed as "required". If you look at the "cust_ref" and "cust_since" elements, you'll see that the restricted type inferencing has also interpreted numerical (unsigned integer) and date types from the sample data, unlike the relaxed case, where string type is assumed throughout.

Once you have your XSD the way you want it, you can test it out by using it to validate some XML data you plan on using with your project. With the XML file open, add the XSD to the document by selecting "Attach Schema" from the toolbar or Tools menu and browsing to an XSD file you inferred. Once the XML Schema has been cited within the document, clicking the Validate button or choosing it from the Tools menu will check the XML against the generated XSD.

Once you have a Schema added to an XML document, you will see Schema-aware intellisense as you edit the data in the source view, prompting you with elements and attributes appropriate to the structures defined within the Schema, relative to your position in the data.

Video Tutorials

VIEW TUTORIAL XML Schema Editor Overview

XML Schema Editor Overview

This video overview shows how to work in the graphical, text and split views, adding items, changing properties and updating documentation annotations.

VIEW TUTORIAL Generate a Sample XML Document

Generate a Sample XML Document

This video tutorial shows you how to create a sample XML document from an industry standard schema (Google Site Map).

VIEW TUTORIAL Generate HTML Documentation

Generate HTML Documentation

This video tutorial shows how to create HTML documentation for an XML Schema.

VIEW TUTORIAL XML Data Binder

XML Data Binding

This video tutorial shows you how to generate strongly typed API source code from your XSD using the Liquid XML Data Binder.

VIEW TUTORIAL XML Data Mapper

XML Data Mapper

This video tutorial shows how to create a mapping transform, using the graphical drag and drop interface, to convert XML data of different XML Schema.

Like what you see? Try Liquid XML Free Free Trial

More XML Tools

FEATURE DETAILS

Graphical XML Schema Editor

Visualize and edit an abstracted view of your XML schema (XSD) using an intuitive user interface, and validate your XSD against the W3C standards. Includes split graphical and text views, intellisense, syntax highlighting, drag and drop, copy and paste, and multi-step undo/redo.

FEATURE DETAILS Graphical WSDL Editor

Graphical WSDL Editor

Visualize and edit an abstracted view of your Web Service using an intuitive user interface, and validate your WSDL against the W3C standards. Includes split graphical and text views, intellisense, syntax highlighting, drag and drop, copy and paste, and multi-step undo/redo.

FEATURE DETAILS XSLT Editor and Debugger

XSLT Editor and Debugger

Author, validate and debug your XSLT style sheet, set breakpoints within the XSLT source, watch variables and analyse the call stack. Includes intellisense, syntax highlighting, drag and drop, copy and paste, and multi-step undo/redo.

FEATURE DETAILS XQuery Editor and Debugger

XQuery Editor and Debugger

Author, validate and debug your XQuery module or library, set breakpoints within the XSLT source, watch variables and analyse the call stack. Includes intellisense, syntax highlighting, drag and drop, copy and paste, and multi-step undo/redo.