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
|
import sys
from xsdata.codegen.mixins import HandlerInterface
from xsdata.codegen.models import Attr, AttrType, Class, Restrictions
from xsdata.models.enums import DataType, NamespaceType, Tag
class ProcessMixedContentClass(HandlerInterface):
"""
Mixed content handler.
If the target class supports mixed content, a new wildcard attr will
replace the originals except any attributes. All the previous attrs
derived from xs:element will be moved as choices for the new
content attr.
"""
__slots__ = ()
def process(self, target: Class):
if not target.is_mixed:
return
attrs = []
choices = []
for attr in list(target.attrs):
if attr.is_attribute:
attrs.append(attr)
elif not attr.is_any_type:
choice = attr.clone()
choice.restrictions.min_occurs = None
choice.restrictions.max_occurs = None
choice.restrictions.sequence = None
choices.append(choice)
wildcard = Attr(
name="content",
types=[AttrType(qname=str(DataType.ANY_TYPE), native=True)],
tag=Tag.ANY,
mixed=True,
choices=choices,
namespace=NamespaceType.ANY_NS,
restrictions=Restrictions(min_occurs=0, max_occurs=sys.maxsize),
)
attrs.append(wildcard)
target.attrs = attrs
|