def bind(self, qname: str, text: NoneStr, tail: NoneStr, objects: List) -> bool: obj: Any = None if self.has_children: obj = AnyElement( qname=qname, text=ParserUtils.normalize_content(text), tail=ParserUtils.normalize_content(tail), attributes=ParserUtils.parse_any_attributes(self.attrs, self.ns_map), children=ParserUtils.fetch_any_children(self.position, objects), ) objects.append((self.var.qname, obj)) else: var = self.var ns_map = self.ns_map datatype = ParserUtils.data_type(self.attrs, self.ns_map) obj = ParserUtils.parse_value(text, [datatype.type], var.default, ns_map) if var.derived: obj = DerivedElement(qname=qname, value=obj) objects.append((qname, obj)) if self.mixed: tail = ParserUtils.normalize_content(tail) if tail: objects.append((None, tail)) return True
def test_parse_any_attributes(self): attrs = {QNames.XSI_TYPE: "xsd:string", "a": "b"} ns_map = {"xsi": Namespace.XSI.uri, "xsd": Namespace.XS.uri} result = ParserUtils.parse_any_attributes(attrs, ns_map) expected = { QNames.XSI_TYPE: "{http://www.w3.org/2001/XMLSchema}string", "a": "b", } self.assertEqual(expected, result)
def bind(self, qname: str, text: NoneStr, tail: NoneStr, objects: List) -> bool: obj = AnyElement( qname=qname, text=ParserUtils.normalize_content(text), tail=ParserUtils.normalize_content(tail), attributes=ParserUtils.parse_any_attributes(self.attrs, self.ns_map), children=ParserUtils.fetch_any_children(self.position, objects), ) objects.append((self.var.qname, obj)) return True
def test_parse_any_attributes(self): element = Element("foo", nsmap=dict(foo="bar")) element.set("a", QName("bar", "val")) element.set("b", "2") element.set("c", "what:3") actual = ParserUtils.parse_any_attributes(element) expected = { QName("a"): QName("bar", "val"), QName("b"): "2", QName("c"): "what:3", } self.assertEqual("foo:val", element.attrib["a"]) self.assertEqual(expected, actual)
def bind(self, qname: str, text: NoneStr, tail: NoneStr, objects: List) -> bool: children = ParserUtils.fetch_any_children(self.position, objects) attributes = ParserUtils.parse_any_attributes(self.attrs, self.ns_map) derived = self.var.derived or 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.wildcard or derived: obj = AnyElement( 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