Programing

XML 스키마 (XSD)에서 Json 스키마 생성

lottogame 2020. 11. 6. 07:45
반응형

XML 스키마 (XSD)에서 Json 스키마 생성


누구든지 기존 XML 스키마 (XSD 파일)에서 JSON 스키마 를 생성하는 방법을 알고 있습니까? 이를 위해 사용할 수있는 도구가 있습니까?


면책 조항 : 저는 강력한 오픈 소스 XML <-> JSON JavaScript 매핑 라이브러리 Jsonix 의 저자입니다 .

오늘 저는 새로운 JSON 스키마 생성 기능 이 포함 된 새 버전의 Jsonix Schema Compiler를 출시했습니다 .

예를 들어 구매 주문 스키마를 살펴 보겠습니다 . 다음은 조각입니다.

  <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

컴파일러는 Jsonix 매핑일치하는 JSON 스키마를 생성 합니다.

결과는 다음과 같습니다 (간결성을 위해 편집 됨).

{
    "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 매핑과 일치합니다. 따라서 XML <-> JSON 변환에 Jsonix를 사용하는 경우 생성 된 JSON 스키마로 JSON의 유효성을 검사 할 수 있어야합니다. 또한 원래 XML 스키마에서 필요한 모든 메타 데이터 (예 : 요소, 속성 및 유형 이름)를 포함합니다.

면책 조항 : 현재 이것은 새롭고 실험적인 기능입니다. 알려진 제한 사항과 누락 된 기능이 있습니다. 그러나 나는 이것이 매우 빨리 나타나고 성숙되기를 기대하고 있습니다.

연결:


A year after this question was originally asked, JSON Schema remains an IETF draft document. As of this writing (18 October 2011) the working group is trying to get agreement on draft 4 of the specification. Although there are a few speculative validation implementations (see http://json-schema.org/), most tool vendors haven't invested much effort into tools that implement JSON Schema development, editing or conversion.


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

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.

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.

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.


Disclaimer: I'm the author of jgeXml.

jgexml has Node.js based utility xsd2json which does a transformation between an XML schema (XSD) and a JSON schema file.

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.

A sample of the purchaseorder.xsd given in another answer is rendered as:

"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}.*$"
    }
  },

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


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

참고URL : https://stackoverflow.com/questions/3922026/generate-json-schema-from-xml-schema-xsd

반응형