File: simple_types.py

package info (click to toggle)
python-xmlschema 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,208 kB
  • sloc: python: 39,174; xml: 1,282; makefile: 36
file content (1553 lines) | stat: -rw-r--r-- 64,218 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
#
# Copyright (c), 2016-2020, SISSA (International School for Advanced Studies).
# All rights reserved.
# This file is distributed under the terms of the MIT License.
# See the file 'LICENSE' in the root directory of the present
# distribution, or http://opensource.org/licenses/MIT.
#
# @author Davide Brunato <brunato@sissa.it>
#
"""
This module contains classes for XML Schema simple data types.
"""
import re
from collections.abc import Callable, Iterator
from decimal import DecimalException, Decimal
from functools import cached_property
from typing import cast, Any, Optional, Union, Type
from xml.etree import ElementTree

from elementpath.datatypes import AnyAtomicType, AbstractDateTime, AbstractQName, \
    Duration, UntypedAtomic

from xmlschema.aliases import ElementType, AtomicValueType, ComponentClassType, \
    BaseXsdType, SchemaType, DecodedValueType, NsmapType
from xmlschema.exceptions import XMLSchemaTypeError, XMLSchemaValueError
from xmlschema.names import XSD_NAMESPACE, XSD_ANY_TYPE, XSD_SIMPLE_TYPE, XSD_PATTERN, \
    XSD_ANY_ATOMIC_TYPE, XSD_ATTRIBUTE, XSD_ATTRIBUTE_GROUP, XSD_ANY_ATTRIBUTE, \
    XSD_MIN_INCLUSIVE, XSD_MIN_EXCLUSIVE, XSD_MAX_INCLUSIVE, XSD_MAX_EXCLUSIVE, \
    XSD_LENGTH, XSD_MIN_LENGTH, XSD_MAX_LENGTH, XSD_WHITE_SPACE, XSD_ENUMERATION, \
    XSD_LIST, XSD_ANY_SIMPLE_TYPE, XSD_UNION, XSD_RESTRICTION, XSD_ANNOTATION, \
    XSD_ASSERTION, XSD_ID, XSD_IDREF, XSD_FRACTION_DIGITS, XSD_TOTAL_DIGITS, \
    XSD_EXPLICIT_TIMEZONE, XSD_ERROR, XSD_ASSERT, XSD_QNAME, XSD_NOTATION, XSD_BOOLEAN
from xmlschema.translation import gettext as _
from xmlschema.utils.qnames import local_name, get_extended_qname
from xmlschema.utils.decoding import raw_encode_value

from .exceptions import XMLSchemaValidationError, XMLSchemaParseError, \
    XMLSchemaCircularityError, XMLSchemaDecodeError, XMLSchemaEncodeError
from .validation import DecodeContext, EncodeContext, ValidationMixin
from .xsdbase import XsdComponent, XsdType
from .facets import XsdFacet, XsdWhiteSpaceFacet, XsdPatternFacets, \
    XsdEnumerationFacets, XsdAssertionFacet, XSD_10_FACETS, XSD_11_FACETS, \
    XSD_10_LIST_FACETS, XSD_11_LIST_FACETS, XSD_10_UNION_FACETS, \
    XSD_11_UNION_FACETS, MULTIPLE_FACETS

FacetsValueType = Union[XsdFacet, Callable[[Any], None], list[XsdAssertionFacet]]
PythonTypeClasses = Union[Type[Any], tuple[Type[Any]]]


