-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxsd2jsonschema.py
108 lines (88 loc) · 3.44 KB
/
xsd2jsonschema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# coding: utf-8
import argparse
from lxml import etree
import json
import os
import sys
def xsd_to_json_schema(element):
schema = {}
if element.tag.endswith('schema'):
schema['$schema'] = 'http://json-schema.org/draft-07/schema#'
schema['type'] = 'object'
schema['properties'] = {}
schema['required'] = []
for child in element:
if child.tag.endswith('element'):
child_name = child.get('name')
if child_name:
schema['properties'][child_name] = xsd_element_to_json_schema(child)
if child.get('minOccurs') != '0':
schema['required'].append(child_name)
return schema
def xsd_element_to_json_schema(element):
element_schema = {}
element_type = element.get('type')
if element_type:
element_schema['type'] = xsd_type_to_json_type(element_type)
else:
for child in element:
if child.tag.endswith('complexType'):
element_schema.update(xsd_complex_type_to_json_schema(child))
elif child.tag.endswith('simpleType'):
element_schema['type'] = 'string'
return element_schema
def xsd_complex_type_to_json_schema(element):
complex_schema = {'type': 'object', 'properties': {}, 'required': []}
for child in element:
if child.tag.endswith('sequence'):
for seq_child in child:
child_name = seq_child.get('name')
if child_name:
complex_schema['properties'][child_name] = xsd_element_to_json_schema(seq_child)
if seq_child.get('minOccurs') != '0':
complex_schema['required'].append(child_name)
return complex_schema
def xsd_type_to_json_type(xsd_type):
mapping = {
'xs:string': 'string',
'xs:integer': 'integer',
'xs:decimal': 'number',
'xs:boolean': 'boolean',
'xs:date': 'string',
'xs:dateTime': 'string',
'xs:time': 'string'
}
return mapping.get(xsd_type, 'string')
def xsd_to_json_schema_file(xsd_file_path):
with open(xsd_file_path, 'rb') as file:
xml_content = file.read()
root = etree.XML(xml_content)
schema_dict = xsd_to_json_schema(root)
# Determine the JSON Schema file path
base_name = os.path.splitext(xsd_file_path)[0]
json_schema_file_path = base_name + '.json'
with open(json_schema_file_path, 'w') as json_file:
json.dump(schema_dict, json_file, indent=4)
print(f"Converted {xsd_file_path} to {json_schema_file_path} successfully.")
return json_schema_file_path
def is_jupyter_notebook():
try:
from IPython import get_ipython
if 'IPKernelApp' not in get_ipython().config:
return False
except ImportError:
return False
except AttributeError:
return False
return True
if __name__ == "__main__":
if is_jupyter_notebook():
# If in a Jupyter Notebook, skip argument parsing
xsd_file_path = 'path/to/your/xsdfile.xsd' # Update this path as needed
xsd_to_json_schema_file(xsd_file_path)
else: #for cli
parser = argparse.ArgumentParser(description='Convert XSD to JSON Schema')
parser.add_argument('xsd_file_path', type=str, help='The path to the XSD file')
args = parser.parse_args()
xsd_to_json_schema_file(args.xsd_file_path)