-
-
Notifications
You must be signed in to change notification settings - Fork 600
/
Copy pathvisitor.py
1294 lines (1056 loc) · 43.3 KB
/
visitor.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import re
import typing
from lxml import etree
from zeep.exceptions import XMLParseError
from zeep.loader import absolute_location, load_external, normalize_location
from zeep.utils import as_qname, qname_attr
from zeep.xsd import elements as xsd_elements
from zeep.xsd import types as xsd_types
from zeep.xsd.const import AUTO_IMPORT_NAMESPACES, xsd_ns
from zeep.xsd.types.facets import Facets
from zeep.xsd.types.unresolved import UnresolvedCustomType, UnresolvedType
logger = logging.getLogger(__name__)
class tags:
schema = xsd_ns("schema")
import_ = xsd_ns("import")
include = xsd_ns("include")
annotation = xsd_ns("annotation")
element = xsd_ns("element")
simpleType = xsd_ns("simpleType")
complexType = xsd_ns("complexType")
simpleContent = xsd_ns("simpleContent")
complexContent = xsd_ns("complexContent")
sequence = xsd_ns("sequence")
group = xsd_ns("group")
choice = xsd_ns("choice")
all = xsd_ns("all")
list = xsd_ns("list")
union = xsd_ns("union")
attribute = xsd_ns("attribute")
any = xsd_ns("any")
anyAttribute = xsd_ns("anyAttribute")
attributeGroup = xsd_ns("attributeGroup")
restriction = xsd_ns("restriction")
extension = xsd_ns("extension")
notation = xsd_ns("notations")
class SchemaVisitor:
"""Visitor which processes XSD files and registers global elements and
types in the given schema.
Notes:
TODO: include and import statements can reference other nodes. We need
to load these first. Always global.
:param schema:
:type schema: zeep.xsd.schema.Schema
:param document:
:type document: zeep.xsd.schema.SchemaDocument
"""
def __init__(self, schema, document):
self.document = document
self.schema = schema
self._includes = set()
def register_element(self, qname: etree.QName, instance: xsd_elements.Element):
self.document.register_element(qname, instance)
def register_attribute(
self, name: etree.QName, instance: xsd_elements.Attribute
) -> None:
self.document.register_attribute(name, instance)
def register_type(self, qname: etree.QName, instance) -> None:
self.document.register_type(qname, instance)
def register_group(self, qname: etree.QName, instance: xsd_elements.Group):
self.document.register_group(qname, instance)
def register_attribute_group(
self, qname: etree.QName, instance: xsd_elements.AttributeGroup
) -> None:
self.document.register_attribute_group(qname, instance)
def register_import(self, namespace, document):
self.document.register_import(namespace, document)
def process(self, node, parent):
visit_func = self.visitors.get(node.tag)
if not visit_func:
raise ValueError("No visitor defined for %r" % node.tag)
result = visit_func(self, node, parent)
return result
def process_ref_attribute(self, node, array_type=None):
ref = qname_attr(node, "ref")
if ref:
ref = self._create_qname(ref)
# Some wsdl's reference to xs:schema, we ignore that for now. It
# might be better in the future to process the actual schema file
# so that it is handled correctly
if ref.namespace == "http://www.w3.org/2001/XMLSchema":
return
return xsd_elements.RefAttribute(
node.tag, ref, self.schema, array_type=array_type
)
def process_reference(self, node, **kwargs):
ref = qname_attr(node, "ref")
if not ref:
return
ref = self._create_qname(ref)
if node.tag == tags.element:
cls = xsd_elements.RefElement
elif node.tag == tags.attribute:
cls = xsd_elements.RefAttribute
elif node.tag == tags.group:
cls = xsd_elements.RefGroup
elif node.tag == tags.attributeGroup:
cls = xsd_elements.RefAttributeGroup
return cls(node.tag, ref, self.schema, **kwargs)
def visit_schema(self, node):
"""Visit the xsd:schema element and process all the child elements
Definition::
<schema
attributeFormDefault = (qualified | unqualified): unqualified
blockDefault = (#all | List of (extension | restriction | substitution) : ''
elementFormDefault = (qualified | unqualified): unqualified
finalDefault = (#all | List of (extension | restriction | list | union): ''
id = ID
targetNamespace = anyURI
version = token
xml:lang = language
{any attributes with non-schema Namespace}...>
Content: (
(include | import | redefine | annotation)*,
(((simpleType | complexType | group | attributeGroup) |
element | attribute | notation),
annotation*)*)
</schema>
:param node: The XML node
:type node: lxml.etree._Element
"""
assert node is not None
# A schema should always have a targetNamespace attribute, otherwise
# it is called a chameleon schema. In that case the schema will inherit
# the namespace of the enclosing schema/node.
tns = node.get("targetNamespace")
if tns:
self.document._target_namespace = tns
self.document._element_form = node.get("elementFormDefault", "unqualified")
self.document._attribute_form = node.get("attributeFormDefault", "unqualified")
for child in node:
self.process(child, parent=node)
def visit_import(self, node, parent):
"""
Definition::
<import
id = ID
namespace = anyURI
schemaLocation = anyURI
{any attributes with non-schema Namespace}...>
Content: (annotation?)
</import>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
schema_node = None
namespace = node.get("namespace")
location = node.get("schemaLocation")
if location:
location = normalize_location(
self.schema.settings, location, self.document._base_url
)
if not namespace and not self.document._target_namespace:
raise XMLParseError(
"The attribute 'namespace' must be existent if the "
"importing schema has no target namespace.",
filename=self.document.location,
sourceline=node.sourceline,
)
# We found an empty <import/> statement, this needs to trigger 4.1.2
# from https://www.w3.org/TR/2012/REC-xmlschema11-1-20120405/#src-resolve
# for QName resolving.
# In essence this means we will resolve QNames without a namespace to no
# namespace instead of the target namespace.
# The following code snippet works because imports have to occur before we
# visit elements.
if not namespace and not location:
self.document._has_empty_import = True
# Check if the schema is already imported before based on the
# namespace. Schema's without namespace are registered as 'None'
document = self.schema.documents.get_by_namespace_and_location(
namespace, location
)
if document:
logger.debug("Returning existing schema: %r", location)
self.register_import(namespace, document)
return document
# Hardcode the mapping between the xml namespace and the xsd for now.
# This seems to fix issues with exchange wsdl's, see #220
if not location and namespace == "http://www.w3.org/XML/1998/namespace":
location = "https://www.w3.org/2001/xml.xsd"
# Silently ignore import statements which we can't resolve via the
# namespace and doesn't have a schemaLocation attribute.
if not location:
logger.debug(
"Ignoring import statement for namespace %r "
+ "(missing schemaLocation)",
namespace,
)
return
# Load the XML
schema_node = self._retrieve_data(location, base_url=self.document._location)
# Check if the xsd:import namespace matches the targetNamespace. If
# the xsd:import statement didn't specify a namespace then make sure
# that the targetNamespace wasn't declared by another schema yet.
schema_tns = schema_node.get("targetNamespace")
if namespace and schema_tns and namespace != schema_tns:
raise XMLParseError(
(
"The namespace defined on the xsd:import doesn't match the "
"imported targetNamespace located at %r "
)
% (location),
filename=self.document._location,
sourceline=node.sourceline,
)
# If the imported schema doesn't define a target namespace and the
# node doesn't specify it either then inherit the existing target
# namespace.
elif not schema_tns and not namespace:
namespace = self.document._target_namespace
schema = self.schema.create_new_document(
schema_node, location, target_namespace=namespace
)
self.register_import(namespace, schema)
return schema
def visit_include(self, node, parent):
"""
Definition::
<include
id = ID
schemaLocation = anyURI
{any attributes with non-schema Namespace}...>
Content: (annotation?)
</include>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
if not node.get("schemaLocation"):
raise NotImplementedError("schemaLocation is required")
location = node.get("schemaLocation")
if location in self._includes:
return
schema_node = self._retrieve_data(location, base_url=self.document._base_url)
self._includes.add(location)
# When the included document has no default namespace defined but the
# parent document does have this then we should (atleast for #360)
# transfer the default namespace to the included schema. We can't
# update the nsmap of elements in lxml so we create a new schema with
# the correct nsmap and move all the content there.
# Included schemas must have targetNamespace equal to parent schema (the including) or None.
# If included schema doesn't have default ns, then it should be set to parent's targetNs.
# See Chameleon Inclusion https://www.w3.org/TR/xmlschema11-1/#chameleon-xslt
if not schema_node.nsmap.get(None) and (
node.nsmap.get(None) or parent.attrib.get("targetNamespace")
):
nsmap = {None: node.nsmap.get(None) or parent.attrib["targetNamespace"]}
nsmap.update(schema_node.nsmap)
new = etree.Element(schema_node.tag, nsmap=nsmap)
for child in schema_node:
new.append(child)
for key, value in schema_node.attrib.items():
new.set(key, value)
if not new.attrib.get("targetNamespace"):
new.attrib["targetNamespace"] = parent.attrib["targetNamespace"]
schema_node = new
# Use the element/attribute form defaults from the schema while
# processing the nodes.
element_form_default = self.document._element_form
attribute_form_default = self.document._attribute_form
base_url = self.document._base_url
self.document._element_form = schema_node.get(
"elementFormDefault", "unqualified"
)
self.document._attribute_form = schema_node.get(
"attributeFormDefault", "unqualified"
)
self.document._base_url = absolute_location(location, self.document._base_url)
# Iterate directly over the children.
for child in schema_node:
self.process(child, parent=schema_node)
self.document._element_form = element_form_default
self.document._attribute_form = attribute_form_default
self.document._base_url = base_url
def visit_element(self, node, parent):
"""
Definition::
<element
abstract = Boolean : false
block = (#all | List of (extension | restriction | substitution))
default = string
final = (#all | List of (extension | restriction))
fixed = string
form = (qualified | unqualified)
id = ID
maxOccurs = (nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
name = NCName
nillable = Boolean : false
ref = QName
substitutionGroup = QName
type = QName
{any attributes with non-schema Namespace}...>
Content: (annotation?, (
(simpleType | complexType)?, (unique | key | keyref)*))
</element>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
is_global = parent.tag == tags.schema
# minOccurs / maxOccurs are not allowed on global elements
if not is_global:
min_occurs, max_occurs = _process_occurs_attrs(node)
else:
max_occurs = 1
min_occurs = 1
# If the element has a ref attribute then all other attributes cannot
# be present. Short circuit that here.
# Ref is prohibited on global elements (parent = schema)
if not is_global:
# Naive workaround to mark fields which are part of a choice element
# as optional
if parent.tag == tags.choice:
min_occurs = 0
result = self.process_reference(
node, min_occurs=min_occurs, max_occurs=max_occurs
)
if result:
return result
element_form = node.get("form", self.document._element_form)
if element_form == "qualified" or is_global:
qname = qname_attr(node, "name", self.document._target_namespace)
else:
qname = etree.QName(node.get("name").strip())
children = list(node)
xsd_type = None
if children:
value = None
for child in children:
if child.tag == tags.annotation:
continue
elif child.tag in (tags.simpleType, tags.complexType):
assert not value
xsd_type = self.process(child, node)
if not xsd_type:
node_type = qname_attr(node, "type")
if node_type:
xsd_type = self._get_type(node_type.text)
else:
xsd_type = xsd_types.AnyType()
nillable = node.get("nillable") == "true"
default = node.get("default")
element = xsd_elements.Element(
name=qname,
type_=xsd_type,
min_occurs=min_occurs,
max_occurs=max_occurs,
nillable=nillable,
default=default,
is_global=is_global,
)
# Only register global elements
if is_global:
self.register_element(qname, element)
return element
def visit_attribute(
self, node: etree._Element, parent: etree._Element
) -> typing.Union[xsd_elements.Attribute, xsd_elements.RefAttribute]:
"""Declares an attribute.
Definition::
<attribute
default = string
fixed = string
form = (qualified | unqualified)
id = ID
name = NCName
ref = QName
type = QName
use = (optional | prohibited | required): optional
{any attributes with non-schema Namespace...}>
Content: (annotation?, (simpleType?))
</attribute>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
is_global = parent.tag == tags.schema
# Check of wsdl:arayType
array_type = node.get("{http://schemas.xmlsoap.org/wsdl/}arrayType")
if array_type:
match = re.match(r"([^\[]+)", array_type)
if match:
array_type = match.groups()[0]
qname = as_qname(array_type, node.nsmap)
array_type = UnresolvedType(qname, self.schema)
# If the elment has a ref attribute then all other attributes cannot
# be present. Short circuit that here.
# Ref is prohibited on global elements (parent = schema)
if not is_global:
result = self.process_ref_attribute(node, array_type=array_type)
if result:
return result
attribute_form = node.get("form", self.document._attribute_form)
if attribute_form == "qualified" or is_global:
name = qname_attr(node, "name", self.document._target_namespace)
else:
name = etree.QName(node.get("name"))
annotation, items = self._pop_annotation(list(node))
if items:
xsd_type = self.visit_simple_type(items[0], node)
else:
node_type = qname_attr(node, "type")
if node_type:
xsd_type = self._get_type(node_type)
else:
xsd_type = xsd_types.AnyType()
# TODO: We ignore 'prohobited' for now
required = node.get("use") == "required"
default = node.get("default")
attr = xsd_elements.Attribute(
name, type_=xsd_type, default=default, required=required
)
# Only register global elements
if is_global:
assert name is not None
self.register_attribute(name, attr)
return attr
def visit_simple_type(self, node, parent):
"""
Definition::
<simpleType
final = (#all | (list | union | restriction))
id = ID
name = NCName
{any attributes with non-schema Namespace}...>
Content: (annotation?, (restriction | list | union))
</simpleType>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
if parent.tag == tags.schema:
name = node.get("name")
is_global = True
else:
name = parent.get("name", "Anonymous")
is_global = False
base_type = "{http://www.w3.org/2001/XMLSchema}string"
qname = as_qname(name, node.nsmap, self.document._target_namespace)
annotation, items = self._pop_annotation(list(node))
child = items[0]
if child.tag == tags.restriction:
base_type, facets = self.visit_restriction_simple_type(child, node)
xsd_type = UnresolvedCustomType(qname, base_type, self.schema, facets)
elif child.tag == tags.list:
xsd_type = self.visit_list(child, node)
elif child.tag == tags.union:
xsd_type = self.visit_union(child, node)
else:
raise AssertionError("Unexpected child: %r" % child.tag)
assert xsd_type is not None
if is_global:
self.register_type(qname, xsd_type)
return xsd_type
def visit_complex_type(self, node, parent):
"""
Definition::
<complexType
abstract = Boolean : false
block = (#all | List of (extension | restriction))
final = (#all | List of (extension | restriction))
id = ID
mixed = Boolean : false
name = NCName
{any attributes with non-schema Namespace...}>
Content: (annotation?, (simpleContent | complexContent |
((group | all | choice | sequence)?,
((attribute | attributeGroup)*, anyAttribute?))))
</complexType>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
children = []
base_type = "{http://www.w3.org/2001/XMLSchema}anyType"
# If the complexType's parent is an element then this type is
# anonymous and should have no name defined. Otherwise it's global
if parent.tag == tags.schema:
name = node.get("name")
is_global = True
else:
name = parent.get("name")
is_global = False
qname = as_qname(name, node.nsmap, self.document._target_namespace)
cls_attributes = {"__module__": "zeep.xsd.dynamic_types", "_xsd_name": qname}
xsd_cls = type(name, (xsd_types.ComplexType,), cls_attributes)
xsd_type = None
# Process content
annotation, children = self._pop_annotation(list(node))
first_tag = children[0].tag if children else None
if first_tag == tags.simpleContent:
base_type, attributes = self.visit_simple_content(children[0], node)
xsd_type = xsd_cls(
attributes=attributes,
extension=base_type,
qname=qname,
is_global=is_global,
)
elif first_tag == tags.complexContent:
kwargs = self.visit_complex_content(children[0], node)
xsd_type = xsd_cls(qname=qname, is_global=is_global, **kwargs)
elif first_tag:
element = None
if first_tag in (tags.group, tags.all, tags.choice, tags.sequence):
child = children.pop(0)
element = self.process(child, node)
attributes = self._process_attributes(node, children)
xsd_type = xsd_cls(
element=element, attributes=attributes, qname=qname, is_global=is_global
)
else:
xsd_type = xsd_cls(qname=qname, is_global=is_global)
if is_global:
self.register_type(qname, xsd_type)
return xsd_type
def visit_complex_content(self, node, parent):
"""The complexContent element defines extensions or restrictions on a
complex type that contains mixed content or elements only.
Definition::
<complexContent
id = ID
mixed = Boolean
{any attributes with non-schema Namespace}...>
Content: (annotation?, (restriction | extension))
</complexContent>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
children = list(node)
child = children[-1]
if child.tag == tags.restriction:
base, element, attributes = self.visit_restriction_complex_content(
child, node
)
return {"attributes": attributes, "element": element, "restriction": base}
elif child.tag == tags.extension:
base, element, attributes = self.visit_extension_complex_content(
child, node
)
return {"attributes": attributes, "element": element, "extension": base}
def visit_simple_content(self, node, parent):
"""Contains extensions or restrictions on a complexType element with
character data or a simpleType element as content and contains no
elements.
Definition::
<simpleContent
id = ID
{any attributes with non-schema Namespace}...>
Content: (annotation?, (restriction | extension))
</simpleContent>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
children = list(node)
child = children[-1]
if child.tag == tags.restriction:
return self.visit_restriction_simple_content(child, node)
elif child.tag == tags.extension:
return self.visit_extension_simple_content(child, node)
raise AssertionError("Expected restriction or extension")
def visit_restriction_simple_type(self, node, parent):
"""
Definition::
<restriction
base = QName
id = ID
{any attributes with non-schema Namespace}...>
Content: (annotation?,
(simpleType?, (
minExclusive | minInclusive | maxExclusive | maxInclusive |
totalDigits |fractionDigits | length | minLength |
maxLength | enumeration | whiteSpace | pattern)*))
</restriction>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
annotation, children = self._pop_annotation(list(node))
facets = Facets.parse_xml(children)
base_name = qname_attr(node, "base")
if base_name:
return self._get_type(base_name), facets
if children[0].tag == tags.simpleType:
return self.visit_simple_type(children[0], node), facets
def visit_restriction_simple_content(self, node, parent):
"""
Definition::
<restriction
base = QName
id = ID
{any attributes with non-schema Namespace}...>
Content: (annotation?,
(simpleType?, (
minExclusive | minInclusive | maxExclusive | maxInclusive |
totalDigits |fractionDigits | length | minLength |
maxLength | enumeration | whiteSpace | pattern)*
)?, ((attribute | attributeGroup)*, anyAttribute?))
</restriction>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
base_name = qname_attr(node, "base")
base_type = self._get_type(base_name)
return base_type, []
def visit_restriction_complex_content(self, node, parent):
"""
Definition::
<restriction
base = QName
id = ID
{any attributes with non-schema Namespace}...>
Content: (annotation?, (group | all | choice | sequence)?,
((attribute | attributeGroup)*, anyAttribute?))
</restriction>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
base_name = qname_attr(node, "base")
base_type = self._get_type(base_name)
annotation, children = self._pop_annotation(list(node))
element = None
attributes = []
if children:
child = children[0]
if child.tag in (tags.group, tags.all, tags.choice, tags.sequence):
children.pop(0)
element = self.process(child, node)
attributes = self._process_attributes(node, children)
return base_type, element, attributes
def visit_extension_complex_content(self, node, parent):
"""
Definition::
<extension
base = QName
id = ID
{any attributes with non-schema Namespace}...>
Content: (annotation?, (
(group | all | choice | sequence)?,
((attribute | attributeGroup)*, anyAttribute?)))
</extension>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
base_name = qname_attr(node, "base")
base_type = self._get_type(base_name)
annotation, children = self._pop_annotation(list(node))
element = None
attributes = []
if children:
child = children[0]
if child.tag in (tags.group, tags.all, tags.choice, tags.sequence):
children.pop(0)
element = self.process(child, node)
attributes = self._process_attributes(node, children)
return base_type, element, attributes
def visit_extension_simple_content(self, node, parent):
"""
Definition::
<extension
base = QName
id = ID
{any attributes with non-schema Namespace}...>
Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
</extension>
"""
base_name = qname_attr(node, "base")
base_type = self._get_type(base_name)
annotation, children = self._pop_annotation(list(node))
attributes = self._process_attributes(node, children)
return base_type, attributes
def visit_annotation(self, node, parent):
"""Defines an annotation.
Definition::
<annotation
id = ID
{any attributes with non-schema Namespace}...>
Content: (appinfo | documentation)*
</annotation>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
return
def visit_any(self, node, parent):
"""
Definition::
<any
id = ID
maxOccurs = (nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
namespace = "(##any | ##other) |
List of (anyURI | (##targetNamespace | ##local))) : ##any
processContents = (lax | skip | strict) : strict
{any attributes with non-schema Namespace...}>
Content: (annotation?)
</any>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
min_occurs, max_occurs = _process_occurs_attrs(node)
process_contents = node.get("processContents", "strict")
return xsd_elements.Any(
max_occurs=max_occurs,
min_occurs=min_occurs,
process_contents=process_contents,
)
def visit_sequence(self, node, parent):
"""
Definition::
<sequence
id = ID
maxOccurs = (nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
{any attributes with non-schema Namespace}...>
Content: (annotation?,
(element | group | choice | sequence | any)*)
</sequence>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
sub_types = [
tags.annotation,
tags.any,
tags.choice,
tags.element,
tags.group,
tags.sequence,
]
min_occurs, max_occurs = _process_occurs_attrs(node)
result = xsd_elements.Sequence(min_occurs=min_occurs, max_occurs=max_occurs)
annotation, children = self._pop_annotation(list(node))
for child in children:
if child.tag not in sub_types:
raise self._create_error(
"Unexpected element %s in xsd:sequence" % child.tag, child
)
item = self.process(child, node)
assert item is not None
result.append(item)
assert None not in result
return result
def visit_all(self, node, parent):
"""Allows the elements in the group to appear (or not appear) in any
order in the containing element.
Definition::
<all
id = ID
maxOccurs= 1: 1
minOccurs= (0 | 1): 1
{any attributes with non-schema Namespace...}>
Content: (annotation?, element*)
</all>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
sub_types = [tags.annotation, tags.element]
result = xsd_elements.All()
annotation, children = self._pop_annotation(list(node))
for child in children:
assert child.tag in sub_types, child
item = self.process(child, node)
result.append(item)
assert None not in result
return result
def visit_group(self, node, parent):
"""Groups a set of element declarations so that they can be
incorporated as a group into complex type definitions.
Definition::
<group
name= NCName
id = ID
maxOccurs = (nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
name = NCName
ref = QName
{any attributes with non-schema Namespace}...>
Content: (annotation?, (all | choice | sequence))
</group>
:param node: The XML node
:type node: lxml.etree._Element
:param parent: The parent XML node
:type parent: lxml.etree._Element
"""
min_occurs, max_occurs = _process_occurs_attrs(node)
result = self.process_reference(
node, min_occurs=min_occurs, max_occurs=max_occurs
)
if result: