Skip to content

primitive

xsdata.formats.dataclass.parsers.nodes.primitive

PrimitiveNode

Bases: XmlNode

XmlNode for text elements with simple type values.

Parameters:

Name Type Description Default
var XmlVar

The xml var instance

required
ns_map Dict

The element namespace prefix-URI map

required
mixed bool

Specifies if this node supports mixed content

required
Source code in xsdata/formats/dataclass/parsers/nodes/primitive.py
 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
class PrimitiveNode(XmlNode):
    """XmlNode for text elements with simple type values.

    Args:
        var: The xml var instance
        ns_map: The element namespace prefix-URI map
        mixed: Specifies if this node supports mixed content
    """

    __slots__ = "var", "ns_map"

    def __init__(self, var: XmlVar, ns_map: Dict, mixed: bool):
        self.var = var
        self.ns_map = ns_map
        self.mixed = mixed

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

        This entry point is called when a xml element ends and is
        responsible to parse the current element attributes/text/tail
        content.

        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.
        """
        obj = ParserUtils.parse_value(
            value=text,
            types=self.var.types,
            default=self.var.default,
            ns_map=self.ns_map,
            tokens_factory=self.var.tokens_factory,
            format=self.var.format,
        )

        if obj is None and not self.var.nillable:
            obj = b"" if bytes in self.var.types else ""

        objects.append((qname, obj))

        if self.mixed:
            tail = ParserUtils.normalize_content(tail)
            if tail:
                objects.append((None, tail))

        return True

    def child(self, qname: str, attrs: Dict, ns_map: Dict, position: int) -> XmlNode:
        """Raise an exception if there is a child element inside this node."""
        raise XmlContextError("Primitive node doesn't support child nodes!")

bind(qname, text, tail, objects)

Bind the parsed data into an object for the ending element.

This entry point is called when a xml element ends and is responsible to parse the current element attributes/text/tail content.

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/primitive.py
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
def bind(
    self,
    qname: str,
    text: Optional[str],
    tail: Optional[str],
    objects: List,
) -> bool:
    """Bind the parsed data into an object for the ending element.

    This entry point is called when a xml element ends and is
    responsible to parse the current element attributes/text/tail
    content.

    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.
    """
    obj = ParserUtils.parse_value(
        value=text,
        types=self.var.types,
        default=self.var.default,
        ns_map=self.ns_map,
        tokens_factory=self.var.tokens_factory,
        format=self.var.format,
    )

    if obj is None and not self.var.nillable:
        obj = b"" if bytes in self.var.types else ""

    objects.append((qname, obj))

    if self.mixed:
        tail = ParserUtils.normalize_content(tail)
        if tail:
            objects.append((None, tail))

    return True

child(qname, attrs, ns_map, position)

Raise an exception if there is a child element inside this node.

Source code in xsdata/formats/dataclass/parsers/nodes/primitive.py
68
69
70
def child(self, qname: str, attrs: Dict, ns_map: Dict, position: int) -> XmlNode:
    """Raise an exception if there is a child element inside this node."""
    raise XmlContextError("Primitive node doesn't support child nodes!")