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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3922026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:36:03  来源:igfitidea点击:

Generate Json schema from XML schema (XSD)

xmljsonxsdtransformjsonschema

提问by JB Hurteaux

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

有人知道如何从现有的 XML 模式(XSD 文件)生成JSON 模式吗?有没有可用的工具?

采纳答案by lexicore

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

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

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

今天我发布了新版本的Jsonix Schema Compiler,带有新的JSON Schema 生成功能。

Let's take the Purchase Orderschema 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 mappingsas well the matching JSON Schema.

编译器生成Jsonix 映射以及匹配的 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.

现在这个 JSON Schema 派生自原始的 XML Schema。它不完全是 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 模式与生成的 Jsonix 映射匹配。因此,如果您使用 Jsonix 进行 XML<->JSON 转换,您应该能够使用生成的 JSON Schema 验证 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:

链接:

回答by Eric Hartford

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

JSON Schema 不打算与 XML Schema 具有等效的功能。一个有特点,而另一个没有。

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 模式将需要人工干预。

回答by MikeRalphson

Disclaimer: I'm the author of jgeXml.

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

jgexmlhas Node.js based utility xsd2jsonwhich does a transformation between an XML schema (XSD) and a JSON schema file.

jgexml具有基于 Node.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}.*$"
    }
  },

回答by Karl Parker

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

在此处复制您的 XML 模式并将 JSON 模式代码获取到可用于从 XML 模式生成 JSON 模式的在线工具

回答by Spokk

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/relaxng/trang.html) 从 xml 文件创建一个 xsd。