class XsdSimpleType(XsdType, ValidationMixin[Union[str, bytes], DecodedValueType]):
    """
    Base class for simpleTypes definitions. Generally used only for
    instances of xs:anySimpleType.

    ..  <simpleType
          final = (#all | List of (list | union | restriction | extension))
          id = ID
          name = NCName
          {any attributes with non-schema namespace . . .}>
          Content: (annotation?, (restriction | list | union))
        </simpleType>
    """
    _special_types = {XSD_ANY_TYPE, XSD_ANY_SIMPLE_TYPE}
    _ADMITTED_TAGS: tuple[str, ...] = XSD_SIMPLE_TYPE,
    _REGEX_SPACE = re.compile(r'\s')
    _REGEX_SPACES = re.compile(r'\s+')

    abstract: bool = False
    block: str = ''

    min_length: Optional[int]
    max_length: Optional[int]
    white_space: Optional[str]
    patterns: Optional[XsdPatternFacets]
    validators: Union[tuple[()], list[Union[XsdFacet, Callable[[Any], None]]]]
    allow_empty: bool

    datatype: Type[Any] = str  # Unicode string as default datatype for XSD simple types
    python_type: Type[Any] = str
    instance_types: PythonTypeClasses = str
    to_python: Union[Type[Any], Callable[[Union[str, bytes]], AtomicValueType]] = str
    from_python: Union[Type[str], Callable[[Any], str]] = str

    __slots__ = ('_facets', 'min_length', 'max_length', 'white_space', 'patterns',
                 'validators', 'allow_empty')

    def __init__(self, elem: ElementType,
                 schema: SchemaType,
                 parent: Optional[XsdComponent] = None,
                 name: Optional[str] = None,
                 facets: Optional[dict[Optional[str], FacetsValueType]] = None) -> None:
        super().__init__(elem, schema, parent, name)
        if not hasattr(self, '_facets'):
            self.facets = facets if facets is not None else {}

    @property
    def facets(self) -> dict[Optional[str], FacetsValueType]:
        return self._facets

    @facets.setter
    def facets(self, facets: dict[Optional[str], FacetsValueType]) -> None:
        self._facets = facets
        self.min_length = self.max_length = None
        self.patterns = None
        self.validators = ()

        if not isinstance(self, XsdAtomicBuiltin):
            self._parse_facets(facets)

        if self.min_length:
            self.allow_empty = False
        else:
            self.allow_empty = True

        white_space = getattr(self.get_facet(XSD_WHITE_SPACE), 'value', None)
        if isinstance(self, XsdUnion):
            if not (white_space is None or white_space == 'collapse'):
                msg = _("wrong value %r for facet xs:whiteSpace")
                raise XMLSchemaValueError(msg % white_space)
            self.white_space = 'collapse'

        else:
            self.white_space = white_space

        patterns = self.get_facet(XSD_PATTERN)
        if isinstance(patterns, XsdPatternFacets):
            self.patterns = patterns
            if patterns.re_match('') is None:
                self.allow_empty = False

        enumeration = self.get_facet(XSD_ENUMERATION)
        if isinstance(enumeration, XsdEnumerationFacets) \
                and '' not in enumeration.enumeration:
            self.allow_empty = False

        if facets:
            validators: list[Union[XsdFacet, Callable[[Any], None]]]

            if callable(func := facets.get(None)):
                validators = [func]  # a validation function
            else:
                validators = [cast(XsdFacet, v) for k, v in facets.items()
                              if k not in (XSD_WHITE_SPACE, XSD_PATTERN, XSD_ASSERTION)]

            if XSD_ASSERTION in facets:
                assertions = facets[XSD_ASSERTION]
                if isinstance(assertions, list):
                    validators.extend(assertions)
                else:
                    validators.append(assertions)
            if validators:
                self.validators = validators

    def _parse_facets(self, facets: Any) -> None:
        base_type: Any

        if facets and self.base_type is not None:
            if isinstance(self.base_type, XsdSimpleType):
                if self.base_type.name == XSD_ANY_SIMPLE_TYPE:
                    msg = _("facets not allowed for a direct derivation of xs:anySimpleType")
                    self.parse_error(msg)
            elif self.base_type.has_simple_content():
                if self.base_type.content.name == XSD_ANY_SIMPLE_TYPE:
                    msg = _("facets not allowed for a direct content "
                            "derivation of xs:anySimpleType")
                    self.parse_error(msg)

        # Checks the applicability of the facets
        if any(k not in self.admitted_facets for k in facets if k is not None):
            msg = _("one or more facets are not applicable, admitted set is {!r}")
            self.parse_error(msg.format({local_name(e) for e in self.admitted_facets if e}))

        # Check group base_type
        base_type = {t.base_type for t in facets.values() if isinstance(t, XsdFacet)}
        if len(base_type) > 1:
            msg = _("facet group must have the same base type: %r")
            self.parse_error(msg % base_type)
        base_type = base_type.pop() if base_type else None

        # Checks length based facets
        length = getattr(facets.get(XSD_LENGTH), 'value', None)
        min_length = getattr(facets.get(XSD_MIN_LENGTH), 'value', None)
        max_length = getattr(facets.get(XSD_MAX_LENGTH), 'value', None)
        if length is not None:
            if length < 0:
                self.parse_error(_("'length' value must be non a negative integer"))

            if min_length is not None:
                if min_length > length:
                    msg = _("'minLength' value must be less than or equal to 'length'")
                    self.parse_error(msg)
                min_length_facet = base_type.get_facet(XSD_MIN_LENGTH)
                length_facet = base_type.get_facet(XSD_LENGTH)
                if (min_length_facet is None
                        or (length_facet is not None
                            and length_facet.base_type == min_length_facet.base_type)):
                    msg = _("cannot specify both 'length' and 'minLength'")
                    self.parse_error(msg)

            if max_length is not None:
                if max_length < length:
                    msg = _("'maxLength' value must be greater or equal to 'length'")
                    self.parse_error(msg)

                max_length_facet = base_type.get_facet(XSD_MAX_LENGTH)
                length_facet = base_type.get_facet(XSD_LENGTH)
                if max_length_facet is None \
                        or (length_facet is not None
                            and length_facet.base_type == max_length_facet.base_type):
                    msg = _("cannot specify both 'length' and 'maxLength'")
                    self.parse_error(msg)

            min_length = max_length = length
        elif min_length is not None or max_length is not None:
            min_length_facet = base_type.get_facet(XSD_MIN_LENGTH)
            max_length_facet = base_type.get_facet(XSD_MAX_LENGTH)
            if min_length is not None:
                if min_length < 0:
                    msg = _("'minLength' value must be a non negative integer")
                    self.parse_error(msg)
                if max_length is not None and max_length < min_length:
                    msg = _("'maxLength' value is less than 'minLength'")
                    self.parse_error(msg)
                if min_length_facet is not None and min_length_facet.value > min_length:
                    msg = _("'minLength' has a lesser value than parent")
                    self.parse_error(msg)
                if max_length_facet is not None and min_length > max_length_facet.value:
                    msg = _("'minLength' has a greater value than parent 'maxLength'")
                    self.parse_error(msg)

            if max_length is not None:
                if max_length < 0:
                    msg = _("'maxLength' value must be a non negative integer")
                    self.parse_error(msg)
                if min_length_facet is not None and min_length_facet.value > max_length:
                    msg = _("'maxLength' has a lesser value than parent 'minLength'")
                    self.parse_error(msg)
                if max_length_facet is not None and max_length > max_length_facet.value:
                    msg = _("'maxLength' has a greater value than parent")
                    self.parse_error(msg)

        # Checks min/max values
        min_inclusive = getattr(facets.get(XSD_MIN_INCLUSIVE), 'value', None)
        min_exclusive = getattr(facets.get(XSD_MIN_EXCLUSIVE), 'value', None)
        max_inclusive = getattr(facets.get(XSD_MAX_INCLUSIVE), 'value', None)
        max_exclusive = getattr(facets.get(XSD_MAX_EXCLUSIVE), 'value', None)

        if min_inclusive is not None:
            if min_exclusive is not None:
                msg = _("cannot specify both 'minInclusive' and 'minExclusive'")
                self.parse_error(msg)
            if max_inclusive is not None and min_inclusive > max_inclusive:
                msg = _("'minInclusive' must be less or equal to 'maxInclusive'")
                self.parse_error(msg)
            elif max_exclusive is not None and min_inclusive >= max_exclusive:
                msg = _("'minInclusive' must be lesser than 'maxExclusive'")
                self.parse_error(msg)

        elif min_exclusive is not None:
            if max_inclusive is not None and min_exclusive >= max_inclusive:
                msg = _("'minExclusive' must be lesser than 'maxInclusive'")
                self.parse_error(msg)
            elif max_exclusive is not None and min_exclusive > max_exclusive:
                msg = _("'minExclusive' must be less or equal to 'maxExclusive'")
                self.parse_error(msg)

        if max_inclusive is not None and max_exclusive is not None:
            self.parse_error(_("cannot specify both 'maxInclusive' and 'maxExclusive'"))

        # Checks fraction digits
        if XSD_TOTAL_DIGITS in facets:
            if XSD_FRACTION_DIGITS in facets and \
                    facets[XSD_TOTAL_DIGITS].value < facets[XSD_FRACTION_DIGITS].value:
                msg = _("fractionDigits facet value cannot be lesser "
                        "than the value of totalDigits facet")
                self.parse_error(msg)

            total_digits = base_type.get_facet(XSD_TOTAL_DIGITS)
            if total_digits is not None and total_digits.value < facets[XSD_TOTAL_DIGITS].value:
                msg = _("totalDigits facet value cannot be greater than "
                        "the value of the same facet in the base type")
                self.parse_error(msg)

        # Checks XSD 1.1 facets
        if XSD_EXPLICIT_TIMEZONE in facets:
            explicit_tz_facet = base_type.get_facet(XSD_EXPLICIT_TIMEZONE)
            if explicit_tz_facet and explicit_tz_facet.value in ('prohibited', 'required') \
                    and facets[XSD_EXPLICIT_TIMEZONE].value != explicit_tz_facet.value:
                msg = _("the explicitTimezone facet value cannot be changed "
                        "if the base type has the same facet with value %r")
                self.parse_error(msg % explicit_tz_facet.value)

        self.min_length = min_length
        self.max_length = max_length

    @property
    def variety(self) -> Optional[str]:
        return None

    @property
    def simple_type(self) -> 'XsdSimpleType':
        return self

    @cached_property
    def min_value(self) -> Optional[AtomicValueType]:
        min_exclusive: Optional['AtomicValueType']
        min_inclusive: Optional['AtomicValueType']
        min_exclusive = cast(
            Optional['AtomicValueType'],
            getattr(self.get_facet(XSD_MIN_EXCLUSIVE), 'value', None)
        )
        min_inclusive = cast(
            Optional['AtomicValueType'],
            getattr(self.get_facet(XSD_MIN_INCLUSIVE), 'value', None)
        )

        if min_exclusive is None:
            return min_inclusive
        elif min_inclusive is None:
            return min_exclusive
        elif min_inclusive <= min_exclusive:  # type: ignore[operator]
            return min_exclusive
        else:
            return min_inclusive

    @cached_property
    def max_value(self) -> Optional[AtomicValueType]:
        max_exclusive: Optional['AtomicValueType']
        max_inclusive: Optional['AtomicValueType']
        max_exclusive = cast(
            Optional['AtomicValueType'],
            getattr(self.get_facet(XSD_MAX_EXCLUSIVE), 'value', None)
        )
        max_inclusive = cast(
            Optional['AtomicValueType'],
            getattr(self.get_facet(XSD_MAX_INCLUSIVE), 'value', None)
        )

        if max_exclusive is None:
            return max_inclusive
        elif max_inclusive is None:
            return max_exclusive
        elif max_inclusive >= max_exclusive:  # type: ignore[operator]
            return max_exclusive
        else:
            return max_inclusive

    @cached_property
    def enumeration(self) -> Optional[list[Optional[AtomicValueType]]]:
        enumeration = self.get_facet(XSD_ENUMERATION)
        if isinstance(enumeration, XsdEnumerationFacets):
            return enumeration.enumeration
        return None

    @property
    def admitted_facets(self) -> set[str]:
        return XSD_10_FACETS if self.xsd_version == '1.0' else XSD_11_FACETS

    @staticmethod
    def is_simple() -> bool:
        return True

    @staticmethod
    def is_complex() -> bool:
        return False

    @property
    def content_type_label(self) -> str:
        return 'empty' if self.max_length == 0 else 'simple'

    @property
    def root_type(self) -> BaseXsdType:
        if self.base_type is None:
            return self
        elif isinstance(self.base_type, XsdAtomic):
            return self.base_type.primitive_type
        else:
            return self.base_type.root_type

    @property
    def sequence_type(self) -> str:
        if self.is_empty():
            return 'empty-sequence()'

        root_type = self.root_type
        if root_type.name is not None:
            sequence_type = f'xs:{root_type.local_name}'
        else:
            sequence_type = 'xs:untypedAtomic'

        if not self.is_list():
            return sequence_type
        elif self.is_emptiable():
            return f'{sequence_type}*'
        else:
            return f'{sequence_type}+'

    def is_empty(self) -> bool:
        return self.max_length == 0 or \
            self.enumeration is not None and all(v == '' for v in self.enumeration)

    def is_emptiable(self) -> bool:
        return self.allow_empty

    def has_simple_content(self) -> bool:
        return self.max_length != 0

    def has_complex_content(self) -> bool:
        return False

    def has_mixed_content(self) -> bool:
        return False

    def is_element_only(self) -> bool:
        return False

    def is_derived(self, other: BaseXsdType, derivation: Optional[str] = None) -> bool:
        if derivation:
            if derivation == self.derivation:
                derivation = None  # derivation mode checked
            elif self.derivation:
                return False

        if other.ref is not None:
            other = other.ref

        if self is other or self.ref is other:
            return True
        elif other.name in self._special_types:
            return derivation != 'extension'
        elif self.base_type is other:
            return True
        elif self.base_type is None:
            if isinstance(other, XsdUnion):
                return any(self.is_derived(m, derivation) for m in other.member_types)
            return False
        elif self.base_type.is_complex():
            if not self.base_type.has_simple_content():
                return False
            return self.base_type.content.is_derived(other, derivation)  # type: ignore
        elif isinstance(other, XsdUnion):
            return any(self.is_derived(m, derivation) for m in other.member_types)
        else:
            return self.base_type.is_derived(other, derivation)

    def is_dynamic_consistent(self, other: BaseXsdType) -> bool:
        return other.name in (XSD_ANY_TYPE, XSD_ANY_SIMPLE_TYPE) or self.is_derived(other) or \
            isinstance(other, XsdUnion) and any(self.is_derived(mt) for mt in other.member_types)

    def normalize(self, text: Union[str, bytes]) -> str:
        """
        Normalize and restrict value-space with pre-lexical and lexical facets.

        :param text: text string encoded value.
        :return: a normalized string.
        """
        if isinstance(text, bytes):
            text = text.decode('utf-8')
        elif not isinstance(text, str):
            raise XMLSchemaValueError('argument is not a string: %r' % text)

        if self.white_space == 'replace':
            return self._REGEX_SPACE.sub(' ', text)
        elif self.white_space == 'collapse':
            return self._REGEX_SPACES.sub(' ', text).strip()
        else:
            return text

    def text_decode(self, text: str, validation: str = 'skip',
                    context: Optional[DecodeContext] = None) -> DecodedValueType:
        if context is None:
            self.schema.validation_context.clear()
            return self.raw_decode(text, validation, self.schema.validation_context)
        return self.raw_decode(text, validation, context)

    def text_is_valid(self, text: str, context: Optional[DecodeContext] = None) -> bool:
        if context is None:
            self.schema.validation_context.clear()
            self.raw_decode(text, 'lax', self.schema.validation_context)
            return not self.schema.validation_context.errors
        else:
            try:
                self.raw_decode(text, 'strict', context)
            except XMLSchemaValidationError:
                return False
            else:
                return True

    def get_atomic_value(self, value: AtomicValueType,
                         namespaces: Optional[NsmapType] = None,
                         strict: bool = False) -> AtomicValueType:
        return value

    def raw_decode(self, obj: Union[str, bytes], validation: str,
                   context: DecodeContext) -> DecodedValueType:
        text = self.normalize(obj)
        if self.patterns is not None:
            try:
                self.patterns(text)
            except XMLSchemaValidationError as err:
                context.validation_error(validation, self, err, obj)

        for validator in self.validators:
            try:
                validator(text)
            except XMLSchemaValidationError as err:
                context.validation_error(validation, self, err, obj)

        return text

    def raw_encode(self, obj: Any, validation: str, context: EncodeContext) \
            -> Optional[str]:
        if isinstance(obj, (str, bytes)):
            text = self.normalize(obj)
        else:
            obj = raw_encode_value(obj)
            text = '' if obj is None else obj

        if self.patterns is not None:
            try:
                self.patterns(text)
            except XMLSchemaValidationError as err:
                context.validation_error(validation, self, err)

        for validator in self.validators:
            try:
                validator(text)
            except XMLSchemaValidationError as err:
                context.validation_error(validation, self, err)

        return text if obj is not None else None

    def get_facet(self, tag: str) -> Optional[FacetsValueType]:
        return self.facets.get(tag)


