/xsd2json

Translate XML Schema into equivalent JSON Schema

Primary LanguagePrologMIT LicenseMIT

xsd2json

Translate an XML Schema into equivalent JSON Schema based on SWI-Prolog and Constraint Handling Rules (CHR). For usage directly in Prolog or node.js.

Installation

First, make sure SWI-Prolog is installed. This module expects the swipl binary to be in PATH somewhere. In other words, type this:

swipl

If that works, so will xsd2json.

Using npm, you can install xsd2json by calling this:

npm install -g xsd2json

Or simply clone this repository and manually run the preinstall scripts to create pre-compiled Prolog files:

git clone https://github.com/fnogatz/xsd2json.git
npm run preinstall

Usage as CLI

If you install xsd2json via npm it will create a new command xsd2json:

xsd2json /path/to/your.xsd > /created/schema.json

Usage with node.js

The xsd2json module can be used programmatically as function or stream:

var xsd2json = require('xsd2json');
var filename = 'test.xsd';

// use as stream: Read from stdin
xsd2json(filename)
  .pipe(process.stdout);

// use as function
xsd2json(filename, function(err, schemaObject) {
  console.log(JSON.stringify(schemaObject, null, 2));
});

In addition to the command line options provided by the Prolog module, there are the following options available in the node.js module:

  • noExe: true | false (default): Use the native Prolog interface instead of the pre-compiled cli.exe. This might be useful for MacOS users (see issue #87 for more details).
  • swi: 'swipl' (default): Executable to call SWI-Prolog.

Usage with Prolog

xsd2json provides a predicate xsd2json(+XSD,-JSON), which holds for a given XML Schema (either file path, URL or stream). For instructions on how to use xsd2json programmatically in Prolog, have a look at the Prolog module's Readme.

Background

This tool has been developed as part of my Bachelor's Thesis at University of Ulm, Germany. The thesis (PDF) explains the general idea of the translation process via Prolog and Constraint Handling Rules (CHR). It also contains tabular and graphical representations of the implemented translations of XML Schema fragments. A shorter explanation of the translation process can be found in the CHR Workshop paper "From XML Schema to JSON Schema: Translation with CHR".

Project Structure

xsd2json is developed in a test-driven way. This reflects in the project's folder structure, too: The /lib-pl directory contains the Prolog and CHR stuff while you will find the TAP testing framework implemented in node.js under /test. Both directories contain their own Readme-File that explain their usage.

Known Bugs

xsd2json's tests are always executed using the latest stable release of SWI-Prolog. Make sure to have the latest version installed before you file a bug.

Apparently SWI-Prolog of version 7.2.x ships with a bug in the CHR implementation. Use version 7.4 or higher instead.

Alternatives