The XML Schema Generator creates an XML Schema (XSD) from a JSON Schema
The XML Schema Generator tool uses a Wizard to create a compliant XML Schema by inferring its structure from a sample JSON Schema. Configuration options allow a strict or lax approach to validation depending on the quality of your sample JSON Schema.
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
Example - Converting a JSON Schema to an XSD
We will use the following sample JSON Schema to demonstrate the effect of the Generation Options:
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "bookstore": { "$ref": "#/$defs/bookstore" } }, "additionalProperties": false, "required": [ "bookstore" ], "$defs": { "bookstore": { "$ref": "#/$defs/bookstoreType" }, "bookstoreType": { "type": "object", "properties": { "book": { "type": "array", "items": { "$ref": "#/$defs/bookType" } } }, "additionalProperties": false }, "bookType": { "type": "object", "properties": { "price": { "type": "number" }, "publicationdate": { "type": "string" }, "ISBN": { "type": "string" }, "title": { "type": "string", "description": "The title of the book. \nMax 50 characters.", "maxLength": 50 }, "author": { "$ref": "#/$defs/authorName" }, "genre": { "type": "string" } }, "additionalProperties": false, "required": [ "price", "title", "author" ] }, "authorName": { "type": "object", "properties": { "first-name": { "type": "string", "description": "The authors first name.\nMax 50 characters." }, "last-name": { "type": "string" } }, "additionalProperties": false, "required": [ "first-name", "last-name" ] } } }
The following XSD is inferred from the sample JSON Schema data above.
<?xml version="1.0" encoding="utf-8"?> <!--Created with Liquid Studio (https://www.liquid-technologies.com)--> <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:group name="bookstoreGroup"> <xs:sequence> <xs:element name="book" minOccurs="0" maxOccurs="1"> <xs:complexType> <xs:sequence> <xs:element name="Item" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:group ref="bookTypeGroup" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:group> <xs:group name="bookstoreTypeGroup"> <xs:sequence> <xs:element name="book" minOccurs="0" maxOccurs="1"> <xs:complexType> <xs:sequence> <xs:element name="Item" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:group ref="bookTypeGroup" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:group> <xs:group name="bookTypeGroup"> <xs:sequence> <xs:element name="price" type="xs:double" minOccurs="1" maxOccurs="1" /> <xs:element name="publicationdate" type="xs:string" minOccurs="0" maxOccurs="1" /> <xs:element name="ISBN" type="xs:string" minOccurs="0" maxOccurs="1" /> <xs:element name="title" type="xs:string" minOccurs="1" maxOccurs="1"> <xs:annotation> <xs:documentation>The title of the book. Max 50 characters.</xs:documentation> </xs:annotation> </xs:element> <xs:element name="author" minOccurs="1" maxOccurs="1"> <xs:complexType> <xs:sequence> <xs:group ref="authorNameGroup" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="genre" type="xs:string" minOccurs="0" maxOccurs="1" /> </xs:sequence> </xs:group> <xs:group name="authorNameGroup"> <xs:sequence> <xs:element name="first-name" type="xs:string" minOccurs="1" maxOccurs="1"> <xs:annotation> <xs:documentation>The authors first name. Max 50 characters.</xs:documentation> </xs:annotation> </xs:element> <xs:element name="last-name" type="xs:string" minOccurs="1" maxOccurs="1" /> </xs:sequence> </xs:group> <xs:element name="Root"> <xs:complexType> <xs:sequence> <xs:element name="bookstore" minOccurs="1" maxOccurs="1"> <xs:complexType> <xs:sequence> <xs:group ref="bookstoreGroup" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Once you have your XSD, 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.