The XML Schema Generator creates an XML Schema (XSD) from XML Documents
The XML Schema Generator tool uses a Wizard to create a compliant XML Schema by inferring its structure from sample XML documents. Configuration options allow a strict or lax approach to validation depending on the quality of your sample XML documents.
The pipeline for rapid XML Schema creation is as follows:
Why use an XML Schema?
- Provides a formal, unambiguous description, essential for distribution to 3rd parties
- Validation of XML documents.
- XML development tools use them to provide intellisense and autocomplete
- Reduces the amount of validation code needed in client applications
- Basis for generating a data layer for your application (XML Data Binding)
- Forms the basis of formal documentation for your data model
Inferring an XSD
You can infer an XML Schema from an XML file open in the Liquid Studio editor, or using the quick start wizard on the start screen.
The Generation Options configure how the wizard should interpret the elements and attributes in the source data. When the XML to XSD converter processes the source 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, the source data may not provide sufficient information to infer the data type of an element or attribute. 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 stand-alone 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.
Example - Inferring an XSD from XML Sample Documents
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.
Notice that the data types are all strings and cardinality on all the elements and attributes is optional.
<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>
The following XSD is inferred from the sample XML data above when both occurrence and type inference are set to restricted
Notice this time the elements and attributes are required and the data types have been assigned based on the data provided in the sample document.
<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>
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.