从 XML 模式 (XSD) 生成 Json 模式 [英] Generate Json schema from XML schema (XSD)

查看:28
本文介绍了从 XML 模式 (XSD) 生成 Json 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

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

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

编译器生成 Jsonix 映射 以及 匹配 JSON Schema.

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":""
            }
        }
    ]
}

现在这个 JSON Schema 是从原始 XML Schema 派生的.这不是 1:1 的转换,而是非常非常接近.

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

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

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.

链接:

  • Demo Purchase Order Project for NPM - just check out and npm install
  • Documentation
  • Current release
  • Jsonix Schema Compiler on npmjs.com

这篇关于从 XML 模式 (XSD) 生成 Json 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