本文介绍了从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模式生成功能.

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映射以及.

结果如下所示(为简洁起见进行了编辑):

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架构派生自原始XML架构.这不是精确的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模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:22