#
# simpleType's derived classes:
class XsdAtomic(XsdSimpleType):
    """
    Class for atomic simpleType definitions. An atomic definition has a base_type
    attribute that refers to primitive or derived atomic built-in type or another
    derived simpleType. The primitive_type here is an extension of XSD definition
    of primitive type, useful for validation.
    """
    _special_types = {XSD_ANY_TYPE, XSD_ANY_SIMPLE_TYPE, XSD_ANY_ATOMIC_TYPE}
    _ADMITTED_TAGS = (XSD_RESTRICTION, XSD_SIMPLE_TYPE)
    primitive_type: XsdSimpleType

    __slots__ = ('primitive_type', 'base_type')

    def __init__(self, elem: ElementType,
                 schema: SchemaType,
                 parent: Optional[XsdComponent] = None,
                 name: Optional[str] = None,
                 facets: Optional[dict[Optional[str], FacetsValueType]] = None,
                 base_type: Optional[BaseXsdType] = None) -> None:

        if base_type is None:
            self.primitive_type = self
            self.base_type = None
        else:
            self._set_base_type(base_type)
        super().__init__(elem, schema, parent, name, facets)

    def __repr__(self) -> str:
        if self.name is None:
            return '%s(primitive_type=%r)' % (
                self.__class__.__name__, self.primitive_type.local_name
            )
        else:
            return '%s(name=%r)' % (self.__class__.__name__, self.prefixed_name)

    def _set_base_type(self, base_type: BaseXsdType) -> None:
        self.base_type = base_type
        if not hasattr(self, 'white_space') and hasattr(base_type, 'white_space'):
            self.white_space = base_type.white_space

        if hasattr(base_type, 'primitive_type'):
            self.primitive_type = base_type.primitive_type
        elif isinstance(base_type, XsdSimpleType):
            self.primitive_type = base_type  # xs:union, xs:list or a special type
        elif hasattr(base_type.content, 'primitive_type'):
            self.primitive_type = base_type.content.primitive_type
        else:
            assert isinstance(base_type.content, XsdSimpleType)
            self.primitive_type = base_type.content

    @property
    def variety(self) -> Optional[str]:
        return 'atomic'

    @property
    def admitted_facets(self) -> set[str]:
        if self.primitive_type.is_complex():
            return XSD_10_FACETS if self.xsd_version == '1.0' else XSD_11_FACETS
        return self.primitive_type.admitted_facets

    def is_datetime(self) -> bool:
        return issubclass(self.primitive_type.python_type, AbstractDateTime)

    def get_facet(self, tag: str) -> Optional[FacetsValueType]:
        facet = self.facets.get(tag)
        if facet is not None:
            return facet
        elif self.base_type is not None:
            return self.base_type.get_facet(tag)
        else:
            return None

    def get_atomic_value(self, value: AtomicValueType,
                         namespaces: Optional[NsmapType] = None,
                         strict: bool = False) -> AtomicValueType:
        """
        Returns a full decoded atomic value for the given value. Used for ensuring
        that the value is compliant for facets validation. If *strict* is `True`
        keeps the original value unchanged, otherwise raises an error.
        """
        if self.primitive_type is not self:
            return self.primitive_type.get_atomic_value(value, namespaces, strict)
        elif not isinstance(value, self.python_type):
            try:
                return self.to_python(value)  # type: ignore[arg-type]
            except (ValueError, DecimalException, TypeError):
                if strict:
                    raise
        elif self.is_qname():
            if isinstance(value, str):
                return get_extended_qname(value, namespaces)
            elif isinstance(value, AbstractQName):
                return value.expanded_name

        return value

    def is_atomic(self) -> bool:
        return True

    def is_primitive(self) -> bool:
        return self.base_type is None


