parser.py
1.56 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
"""XSD schema parser
For now we suppose the file contains a single schema
"""
from lxml import etree
from .type import SimpleType, ComplexType, Group, from_element
from .annotation import Annotation
from .schema import Schema
class XSDParser:
def __init__(self, filename=None):
# initialize type definition storage
self.schemas=[]
# if filename was passed as argument
self.parse(filename)
def parse(self, filename):
tree = etree.parse(filename)
root = tree.getroot()
ns=root.nsmap
# if root is a schema
if root.tag.endswith("schema"):
self.schemas.append(Schema.from_element(root, filename))
# apply substitutions
self.schemas[-1].apply_substitutions()
self.schemas[-1].apply_extensions()
return
def get(self, typename):
for schema in self.schemas:
if typename in schema:
return schema.get(typename)
if __name__=="__main__":
import sys
if len(sys.argv)<2:
exit(0)
xsd_filename = sys.argv[1]
parser = XSDParser(xsd_filename)
print("Found {} schemas in {}".format(len(parser.schemas), xsd_filename))
for schema in parser.schemas:
print("Elements : {}".format(len(schema.items)))
print("SimpleTypes : {}".format(len(schema.simple_types)))
print("ComplexTypes : {}".format(len(schema.complex_types)))
print("Groups : {}".format(len(schema.groups)))
if len(sys.argv)>2:
for tn in sys.argv[2:]:
print(parser.get(tn))