从XML模式(XSD)生成Json模式

时间:2022-01-14 17:17:44

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

有人知道如何从现有的XML模式(XSD文件)生成JSON模式吗?这方面有什么工具吗?

6 个解决方案

#1


25  

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

声明:我是Jsonix的作者,Jsonix是一个强大的开源XML<->JSON JavaScript映射库。

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

今天,我发布了Jsonix模式编译器的新版本,带有新的JSON模式生成特性。

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.

编译器生成Jsonix映射以及匹配的JSON模式。

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.

现在,这个JSON模式源自原始的XML模式。这不是1:1的变换,但是非常非常接近。

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).

生成的JSON模式匹配generatd Jsonix映射。因此,如果您使用Jsonix进行XML<->JSON转换,您应该能够使用生成的JSON模式验证JSON。它还包含来自原始XML模式(如元素、属性和类型名称)所需的所有元数据。

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:

链接:

#2


10  

A year after this question was originally asked, JSON Schema remains an IETF draft document. As of this writing (18 October 2011) the working group is trying to get agreement on draft 4 of the specification. Although there are a few speculative validation implementations (see http://json-schema.org/), most tool vendors haven't invested much effort into tools that implement JSON Schema development, editing or conversion.

在这个问题最初被提出一年后,JSON模式仍然是一个IETF草稿文档。在撰写本文时(2011年10月18日),工作组正试图就规范草案4达成一致。虽然有一些推测验证实现(参见http://json-schema.org/),但是大多数工具供应商并没有投入太多精力到实现JSON模式开发、编辑或转换的工具上。

#3


6  

JSON Schema is not intended to be feature equivalent with XML Schema. There are features in one but not in the other.

JSON模式并不打算与XML模式具有相同的特性。其中有一个特性,另一个却没有。

In general you can create a mapping from XML to JSON and back again, but that is not the case for XML schema and JSON schema.

通常,您可以从XML创建一个映射到JSON,然后再创建一个映射,但XML模式和JSON模式不是这样的。

That said, if you have mapped a XML file to JSON, it is quite possible to craft an JSON Schema that validates that JSON in nearly the same way that the XSD validates the XML. But it isn't a direct mapping. And it is not possible to guarantee that it will validate the JSON exactly the same as the XSD validates the XML.

也就是说,如果您已经将XML文件映射到JSON,那么很有可能创建一个JSON模式,以与XSD验证XML几乎相同的方式验证JSON。但它不是直接映射。而且不可能保证它将验证JSON,就像XSD验证XML一样。

For this reason, and unless the two specs are made to be 100% feature compatible, migrating a validation system from XML/XSD to JSON/JSON Schema will require human intervention.

因此,除非这两个规范是100%兼容的,否则将验证系统从XML/XSD迁移到JSON/JSON模式将需要人工干预。

#4


3  

Disclaimer: I'm the author of jgeXml.

免责声明:我是jgeXml的作者。

jgexml has Node.js based utility xsd2json which does a transformation between an XML schema (XSD) and a JSON schema file.

jgexml节点。基于js的实用程序xsd2json,在XML模式(XSD)和JSON模式文件之间进行转换。

As with other options, it's not a 1:1 conversion, and you may need to hand-edit the output to improve the JSON schema validation, but it has been used to represent a complex XML schema inside an OpenAPI (swagger) definition.

与其他选项一样,它不是一个1:1的转换,您可能需要手工编辑输出来改进JSON模式验证,但是它已经被用于在OpenAPI (swagger)定义中表示一个复杂的XML模式。

A sample of the purchaseorder.xsd given in another answer is rendered as:

purchaseorder的示例。另一个答案中给出的xsd表示为:

"PurchaseOrderType": {
  "type": "object",
  "properties": {
    "shipTo": {
      "$ref": "#/definitions/USAddress"
    },
    "billTo": {
      "$ref": "#/definitions/USAddress"
    },
    "comment": {
      "$ref": "#/definitions/comment"
    },
    "items": {
      "$ref": "#/definitions/Items"
    },
    "orderDate": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}.*$"
    }
  },

#5


0  

Copy your XML schema here & get the JSON schema code to the online tools which are available to generate JSON schema from XML schema.

在这里复制您的XML模式&获取JSON模式代码到可以从XML模式生成JSON模式的在线工具。

#6


-3  

True, but after turning json to xml with xmlspy, you can use trang application (http://www.thaiopensource.com/relaxng/trang.html) to create an xsd from xml file(s).

确实,但是在使用xmlspy将json转换为xml之后,您可以使用trang应用程序(http://www.thaiopensource.com/弛缓ng/trang.html)从xml文件创建xsd。

#1


25  

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

声明:我是Jsonix的作者,Jsonix是一个强大的开源XML<->JSON JavaScript映射库。

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

今天,我发布了Jsonix模式编译器的新版本,带有新的JSON模式生成特性。

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.

编译器生成Jsonix映射以及匹配的JSON模式。

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.

现在,这个JSON模式源自原始的XML模式。这不是1:1的变换,但是非常非常接近。

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).

生成的JSON模式匹配generatd Jsonix映射。因此,如果您使用Jsonix进行XML<->JSON转换,您应该能够使用生成的JSON模式验证JSON。它还包含来自原始XML模式(如元素、属性和类型名称)所需的所有元数据。

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:

链接:

#2


10  

A year after this question was originally asked, JSON Schema remains an IETF draft document. As of this writing (18 October 2011) the working group is trying to get agreement on draft 4 of the specification. Although there are a few speculative validation implementations (see http://json-schema.org/), most tool vendors haven't invested much effort into tools that implement JSON Schema development, editing or conversion.

在这个问题最初被提出一年后,JSON模式仍然是一个IETF草稿文档。在撰写本文时(2011年10月18日),工作组正试图就规范草案4达成一致。虽然有一些推测验证实现(参见http://json-schema.org/),但是大多数工具供应商并没有投入太多精力到实现JSON模式开发、编辑或转换的工具上。

#3


6  

JSON Schema is not intended to be feature equivalent with XML Schema. There are features in one but not in the other.

JSON模式并不打算与XML模式具有相同的特性。其中有一个特性,另一个却没有。

In general you can create a mapping from XML to JSON and back again, but that is not the case for XML schema and JSON schema.

通常,您可以从XML创建一个映射到JSON,然后再创建一个映射,但XML模式和JSON模式不是这样的。

That said, if you have mapped a XML file to JSON, it is quite possible to craft an JSON Schema that validates that JSON in nearly the same way that the XSD validates the XML. But it isn't a direct mapping. And it is not possible to guarantee that it will validate the JSON exactly the same as the XSD validates the XML.

也就是说,如果您已经将XML文件映射到JSON,那么很有可能创建一个JSON模式,以与XSD验证XML几乎相同的方式验证JSON。但它不是直接映射。而且不可能保证它将验证JSON,就像XSD验证XML一样。

For this reason, and unless the two specs are made to be 100% feature compatible, migrating a validation system from XML/XSD to JSON/JSON Schema will require human intervention.

因此,除非这两个规范是100%兼容的,否则将验证系统从XML/XSD迁移到JSON/JSON模式将需要人工干预。

#4


3  

Disclaimer: I'm the author of jgeXml.

免责声明:我是jgeXml的作者。

jgexml has Node.js based utility xsd2json which does a transformation between an XML schema (XSD) and a JSON schema file.

jgexml节点。基于js的实用程序xsd2json,在XML模式(XSD)和JSON模式文件之间进行转换。

As with other options, it's not a 1:1 conversion, and you may need to hand-edit the output to improve the JSON schema validation, but it has been used to represent a complex XML schema inside an OpenAPI (swagger) definition.

与其他选项一样,它不是一个1:1的转换,您可能需要手工编辑输出来改进JSON模式验证,但是它已经被用于在OpenAPI (swagger)定义中表示一个复杂的XML模式。

A sample of the purchaseorder.xsd given in another answer is rendered as:

purchaseorder的示例。另一个答案中给出的xsd表示为:

"PurchaseOrderType": {
  "type": "object",
  "properties": {
    "shipTo": {
      "$ref": "#/definitions/USAddress"
    },
    "billTo": {
      "$ref": "#/definitions/USAddress"
    },
    "comment": {
      "$ref": "#/definitions/comment"
    },
    "items": {
      "$ref": "#/definitions/Items"
    },
    "orderDate": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}.*$"
    }
  },

#5


0  

Copy your XML schema here & get the JSON schema code to the online tools which are available to generate JSON schema from XML schema.

在这里复制您的XML模式&获取JSON模式代码到可以从XML模式生成JSON模式的在线工具。

#6


-3  

True, but after turning json to xml with xmlspy, you can use trang application (http://www.thaiopensource.com/relaxng/trang.html) to create an xsd from xml file(s).

确实,但是在使用xmlspy将json转换为xml之后,您可以使用trang应用程序(http://www.thaiopensource.com/弛缓ng/trang.html)从xml文件创建xsd。