将xsd转换为json模式(Jsonix,XMLSpy)

时间:2016-11-27 18:02:07

标签: json xml xsd jsonschema jsonix

我正在尝试将xml架构转换为json架构。

在某些原因,我得到的json架构很奇怪,而且不正确。我使用Jsonix和Altova XMLSpy 2017

例如,这是给定的xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://jaxb.vo.dozer.org/Employee" xmlns:tns="http://jaxb.vo.dozer.org/Employee">
  <complexType name="EmployeeType">
    <sequence>
      <element name="FirstName" type="string" minOccurs="1" maxOccurs="1"></element>
      <element name="BirthDate" type="date" minOccurs="0" maxOccurs="1"></element>
      <element name="LastName" type="string" minOccurs="1" maxOccurs="1"></element>
    </sequence>
  </complexType>

  <element name="Employee" type="tns:EmployeeType"></element>
  <element name="EmployeeWithInnerClass">
    <complexType>
      <sequence>
        <element name="FirstName" type="string" minOccurs="1" maxOccurs="1"></element>
        <element name="BirthDate" type="date" minOccurs="0" maxOccurs="1"></element>
        <element name="LastName" type="string" minOccurs="1" maxOccurs="1"></element>
        <element name="Address">
          <complexType>
            <sequence>
              <element name="Street" type="string"></element>
            </sequence>
          </complexType>
        </element>
      </sequence>
    </complexType>
  </element>
</schema>

这是我从Jsonix获得的json架构:

var org_dozer_vo_jaxb_employee_Module_Factory = function () {
  var org_dozer_vo_jaxb_employee = {
    name: 'org_dozer_vo_jaxb_employee',
    typeInfos: [{
        localName: 'EmployeeType',
        typeName: {
          namespaceURI: 'http:\/\/jaxb.vo.dozer.org\/Employee',
          localPart: 'EmployeeType'
        },
        propertyInfos: [{
            name: 'firstName',
            required: true,
            elementName: {
              localPart: 'FirstName'
            }
          }, {
            name: 'birthDate',
            elementName: {
              localPart: 'BirthDate'
            },
            typeInfo: 'Date'
          }, {
            name: 'lastName',
            required: true,
            elementName: {
              localPart: 'LastName'
            }
          }]
      }, {
        localName: 'EmployeeWithInnerClass.Address',
        typeName: null,
        propertyInfos: [{
            name: 'street',
            required: true,
            elementName: {
              localPart: 'Street'
            }
          }]
      }, {
        localName: 'EmployeeWithInnerClass',
        typeName: null,
        propertyInfos: [{
            name: 'firstName',
            required: true,
            elementName: {
              localPart: 'FirstName'
            }
          }, {
            name: 'birthDate',
            elementName: {
              localPart: 'BirthDate'
            },
            typeInfo: 'Date'
          }, {
            name: 'lastName',
            required: true,
            elementName: {
              localPart: 'LastName'
            }
          }, {
            name: 'address',
            required: true,
            elementName: {
              localPart: 'Address'
            },
            typeInfo: '.EmployeeWithInnerClass.Address'
          }]
      }],
    elementInfos: [{
        typeInfo: '.EmployeeWithInnerClass',
        elementName: {
          localPart: 'EmployeeWithInnerClass',
          namespaceURI: 'http:\/\/jaxb.vo.dozer.org\/Employee'
        }
      }, {
        typeInfo: '.EmployeeType',
        elementName: {
          localPart: 'Employee',
          namespaceURI: 'http:\/\/jaxb.vo.dozer.org\/Employee'
        }
      }]
  };
  return {
    org_dozer_vo_jaxb_employee: org_dozer_vo_jaxb_employee
  };
};
if (typeof define === 'function' && define.amd) {
  define([], org_dozer_vo_jaxb_employee_Module_Factory);
}
else {
  var org_dozer_vo_jaxb_employee_Module = org_dozer_vo_jaxb_employee_Module_Factory();
  if (typeof module !== 'undefined' && module.exports) {
    module.exports.org_dozer_vo_jaxb_employee = org_dozer_vo_jaxb_employee_Module.org_dozer_vo_jaxb_employee;
  }
  else {
    var org_dozer_vo_jaxb_employee = org_dozer_vo_jaxb_employee_Module.org_dozer_vo_jaxb_employee;
  }
}

为什么我在那里得到了所有这些功能?我做了些什么?

使用Altova XMLSpy,结果完全不同:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "additionalProperties": false,
    "definitions": {
        ".tns:Employee": {
            "$ref": "#/definitions/tns:EmployeeType"
        },
        ".tns:EmployeeWithInnerClass": {
            "additionalProperties": false,
            "patternProperties": {
                "^\\?\\w+$": {
                    "type": "string"
                }
            },
            "properties": {
                "Address": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "Street": {
                            "additionalProperties": false,
                            "patternProperties": {
                                "^\\?\\w+$": {
                                    "type": "string"
                                }
                            },
                            "properties": {
                                "$": {
                                    "$ref": "#/definitions/xs:string"
                                }
                            },
                            "type": "object"
                        }
                    },
                    "required": [
                        "Street"
                    ],
                    "type": "object"
                },
                "BirthDate": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "$": {
                            "$ref": "#/definitions/xs:date"
                        }
                    },
                    "type": "object"
                },
                "FirstName": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "$": {
                            "$ref": "#/definitions/xs:string"
                        }
                    },
                    "type": "object"
                },
                "LastName": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "$": {
                            "$ref": "#/definitions/xs:string"
                        }
                    },
                    "type": "object"
                }
            },
            "required": [
                "Address",
                "FirstName",
                "LastName"
            ],
            "type": "object"
        },
        "tns:EmployeeType": {
            "additionalProperties": false,
            "patternProperties": {
                "^\\?\\w+$": {
                    "type": "string"
                }
            },
            "properties": {
                "BirthDate": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "$": {
                            "$ref": "#/definitions/xs:date"
                        }
                    },
                    "type": "object"
                },
                "FirstName": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "$": {
                            "$ref": "#/definitions/xs:string"
                        }
                    },
                    "type": "object"
                },
                "LastName": {
                    "additionalProperties": false,
                    "patternProperties": {
                        "^\\?\\w+$": {
                            "type": "string"
                        }
                    },
                    "properties": {
                        "$": {
                            "$ref": "#/definitions/xs:string"
                        }
                    },
                    "type": "object"
                }
            },
            "required": [
                "FirstName",
                "LastName"
            ],
            "type": "object"
        },
        "xs:date": {
            "type": "string"
        },
        "xs:string": {
            "type": "string"
        }
    },
    "description": "JSON Schema generated by XMLSpy v2017 sp1 (x64) (http://www.altova.com)",
    "patternProperties": {
        "^\\?\\w+$": {
            "type": "string"
        }
    },
    "properties": {
        "@xmlns:tns": {
            "default": "http://jaxb.vo.dozer.org/Employee"
        },
        "@xmlns:xs": {
            "default": "http://www.w3.org/2001/XMLSchema"
        },
        "tns:Employee": {
            "$ref": "#/definitions/.tns:Employee"
        },
        "tns:EmployeeWithInnerClass": {
            "$ref": "#/definitions/.tns:EmployeeWithInnerClass"
        }
    },
    "type": "object"
}

由于

1 个答案:

答案 0 :(得分:1)

我是Jsonix的作者。

你发布的Jsonix输出不是JSON Schema,你的架构是Jsonix mappings。如果要生成JSON模式,请添加concurrency_hint参数。请参阅here

这是Jsonix为您的架构生成的内容:

-generateJsonSchema