Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

2年前关闭。



Improve this question




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

最佳答案

免责声明:我是Jsonix的作者,Jsonix Schema Compiler是一个功能强大的开源XML JSON JavaScript映射库。

今天,我发布了具有新JSON Schema generation功能的Purchase Order新版本。

让我们以Jsonix mappings模式为例。这是一个片段:

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

您可以使用提供的命令行工具来编译此架构:
java -jar jsonix-schema-compiler-full.jar
    -generateJsonSchema
    -p PO
    schemas/purchaseorder.xsd

编译器会生成matching JSON Schema以及limitations and missing functionality

结果如下所示(为简洁起见编辑):
{
    "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转换,但非常接近。

生成的JSON模式与生成的Jsonix映射匹配。因此,如果将Jsonix用于XML JSON转换,则应该能够使用生成的JSON Schema验证JSON。它还包含来自原始XML模式的所有必需元数据(例如元素,属性和类型名称)。

免责声明:目前,这是一个新的实验性功能。有某些已知的Demo Purchase Order Project for NPM。但我希望这会很快显现并成熟。

链接:
  • Documentation-只需 checkout npm install
  • Current release
  • Jsonix Schema Compiler on npmjs.com
  • ojit_a
  • 07-28 00:54
    查看更多