class XsdAtomicBuiltin(XsdAtomic):
    """
    Class for defining XML Schema built-in simpleType atomic datatypes. An instance
    contains a Python's type transformation and a list of validator functions. The
    'base_type' is not used for validation, but only for reference to the XML Schema
    restriction hierarchy.

    Type conversion methods:
      - to_python(value): Decoding from XML
      - from_python(value): Encoding to XML
    """
    __slots__ = ('datatype', 'instance_types', 'python_type', 'to_python', 'from_python',
                 'post_decode', '_admitted_facets')

    def __init__(self, elem: ElementType,
                 schema: SchemaType,
                 name: str,
                 datatype: Type[AnyAtomicType],
                 python_type: PythonTypeClasses,
                 base_type: Optional['XsdAtomicBuiltin'] = None,
                 admitted_facets: Optional[set[str]] = None,
                 facets: Optional[dict[Optional[str], FacetsValueType]] = None,
                 to_python: Optional[Callable[[Any], AtomicValueType]] = None,
                 from_python: Optional[Callable[[Any], str]] = None) -> None:
        """
        :param name: the XSD type's qualified name.
        :param datatype: the XSD datatype.
        :param python_type: the correspondent Python's type. If a tuple of types \
        is provided uses the first and consider the others as compatible types.
        :param base_type: the reference base type, None if it's a primitive type.
        :param admitted_facets: admitted facets tags for type (required for primitive types).
        :param facets: optional facets validators.
        :param to_python: optional decode function.
        :param from_python: optional encode function.
        """
        if isinstance(python_type, tuple):
            self.instance_types, python_type = python_type, python_type[0]
        else:
            self.instance_types = python_type

        if not isinstance(datatype, type):
            raise XMLSchemaTypeError(f"{datatype!r} object is not a type")

        if not isinstance(python_type, type):
            raise XMLSchemaTypeError(f"{python_type!r} object is not a type")

        if base_type is None and not admitted_facets and name != XSD_ERROR:
            raise XMLSchemaValueError("argument 'admitted_facets' must be "
                                      "a not empty set of a primitive type")
        self._admitted_facets = admitted_facets

        super().__init__(elem, schema, None, name, facets, base_type)
        self.datatype = datatype
        self.python_type = python_type
        self.to_python = to_python if to_python is not None else python_type
        self.from_python = from_python if from_python is not None else str

        self.post_decode = name in (XSD_QNAME, XSD_NOTATION, XSD_ID, XSD_IDREF)

    def __repr__(self) -> str:
        return '%s(name=%r)' % (self.__class__.__name__, self.prefixed_name)

    @property
    def admitted_facets(self) -> set[str]:
        return self._admitted_facets or self.primitive_type.admitted_facets

    def raw_decode(self, obj: Union[str, bytes], validation: str,
                   context: DecodeContext) -> DecodedValueType:
        if isinstance(obj, (str, bytes)):
            obj = self.normalize(obj)
        elif not isinstance(obj, self.instance_types):
            msg = _("value is not an instance of {!r}").format(self.instance_types)
            context.decode_error(validation, self, obj, self.to_python, msg)

        if validation == 'skip':
            try:
                return self.to_python(obj)
            except (ValueError, TypeError, DecimalException):
                return raw_encode_value(obj)

        if self.patterns is not None:
            try:
                self.patterns(obj)
            except XMLSchemaValidationError as err:
                context.validation_error(validation, self, err)

        try:
            result: DecodedValueType = self.to_python(obj)
        except (ValueError, DecimalException) as err:
            context.decode_error(validation, self, obj, self.to_python, err)
            return None
        except TypeError:
            # xs:error type (e.g. an XSD 1.1 type alternative used to catch invalid values)
            reason = _("invalid value {!r}").format(obj)
            context.validation_error(validation, self, reason, obj)
            return None

        for validator in self.validators:
            try:
                validator(result)
            except XMLSchemaValidationError as err:
                context.validation_error(validation, self, err)

        if self.post_decode:
            if self.name == XSD_QNAME:
                if ':' not in obj:
                    default_namespace = context.namespaces.get('')
                    if default_namespace:
                        result = f"{{{default_namespace}}}{obj}"
                else:
                    try:
                        prefix, name = obj.split(':')
                    except ValueError:
                        pass
                    else:
                        try:
                            result = f"{{{context.namespaces[prefix]}}}{name}"
                        except (TypeError, KeyError):
                            if context.root_namespace != XSD_NAMESPACE:
                                # For a schema is already found by meta-schema validation
                                reason = _("unmapped prefix %r in a QName") % prefix
                                context.validation_error(validation, self, reason, obj)

            elif not context.check_identities:
                pass  # context created from a component
            elif self.name == XSD_IDREF:
                if obj not in context.id_map:
                    context.id_map[obj] = 0
            elif context.level:
                if context.id_list is None:
                    if not context.id_map[obj]:
                        context.id_map[obj] = 1
                    else:
                        reason = _("duplicated xs:ID value {!r}").format(obj)
                        context.validation_error(validation, self, reason, obj)
                elif not context.id_map[obj]:
                    context.id_map[obj] = 1
                    context.id_list.append(obj)
                    if len(context.id_list) > 1 and self.xsd_version == '1.0':
                        reason = _("no more than one attribute of type ID should "
                                   "be present in an element")
                        context.validation_error(validation, self, reason, obj)

                elif obj not in context.id_list or self.xsd_version == '1.0':
                    reason = _("duplicated xs:ID value {!r}").format(obj)
                    context.validation_error(validation, self, reason, obj)

        return result

    def raw_encode(self, obj: Any, validation: str, context: EncodeContext) \
            -> Optional[str]:
        if isinstance(obj, (str, bytes)):
            obj = self.normalize(obj)

        if validation == 'skip':
            try:
                return self.from_python(obj)
            except ValueError:
                return raw_encode_value(obj)

        if isinstance(obj, bool) and self.name != XSD_BOOLEAN:
            msg = _("boolean value {0!r} requires a {1!r} decoder").format(obj, bool)
            context.encode_error(validation, self, obj, self.from_python, msg)

        if isinstance(obj, str):
            try:
                value = self.to_python(obj)
            except (ValueError, TypeError) as err:
                context.encode_error(validation, self, obj, self.to_python, err)
                return None

            text = obj
        else:
            if not isinstance(obj, self.instance_types):
                if not context.untyped_data or not isinstance(obj, UntypedAtomic):
                    msg = _("{0!r} is not an instance of {1!r}").format(obj, self.instance_types)
                    context.encode_error(validation, self, obj, self.to_python, msg)

                try:
                    obj = self.python_type(obj)
                except (ValueError, TypeError) as err:
                    context.encode_error(validation, self, obj, self.to_python, err)
                    return None

            try:
                text = self.from_python(obj)
            except ValueError as err:
                context.encode_error(validation, self, obj, self.from_python, err)
                return None

            value = obj

        for validator in self.validators:
            try:
                validator(value)
            except XMLSchemaValidationError as err:
                context.validation_error(validation, self, err)

        if self.patterns is not None:
            try:
                self.patterns(text)
            except XMLSchemaValidationError as error:
                context.validation_error(validation, self, error)

        return text


