Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Json schema from XML schema (XSD) [closed]

Does anybody know how to generate a JSON schema from a existing XML schema (XSD file)? Are there any tools available for this?

like image 258
JB Hurteaux Avatar asked Oct 13 '10 08:10

JB Hurteaux


People also ask

How do you convert XSD to JSON Schema?

Plugin for converting an XML Schema (XSD) file to a JSON Schema file. Once installed, go to Tools -> XML Actions, or right-click on an XML Schema file from Project view, and select "Convert XSD to JSON Schema".

Is there an XSD for JSON?

Result: The XSD to JSON Schema dialog box is now available and can be selected from the Tools > JSON Tools menu.

Can you generate XSD from XML?

How to generate/create a schema xsd from an XML document? Step 1: click Open File button and select the xml file from the file system that you have access, or get the xml file from internet via URL, click By URL. Step 2: click the Generate XSD button, the generated schema will be displayed in an indented XML format.

What is Jsonix?

Jsonix (JSON interfaces for XML) is a JavaScript library which allows you to convert between XML and JSON structures. With Jsonix you can parse XML into JSON (this process is called unmarshalling) or serialize JSON in XML form (this is called marshalling).


1 Answers

Disclaimer: I am the author of Jsonix, a powerful open-source XML<->JSON JavaScript mapping library.

Today I've released the new version of the Jsonix Schema Compiler, with the new JSON Schema generation feature.

Let's take the Purchase Order schema for example. Here's a fragment:

  <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>    <xsd:complexType name="PurchaseOrderType">     <xsd:sequence>       <xsd:element name="shipTo" type="USAddress"/>       <xsd:element name="billTo" type="USAddress"/>       <xsd:element ref="comment" minOccurs="0"/>       <xsd:element name="items"  type="Items"/>     </xsd:sequence>     <xsd:attribute name="orderDate" type="xsd:date"/>   </xsd:complexType> 

You can compile this schema using the provided command-line tool:

java -jar jsonix-schema-compiler-full.jar     -generateJsonSchema     -p PO     schemas/purchaseorder.xsd 

The compiler generates Jsonix mappings as well the matching JSON Schema.

Here's what the result looks like (edited for brevity):

{     "id":"PurchaseOrder.jsonschema#",     "definitions":{         "PurchaseOrderType":{             "type":"object",             "title":"PurchaseOrderType",             "properties":{                 "shipTo":{                     "title":"shipTo",                     "allOf":[                         {                             "$ref":"#/definitions/USAddress"                         }                     ]                 },                 "billTo":{                     "title":"billTo",                     "allOf":[                         {                             "$ref":"#/definitions/USAddress"                         }                     ]                 }, ...             }         },         "USAddress":{ ... }, ...     },     "anyOf":[         {             "type":"object",             "properties":{                 "name":{                     "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/QName"                 },                 "value":{                     "$ref":"#/definitions/PurchaseOrderType"                 }             },             "elementName":{                 "localPart":"purchaseOrder",                 "namespaceURI":""             }         }     ] } 

Now this JSON Schema is derived from the original XML Schema. It is not exactly 1:1 transformation, but very very close.

The generated JSON Schema matches the generatd Jsonix mappings. So if you use Jsonix for XML<->JSON conversion, you should be able to validate JSON with the generated JSON Schema. It also contains all the required metadata from the originating XML Schema (like element, attribute and type names).

Disclaimer: At the moment this is a new and experimental feature. There are certain known limitations and missing functionality. But I'm expecting this to manifest and mature very fast.

Links:

  • Demo Purchase Order Project for NPM - just check out and npm install
  • Documentation
  • Current release
  • Jsonix Schema Compiler on npmjs.com
like image 120
lexicore Avatar answered Sep 30 '22 08:09

lexicore