Skip to content

process_mixed_content_class

xsdata.codegen.handlers.process_mixed_content_class

ProcessMixedContentClass

Bases: HandlerInterface

Mixed content handler.

Source code in xsdata/codegen/handlers/process_mixed_content_class.py
 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
class ProcessMixedContentClass(HandlerInterface):
    """Mixed content handler."""

    __slots__ = ()

    def process(self, target: Class):
        """Add a wildcard attr if the class supports mixed content.

        All other elements will be moved as the wildcard attr choices.

        Args:
            target: The target class instance
        """
        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

process(target)

Add a wildcard attr if the class supports mixed content.

All other elements will be moved as the wildcard attr choices.

Parameters:

Name Type Description Default
target Class

The target class instance

required
Source code in xsdata/codegen/handlers/process_mixed_content_class.py
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
def process(self, target: Class):
    """Add a wildcard attr if the class supports mixed content.

    All other elements will be moved as the wildcard attr choices.

    Args:
        target: The target class instance
    """
    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