class XsdList(XsdSimpleType):
    """
    Class for 'list' definitions. A list definition has an item_type attribute
    that refers to an atomic or union simpleType definition.

    ..  <list
          id = ID
          itemType = QName
          {any attributes with non-schema namespace ...}>
          Content: (annotation?, simpleType?)
        </list>
    """
    item_type: XsdSimpleType
    _ADMITTED_TAGS = XSD_LIST,
    _white_space_elem = ElementTree.Element(
        XSD_WHITE_SPACE, attrib={'value': 'collapse', 'fixed': 'true'}
    )

    __slots__ = ('item_type',)

    def __init__(self, elem: ElementType,
                 schema: SchemaType,
                 parent: Optional[XsdComponent],
                 name: Optional[str] = None) -> None:
        facets: Optional[dict[Optional[str], FacetsValueType]] = {
            XSD_WHITE_SPACE: XsdWhiteSpaceFacet(self._white_space_elem, schema, self, self)
        }
        super().__init__(elem, schema, parent, name, facets)

        if not self.item_type.allow_empty and self.min_length:
            self.allow_empty = False

    def __repr__(self) -> str:
        if self.name is None:
            return '%s(item_type=%r)' % (self.__class__.__name__, self.item_type)
        else:
            return '%s(name=%r)' % (self.__class__.__name__, self.prefixed_name)

    def parse(self, elem: ElementType) -> None:
        if elem.tag != XSD_LIST:
            if elem.tag == XSD_SIMPLE_TYPE:
                for child in elem:
                    if child.tag == XSD_LIST:
                        super().parse(child)
                        return
            raise XMLSchemaValueError(
                f"a {XSD_LIST!r} definition required for {self!r}"
            )
        super().parse(elem)

    def _parse(self) -> None:
        item_type: Any

        child = self._parse_child_component(self.elem)
        if child is not None:
            # Case of a local simpleType declaration inside the list tag
            try:
                item_type = self.builders.simple_type_factory(child, self.schema, self)
            except XMLSchemaParseError as err:
                self.parse_error(err)
                item_type = self.any_atomic_type

            if 'itemType' in self.elem.attrib:
                self.parse_error(_("ambiguous list type declaration"))

        else:
            # List tag with itemType attribute that refers to a global type
            try:
                item_qname = self.schema.resolve_qname(self.elem.attrib['itemType'])
            except (KeyError, ValueError, RuntimeError) as err:
                if 'itemType' not in self.elem.attrib:
                    self.parse_error(_("missing list type declaration"))
                else:
                    self.parse_error(err)
                item_type = self.any_atomic_type
            else:
                try:
                    item_type = self.maps.types[item_qname]
                except KeyError:
                    msg = _("unknown type {!r}")
                    self.parse_error(msg.format(self.elem.attrib['itemType']))
                    item_type = self.any_atomic_type
                except XMLSchemaCircularityError as err:
                    self.parse_error(err, err.elem)
                    item_type = self.any_atomic_type

        if item_type.final == '#all' or 'list' in item_type.final:
            msg = _("'final' value of the itemType %r forbids derivation by list")
            self.parse_error(msg % item_type)

        if item_type.name == XSD_ANY_ATOMIC_TYPE:
            msg = _("cannot use xs:anyAtomicType as base type of a user-defined type")
            self.parse_error(msg)

        if item_type.is_atomic():
            self.item_type = item_type
        else:
            self.parse_error(_("%r: a list must be based on atomic data types") % item_type)
            self.item_type = self.any_atomic_type

    @property
    def variety(self) -> Optional[str]:
        return 'list'

    @property
    def admitted_facets(self) -> set[str]:
        return XSD_10_LIST_FACETS if self.xsd_version == '1.0' else XSD_11_LIST_FACETS

    @property
    def root_type(self) -> BaseXsdType:
        return self.item_type.root_type

    def is_atomic(self) -> bool:
        return False

    def is_list(self) -> bool:
        return True

    def is_derived(self, other: BaseXsdType, derivation: Optional[str] = None) -> bool:
        if other.ref is not None:
            other = other.ref
        if derivation and derivation == self.derivation:
            derivation = None  # derivation mode checked

        if derivation and self.derivation and derivation != self.derivation:
            return False
        elif self is other or self.ref is other:
            return True
        elif other.name in self._special_types:
            return derivation != 'extension'
        elif self.item_type is other:
            return True
        else:
            return False

    def iter_components(self, xsd_classes: ComponentClassType = None) \
            -> Iterator[XsdComponent]:
        if xsd_classes is None or isinstance(self, xsd_classes):
            yield self
        if self.item_type.parent is not None:
            yield from self.item_type.iter_components(xsd_classes)

    def get_atomic_value(self, value: AtomicValueType,
                         namespaces: Optional[NsmapType] = None,
                         strict: bool = False) -> AtomicValueType:
        return self.item_type.get_atomic_value(value, namespaces=namespaces, strict=strict)

    def raw_decode(self, obj: Union[str, bytes], validation: str, context: DecodeContext) \
            -> list[Optional[AtomicValueType]]:
        items = []
        for chunk in self.normalize(obj).split():
            result = self.item_type.raw_decode(chunk, validation, context)

            if isinstance(result, list):
                reason = _("unexpected nested list item {!r}").format(obj)
                context.validation_error(validation, self, reason, obj)
                items.extend(result)
                continue
            elif isinstance(result, context.keep_datatypes) or result is None:
                pass
            elif isinstance(result, str):
                if result[:1] == '{' and self.is_qname():
                    result = chunk
            elif isinstance(result, Decimal):
                if context.decimal_type is not None:
                    result = context.decimal_type(result)
            elif isinstance(result, (AbstractDateTime, Duration)):
                result = chunk.strip()
            else:
                result = str(result)

            items.append(result)
        else:
            return items

    def raw_encode(self, obj: Any, validation: str, context: EncodeContext) -> Optional[str]:
        if not hasattr(obj, '__iter__') or isinstance(obj, (str, bytes)):
            obj = [obj]

        encoded_items: list[Any] = []
        for item in obj:
            encoded_items.append(self.item_type.raw_encode(item, validation, context))

        return ' '.join(item for item in encoded_items if item is not None)


