Skip to content

wildcard

xsdata.formats.dataclass.parsers.nodes.wildcard

WildcardNode

Bases: XmlNode

XmlNode for extensible elements that can hold any attribute and content.

The resulting object tree will be a :class:~xsdata.formats.dataclass.models.generics.AnyElement instance.

Parameters:

Name Type Description Default
var XmlVar

The xml var instance

required
attrs Dict

The element attributes

required
ns_map Dict

The element namespace prefix-URI map

required
position int

The current objects length, everything after this position are considered children of this node.

required
factory Type

The generic element factory

required
Source code in xsdata/formats/dataclass/parsers/nodes/wildcard.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
 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
class WildcardNode(XmlNode):
    """XmlNode for extensible elements that can hold any attribute and content.

    The resulting object tree will be a
    :class:`~xsdata.formats.dataclass.models.generics.AnyElement`
    instance.

    Args:
        var: The xml var instance
        attrs: The element attributes
        ns_map: The element namespace prefix-URI map
        position: The current objects length, everything after
            this position are considered children of this node.
        factory: The generic element factory
    """

    __slots__ = "var", "attrs", "ns_map", "position", "factory"

    def __init__(
        self,
        var: XmlVar,
        attrs: Dict,
        ns_map: Dict,
        position: int,
        factory: Type,
    ):
        self.var = var
        self.attrs = attrs
        self.ns_map = ns_map
        self.position = position
        self.factory = factory

    def bind(
        self, qname: str, text: Optional[str], tail: Optional[str], objects: List
    ) -> bool:
        """Bind the parsed data into a generic element.

        This entry point is called when a xml element ends and is
        responsible to parse the current element attributes/text, bind
        any children objects and initialize new generic element that
        can fit any xml string.

        Args:
            qname: The element qualified name
            text: The element text content
            tail: The element tail content
            objects: The list of intermediate parsed objects

        Returns:
            Whether the binding process was successful or not.
        """
        children = self.fetch_any_children(self.position, objects)
        attributes = ParserUtils.parse_any_attributes(self.attrs, self.ns_map)
        derived = qname != self.var.qname
        text = ParserUtils.normalize_content(text) if children else text
        text = "" if text is None and not self.var.nillable else text
        tail = ParserUtils.normalize_content(tail)

        if tail or attributes or children or self.var.is_wildcard or derived:
            obj = self.factory(
                qname=qname,
                text=text,
                tail=tail,
                attributes=attributes,
                children=children,
            )
            objects.append((self.var.qname, obj))
        else:
            objects.append((self.var.qname, text))

        return True

    @classmethod
    def fetch_any_children(cls, position: int, objects: List) -> List:
        """Fetch the children of this node in the objects list.

        The children are removed from the objects list.

        Args:
            position: The position of the objects when this node was created.
            objects: The list of intermediate parsed objects

        Returns:
            A list of parsed objects.
        """
        children = [value for _, value in objects[position:]]

        del objects[position:]

        return children

    def child(self, qname: str, attrs: Dict, ns_map: Dict, position: int) -> XmlNode:
        """Initialize the next child wildcard node to be queued, when an element starts.

        This entry point is responsible to create the next node type
        with all the necessary information on how to bind the incoming
        input data. Wildcard nodes always return wildcard children.

        Args:
            qname: The element qualified name
            attrs: The element attributes
            ns_map: The element namespace prefix-URI map
            position: The current length of the intermediate objects
        """
        return WildcardNode(
            position=position,
            var=self.var,
            attrs=attrs,
            ns_map=ns_map,
            factory=self.factory,
        )

bind(qname, text, tail, objects)

Bind the parsed data into a generic element.

This entry point is called when a xml element ends and is responsible to parse the current element attributes/text, bind any children objects and initialize new generic element that can fit any xml string.

Parameters:

Name Type Description Default
qname str

The element qualified name

required
text Optional[str]

The element text content

required
tail Optional[str]

The element tail content

required
objects List

The list of intermediate parsed objects

required

Returns:

Type Description
bool

Whether the binding process was successful or not.

Source code in xsdata/formats/dataclass/parsers/nodes/wildcard.py
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
def bind(
    self, qname: str, text: Optional[str], tail: Optional[str], objects: List
) -> bool:
    """Bind the parsed data into a generic element.

    This entry point is called when a xml element ends and is
    responsible to parse the current element attributes/text, bind
    any children objects and initialize new generic element that
    can fit any xml string.

    Args:
        qname: The element qualified name
        text: The element text content
        tail: The element tail content
        objects: The list of intermediate parsed objects

    Returns:
        Whether the binding process was successful or not.
    """
    children = self.fetch_any_children(self.position, objects)
    attributes = ParserUtils.parse_any_attributes(self.attrs, self.ns_map)
    derived = qname != self.var.qname
    text = ParserUtils.normalize_content(text) if children else text
    text = "" if text is None and not self.var.nillable else text
    tail = ParserUtils.normalize_content(tail)

    if tail or attributes or children or self.var.is_wildcard or derived:
        obj = self.factory(
            qname=qname,
            text=text,
            tail=tail,
            attributes=attributes,
            children=children,
        )
        objects.append((self.var.qname, obj))
    else:
        objects.append((self.var.qname, text))

    return True

fetch_any_children(position, objects) classmethod

Fetch the children of this node in the objects list.

The children are removed from the objects list.

Parameters:

Name Type Description Default
position int

The position of the objects when this node was created.

required
objects List

The list of intermediate parsed objects

required

Returns:

Type Description
List

A list of parsed objects.

Source code in xsdata/formats/dataclass/parsers/nodes/wildcard.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
@classmethod
def fetch_any_children(cls, position: int, objects: List) -> List:
    """Fetch the children of this node in the objects list.

    The children are removed from the objects list.

    Args:
        position: The position of the objects when this node was created.
        objects: The list of intermediate parsed objects

    Returns:
        A list of parsed objects.
    """
    children = [value for _, value in objects[position:]]

    del objects[position:]

    return children

child(qname, attrs, ns_map, position)

Initialize the next child wildcard node to be queued, when an element starts.

This entry point is responsible to create the next node type with all the necessary information on how to bind the incoming input data. Wildcard nodes always return wildcard children.

Parameters:

Name Type Description Default
qname str

The element qualified name

required
attrs Dict

The element attributes

required
ns_map Dict

The element namespace prefix-URI map

required
position int

The current length of the intermediate objects

required
Source code in xsdata/formats/dataclass/parsers/nodes/wildcard.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def child(self, qname: str, attrs: Dict, ns_map: Dict, position: int) -> XmlNode:
    """Initialize the next child wildcard node to be queued, when an element starts.

    This entry point is responsible to create the next node type
    with all the necessary information on how to bind the incoming
    input data. Wildcard nodes always return wildcard children.

    Args:
        qname: The element qualified name
        attrs: The element attributes
        ns_map: The element namespace prefix-URI map
        position: The current length of the intermediate objects
    """
    return WildcardNode(
        position=position,
        var=self.var,
        attrs=attrs,
        ns_map=ns_map,
        factory=self.factory,
    )