def test_clear(self): namespaces = Namespaces() namespaces.add_all({ "b": "bar", "foo": "http://www.w3.org/2001/XMLSchema-instance" }) self.assertEqual(2, len(namespaces.ns_map)) namespaces.clear() self.assertEqual(0, len(namespaces.ns_map))
def test_property_prefixes(self): namespaces = Namespaces() namespaces.add_all({ "b": "bar", None: "http://www.w3.org/2001/XMLSchema", "foo": "http://www.w3.org/2001/XMLSchema-instance", }) self.assertEqual(["b", "xs", "xsi"], namespaces.prefixes) self.assertEqual("b", namespaces.prefix("bar")) self.assertEqual("xs", namespaces.prefix("http://www.w3.org/2001/XMLSchema")) self.assertIsNone(namespaces.prefix("http://xsdata"))
def test_add_all(self): namespaces = Namespaces() namespaces.add_all({ "b": "bar", None: "http://www.w3.org/2001/XMLSchema", "foo": "http://www.w3.org/2001/XMLSchema-instance", }) expected = { "bar": {"b"}, "http://www.w3.org/2001/XMLSchema": {"xs"}, "http://www.w3.org/2001/XMLSchema-instance": {"xsi"}, } self.assertEqual(expected, namespaces.data)
def render_wildcard_node(self, parent: Element, value: Any, var: XmlVar, namespaces: Namespaces): if value.qname: sub_element = SubElement(parent, value.qname) else: sub_element = parent namespaces.add_all(value.ns_map) SerializeUtils.set_text(sub_element, value.text, namespaces) SerializeUtils.set_tail(sub_element, value.tail, namespaces) SerializeUtils.set_attributes(sub_element, value.attributes, namespaces) for child in value.children: self.render_sub_node(sub_element, child, var, namespaces) SerializeUtils.set_nil_attribute(sub_element, var.nillable, namespaces)
def render_wildcard_node(self, parent: Element, value: Any, var: XmlVar, namespaces: Namespaces): """Render a child element for the given parent according to the wildcard field metadata.""" if value.qname: sub_element = SubElement(parent, value.qname) else: sub_element = parent namespaces.add_all(value.ns_map) SerializeUtils.set_text(sub_element, value.text, namespaces) SerializeUtils.set_tail(sub_element, value.tail, namespaces) SerializeUtils.set_attributes(sub_element, value.attributes, namespaces) for child in value.children: self.render_sub_node(sub_element, child, var, namespaces) SerializeUtils.set_nil_attribute(sub_element, var.nillable, namespaces)
def test_property_ns_map(self): namespaces = Namespaces() namespaces.add_all({ "": "xsdata", "foo": "http://www.w3.org/2001/XMLSchema-instance", "b": "bar", }) namespaces.add("bar", "again") namespaces.add("one") namespaces.add("two") expected = { None: "xsdata", "b": "bar", "again": "bar", "ns4": "one", "ns5": "two", "xsi": "http://www.w3.org/2001/XMLSchema-instance", } self.assertEqual(expected, namespaces.ns_map)