class XsdUnion(XsdSimpleType):
    """
    Class for 'union' definitions. A union definition has a member_types
    attribute that refers to a 'simpleType' definition.

    ..  <union
          id = ID
          memberTypes = list of QName
          {any attributes with non-schema namespace ...}>
          Content: (annotation?, simpleType*)
        </union>
    """
    member_types: list[XsdSimpleType]
    _ADMITTED_TYPES: Any = XsdSimpleType
    _ADMITTED_TAGS = XSD_UNION,

    __slots__ = ('member_types',)

    def __init__(self, elem: ElementType,
                 schema: SchemaType,
                 parent: Optional[XsdComponent],
                 name: Optional[str] = None) -> None:
        super().__init__(elem, schema, parent, name, facets=None)

    def __repr__(self) -> str:
        if self.name is None:
            return '%s(member_types=%r)' % (self.__class__.__name__, self.member_types)
        else:
            return '%s(name=%r)' % (self.__class__.__name__, self.prefixed_name)

    def parse(self, elem: ElementType) -> None:
        if elem.tag != XSD_UNION:
            if elem.tag == XSD_SIMPLE_TYPE:
                for child in elem:
                    if child.tag == XSD_UNION:
                        super().parse(child)
                        return
            raise XMLSchemaValueError(
                f"a {XSD_UNION!r} definition required for {self!r}"
            )
        super().parse(elem)

    def _parse(self) -> None:
        mt: Any
        self.member_types = []

        for child in self.elem:
            if child.tag != XSD_ANNOTATION and not callable(child.tag):
                mt = self.builders.simple_type_factory(child, self.schema, self)
                if isinstance(mt, XMLSchemaParseError):
                    self.parse_error(mt)
                else:
                    self.member_types.append(mt)

        if 'memberTypes' in self.elem.attrib:
            for name in self.elem.attrib['memberTypes'].split():
                try:
                    type_qname = self.schema.resolve_qname(name)
                except (KeyError, ValueError, RuntimeError) as err:
                    self.parse_error(err)
                    continue

                try:
                    mt = self.maps.types[type_qname]
                except KeyError:
                    self.parse_error(_("unknown type {!r}").format(type_qname))
                    mt = self.any_atomic_type
                except XMLSchemaParseError as err:
                    self.parse_error(err)
                    mt = self.any_atomic_type
                except XMLSchemaCircularityError as err:
                    self.parse_error(err, err.elem)
                    continue

                if not isinstance(mt, self._ADMITTED_TYPES):
                    msg = _("a {0!r} required, not {1!r}")
                    self.parse_error(msg.format(self._ADMITTED_TYPES, mt))
                    continue
                elif mt.final == '#all' or 'union' in mt.final:
                    msg = _("'final' value of the memberTypes %r forbids derivation by union")
                    self.parse_error(msg % self.member_types)

                self.member_types.append(mt)

        if not self.member_types:
            self.parse_error(_("missing xs:union type declarations"))
            self.member_types = [self.any_atomic_type]
        elif any(mt.name == XSD_ANY_ATOMIC_TYPE for mt in self.member_types):
            msg = _("cannot use xs:anyAtomicType as base type of a user-defined type")
            self.parse_error(msg)
        else:
            if all(not mt.allow_empty for mt in self.member_types):
                self.allow_empty = False

    @property
    def variety(self) -> Optional[str]:
        return 'union'

    @property
    def admitted_facets(self) -> set[str]:
        return XSD_10_UNION_FACETS if self.xsd_version == '1.0' else XSD_11_UNION_FACETS

    def is_atomic(self) -> bool:
        return all(mt.is_atomic() for mt in self.member_types)

    def is_list(self) -> bool:
        return all(mt.is_list() for mt in self.member_types)

    def is_key(self) -> bool:
        return any(mt.is_key() for mt in self.member_types)

    def is_union(self) -> bool:
        return True

    def is_dynamic_consistent(self, other: Any) -> bool:
        return other.name in (XSD_ANY_TYPE, XSD_ANY_SIMPLE_TYPE) or \
            other.is_derived(self) or isinstance(other, self.__class__) and \
            any(mt1.is_derived(mt2) for mt1 in other.member_types for mt2 in self.member_types)

    def iter_components(self, xsd_classes: ComponentClassType = None) \
            -> Iterator[XsdComponent]:
        if xsd_classes is None or isinstance(self, xsd_classes):
            yield self
        for mt in filter(lambda x: x.parent is not None, self.member_types):
            yield from mt.iter_components(xsd_classes)

    def get_atomic_value(self, value: AtomicValueType,
                         namespaces: Optional[NsmapType] = None,
                         strict: bool = False) -> AtomicValueType:
        values = []
        for mt in self.member_types:
            try:
                values.append(mt.get_atomic_value(value, namespaces, strict=True))
            except (TypeError, ValueError, DecimalException):
                pass

        if not values:
            if strict:
                msg = f'{self!r} has not compatible types for decoding the given value'
                raise XMLSchemaTypeError(msg)
            return value
        elif any(v == value for v in values):
            return value
        else:
            return values[0]

    def raw_decode(self, obj: Union[str, bytes], validation: str, context: DecodeContext) \
            -> DecodedValueType:
        patterns = context.patterns  # Use and clean pushed patterns
        context.patterns = None

        xsd_type = None
        for mt in self.member_types:
            try:
                result = mt.raw_decode(obj, 'strict', context)
            except XMLSchemaValidationError as err:
                if xsd_type is None and not isinstance(err, XMLSchemaDecodeError):
                    xsd_type = mt
            else:
                if patterns and isinstance(obj, (str, bytes)):
                    try:
                        patterns(mt.normalize(obj))
                    except XMLSchemaValidationError as err:
                        context.validation_error(validation, self, err)
                return result

        if validation == 'skip':
            return raw_encode_value(obj)
        elif validation == 'lax' and xsd_type is not None:
            result = xsd_type.raw_decode(obj, validation, context)
            if patterns and isinstance(obj, (str, bytes)):
                try:
                    patterns(xsd_type.normalize(obj))
                except XMLSchemaValidationError as err:
                    context.validation_error(validation, self, err)
            return result

        msg = _("invalid value {!r}").format(obj)
        context.decode_error(validation, self, obj, self.member_types, msg)
        return None

    def raw_encode(self, obj: Any, validation: str, context: EncodeContext) -> Optional[str]:
        patterns = context.patterns  # Use and clean pushed patterns
        context.patterns = None

        xsd_type = None
        for mt in self.member_types:
            try:
                result = mt.raw_encode(obj, 'strict', context)
            except XMLSchemaValidationError as err:
                if xsd_type is None and not isinstance(err, XMLSchemaEncodeError):
                    xsd_type = mt
            else:
                if patterns and isinstance(result, str):
                    try:
                        patterns(mt.normalize(result))
                    except XMLSchemaValidationError as err:
                        context.validation_error(validation, self, err)
                return result

        if validation == 'skip':
            return raw_encode_value(obj)
        elif validation == 'lax' and xsd_type is not None:
            result = xsd_type.raw_encode(obj, validation, context)
            if patterns and isinstance(result, str):
                try:
                    patterns(result)
                except XMLSchemaValidationError as err:
                    context.validation_error(validation, self, err)
            return result

        msg = _("no type suitable for encoding the object")
        context.encode_error(validation, self, obj, self.member_types, msg)
        return None


