test1.py
2.2 KB
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
import tkinter as tk
from tkinter import ttk
from enum import Enum
from lxml import etree
class xsdtags(Enum):
SCHEMA="schema"
ELEMENT="element"
SIMPLE_TYPE="simpleType"
COMPLEX_TYPE="complexType"
ENUMERATION="enumeration"
SEQUENCE="sequence"
CHOICE="choice"
def find_type_definition(xsd, name):
for el in xsd.iter("{*}complexType", "{*}simpleType"):
if "name" in el.attrib:
if el.attrib["name"]==name:
return el
return None
def describe_schema(xsd, element):
# go through elements and choices and print their description
for e in element:
if isinstance(e, etree._Comment):
continue
elif e.tag.endswith("element"):
describe_element(xsd, e)
elif e.tag.endswith("choice"):
describe_choice(xsd, e)
else:
pass
def describe_element(xsd, element, lvl=0):
print("{}- {}".format(lvl*" ",element.attrib["name"]))
# get the type definition
_type = element.attrib["type"]
if ":" in _type:
_type = _type.split(":")[-1]
d = find_type_definition(xsd, _type)
# iterate over elements of the definition and describe them
for el in d:
if el.tag.endswith("sequence"):
print("{}- seq :".format((lvl+1)*" "))
for item in el:
if item.tag.endswith("element"):
describe_element(xsd, item, lvl+2)
if item.tag.endswith("choice"):
print("{}- choice :".format((lvl+2)*" "), item.attrib)
for c in item:
if c.tag.endswith("element"):
describe_element(xsd, c, lvl+3)
def describe_choice(xsd, element):
print("describe choice {}".format(element.tag))
"""
A single XSD file can contain multiple schema definitions. Find each one and list the complexTypes that
are defined therein.
"""
if __name__=="__main__":
xsd_filename = "spase-2.3.1.xsd"
# load the tree
xsd_tree = etree.parse(xsd_filename)
# list all schemas in the XSD file
for schema_el in xsd_tree.iter(tag="{*}schema"):
print("Schema name : ",schema_el)
describe_schema(xsd_tree, schema_el)
exit()