class Xsd11Union(XsdUnion):
    _ADMITTED_TYPES = XsdAtomic, XsdList, XsdUnion


class XsdAtomicRestriction(XsdAtomic):
    """
    Class for XSD 1.0 atomic simpleType and complexType's simpleContent restrictions.

    ..  <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>
    """
    parent: 'XsdSimpleType'
    base_type: BaseXsdType
    derivation = 'restriction'
    _CONTENT_TAIL_TAGS = {XSD_ATTRIBUTE, XSD_ATTRIBUTE_GROUP, XSD_ANY_ATTRIBUTE}

    def parse(self, elem: ElementType) -> None:
        if self.name != XSD_ANY_ATOMIC_TYPE and elem.tag != XSD_RESTRICTION:
            if not (elem.tag == XSD_SIMPLE_TYPE and elem.get('name') is not None):
                raise XMLSchemaValueError(
                    "an xs:restriction definition required for %r." % self
                )
        super().parse(elem)

    def _parse(self) -> None:
        elem = self.elem
        if elem.get('name') == XSD_ANY_ATOMIC_TYPE:
            return  # skip special type xs:anyAtomicType
        elif elem.tag == XSD_SIMPLE_TYPE and elem.get('name') is not None:
            # Global simpleType with internal restriction
            elem = cast(ElementType, self._parse_child_component(elem))

        if self.name is not None and self.parent is not None:
            msg = _("'name' attribute in a local simpleType definition")
            self.parse_error(msg)

        base_type: Any = None
        facets: Any = {}
        has_attributes = False
        has_simple_type_child = False

        if 'base' in elem.attrib:
            try:
                base_qname = self.schema.resolve_qname(elem.attrib['base'])
            except (KeyError, ValueError, RuntimeError) as err:
                self.parse_error(err)
                base_type = self.any_atomic_type
            else:
                if base_qname == self.name:
                    if self.redefine is None:
                        msg = _("wrong definition with self-reference")
                        self.parse_error(msg)
                        base_type = self.any_atomic_type
                    else:
                        base_type = self.base_type
                else:
                    if self.redefine is not None:
                        msg = _("wrong redefinition without self-reference")
                        self.parse_error(msg)

                    try:
                        base_type = self.maps.types[base_qname]
                    except KeyError:
                        self.parse_error(_("unknown type {!r}").format(elem.attrib['base']))

                        base_type = self.any_atomic_type
                    except XMLSchemaParseError as err:
                        self.parse_error(err)
                        base_type = self.any_atomic_type
                    except XMLSchemaCircularityError as err:
                        self.parse_error(err, err.elem)
                        base_type = self.any_atomic_type

            if base_type.is_simple() and base_type.name == XSD_ANY_SIMPLE_TYPE:
                msg = _("wrong base type %r, an atomic type required")
                self.parse_error(msg % XSD_ANY_SIMPLE_TYPE)
            elif base_type.is_complex():
                if base_type.mixed and base_type.is_emptiable():
                    child = self._parse_child_component(elem, strict=False)
                    if child is None:
                        msg = _("an xs:simpleType definition expected")
                        self.parse_error(msg)
                    elif child.tag != XSD_SIMPLE_TYPE:
                        # See: "http://www.w3.org/TR/xmlschema-2/#element-restriction"
                        self.parse_error(_(
                            "when a complexType with simpleContent restricts a complexType "
                            "with mixed and with emptiable content then a simpleType child "
                            "declaration is required"
                        ))
                elif self.parent is None or self.parent.is_simple():
                    msg = _("simpleType restriction of %r is not allowed")
                    self.parse_error(msg % base_type)

        for child in elem:
            if child.tag == XSD_ANNOTATION or callable(child.tag):
                continue
            elif child.tag in self._CONTENT_TAIL_TAGS:
                has_attributes = True  # only if it's a complexType restriction
            elif has_attributes:
                msg = _("unexpected tag after attribute declarations")
                self.parse_error(msg)
            elif child.tag == XSD_SIMPLE_TYPE:
                # Case of simpleType declaration inside a restriction
                if has_simple_type_child:
                    msg = _("duplicated simpleType declaration")
                    self.parse_error(msg)

                if base_type is None:
                    try:
                        base_type = self.builders.simple_type_factory(
                            child, self.schema, self
                        )
                    except XMLSchemaParseError as err:
                        self.parse_error(err, child)
                        base_type = self.any_simple_type
                elif base_type.is_complex():
                    if base_type.admit_simple_restriction():
                        base_type = self.builders.complex_type_class(
                            elem=elem,
                            schema=self.schema,
                            parent=self,
                            content=self.builders.simple_type_factory(
                                child, self.schema, self
                            ),
                            attributes=base_type.attributes,
                            mixed=base_type.mixed,
                            block=base_type.block,
                            final=base_type.final,
                        )
                elif 'base' in elem.attrib:
                    msg = _("restriction with 'base' attribute and simpleType declaration")
                    self.parse_error(msg)

                has_simple_type_child = True
            else:
                try:
                    facet_class = self.builders.facets[child.tag]
                except KeyError:
                    self.parse_error(_("unexpected tag %r in restriction") % child.tag)
                    continue

                if child.tag not in facets:
                    facets[child.tag] = facet_class(child, self.schema, self, base_type)
                elif child.tag not in MULTIPLE_FACETS:
                    msg = _("multiple %r constraint facet")
                    self.parse_error(msg % local_name(child.tag))
                elif child.tag != XSD_ASSERTION:
                    facets[child.tag].append(child)
                else:
                    assertion = facet_class(child, self.schema, self, base_type)
                    try:
                        facets[child.tag].append(assertion)
                    except AttributeError:
                        facets[child.tag] = [facets[child.tag], assertion]

        if base_type is None:
            self.parse_error(_("missing base type in restriction"))
        elif base_type.final == '#all' or 'restriction' in base_type.final:
            msg = _("'final' value of the baseType %r forbids derivation by restriction")
            self.parse_error(msg % base_type)
        if base_type.name == XSD_ANY_ATOMIC_TYPE:
            msg = _("cannot use xs:anyAtomicType as base type of a user-defined type")
            self.parse_error(msg)

        self._set_base_type(base_type)
        self.facets = facets

    @property
    def variety(self) -> Optional[str]:
        return cast(Optional[str], getattr(self.base_type, 'variety', None))

    def iter_components(self, xsd_classes: ComponentClassType = None) \
            -> Iterator[XsdComponent]:
        if xsd_classes is None:
            yield self
            for facet in self.facets.values():
                if isinstance(facet, list):
                    yield from facet  # XSD 1.1 assertions can be more than one
                elif isinstance(facet, XsdFacet):
                    yield facet  # only XSD facets, skip callables
        else:
            if isinstance(self, xsd_classes):
                yield self
            if issubclass(XsdFacet, xsd_classes):
                for facet in self.facets.values():
                    if isinstance(facet, list):
                        yield from facet
                    elif isinstance(facet, XsdFacet):
                        yield facet

        if self.base_type.parent is not None:
            yield from self.base_type.iter_components(xsd_classes)

    def raw_decode(self, obj: Union[str, bytes], validation: str,
                   context: DecodeContext) -> DecodedValueType:

        if isinstance(obj, (str, bytes)):
            obj = self.normalize(obj)

            if self.patterns:
                if not isinstance(self.primitive_type, XsdUnion):
                    try:
                        self.patterns(obj)
                    except XMLSchemaValidationError as err:
                        context.validation_error(validation, self, err)
                elif context.patterns is None:
                    context.patterns = self.patterns

        if isinstance(self.base_type, XsdSimpleType):
            base_type = self.base_type
        elif isinstance(self.base_type.content, XsdSimpleType):
            base_type = self.base_type.content
        elif self.base_type.mixed:
            return obj
        else:  # pragma: no cover
            msg = _("wrong base type %r: a simpleType or a complexType "
                    "with simple or mixed content required")
            raise XMLSchemaValueError(msg % self.base_type)

        result = base_type.raw_decode(obj, validation, context)
        if result is not None:
            for validator in self.validators:
                try:
                    validator(result)
                except XMLSchemaValidationError as err:
                    context.validation_error(validation, self, err)

        return result

    def raw_encode(self, obj: Any, validation: str, context: EncodeContext) -> Optional[str]:
        base_type: XsdSimpleType
        if isinstance(self.base_type, XsdSimpleType):
            base_type = self.base_type
        elif isinstance(self.base_type.content, XsdSimpleType) and self.max_length != 0:
            base_type = self.base_type.content
        elif self.base_type.mixed:
            return str(obj)
        else:  # pragma: no cover
            msg = _("wrong base type %r: a simpleType or a complexType "
                    "with simple or mixed content required")
            raise XMLSchemaValueError(msg % self.base_type)

        if self.is_list():
            if not hasattr(obj, '__iter__') or isinstance(obj, (str, bytes)):
                obj = [] if obj is None or obj == '' else [obj]
        elif isinstance(obj, (str, bytes)):
            obj = self.normalize(obj)

        if self.patterns:
            if context.patterns is None and isinstance(self.primitive_type, XsdUnion):
                context.patterns = self.patterns

        result = base_type.raw_encode(obj, validation, context)

        if self.validators:
            value: DecodedValueType
            if isinstance(obj, list):
                value = [self.get_atomic_value(x, context.namespaces) for x in obj]
            else:
                value = self.get_atomic_value(obj, context.namespaces)

            for validator in self.validators:
                try:
                    validator(value)
                except XMLSchemaValidationError as err:
                    context.validation_error(validation, self, err)

        if self.patterns and not isinstance(self.primitive_type, XsdUnion) and result is not None:
            try:
                self.patterns(result)
            except XMLSchemaValidationError as err:
                context.validation_error(validation, self, err)

        return result

    def is_list(self) -> bool:
        return self.primitive_type.is_list()

    def is_union(self) -> bool:
        return self.primitive_type.is_union()


class Xsd11AtomicRestriction(XsdAtomicRestriction):
    """
    Class for XSD 1.1 atomic simpleType and complexType's simpleContent restrictions.

    ..  <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 | assertion | explicitTimezone |
          {any with namespace: ##other})*))
        </restriction>
    """
    _CONTENT_TAIL_TAGS = {XSD_ATTRIBUTE, XSD_ATTRIBUTE_GROUP, XSD_ANY_ATTRIBUTE, XSD_ASSERT}