def set_attribute(element: Element, key: Any, value: Any, namespaces: Namespaces): if key == QNames.XSI_NIL and (element.text or len(element) > 0): return if isinstance(key, QName): namespaces.add(key.namespace) value = to_xml(value, namespaces) if not value: return element.set(key, value)
def set_tail(element: Element, value: Any, namespaces: Namespaces): """Set element tail optional content from the given value.""" value = to_xml(value, namespaces) if isinstance(value, str) and len(value) == 0: value = None element.tail = to_xml(value, namespaces)
def test_to_xml(self): self.assertEqual(None, to_xml(None)) self.assertEqual("1", to_xml(1)) self.assertEqual("1.5", to_xml(1.5)) self.assertEqual("true", to_xml(True)) self.assertEqual("false", to_xml(False)) self.assertEqual("optional", to_xml(UseType.OPTIONAL)) self.assertEqual("INF", to_xml(float("inf"))) self.assertEqual("INF", to_xml(float("+inf"))) self.assertEqual("-INF", to_xml(float("-inf"))) self.assertEqual("NaN", to_xml(float("nan"))) self.assertEqual("INF", to_xml(Decimal("inf"))) self.assertEqual("INF", to_xml(Decimal("+inf"))) self.assertEqual("-INF", to_xml(Decimal("-inf"))) self.assertEqual("8.77683E-8", to_xml(Decimal("8.77683E-8"))) self.assertEqual("8.77683E-08", to_xml(float("8.77683E-8"))) self.assertEqual("a", to_xml(QName("a"))) self.assertEqual("a", to_xml(QName("a"))) self.assertEqual("{a}b", to_xml(QName("a", "b"))) self.assertEqual("1 2", to_xml([1, 2])) self.assertEqual("INF optional", to_xml([float("inf"), UseType.OPTIONAL])) namespaces = Namespaces() namespaces.add("a", "aa") self.assertEqual("aa:b", to_xml(QName("a", "b"), namespaces)) self.assertEqual("b", to_xml(QName("b"), namespaces)) with self.assertRaises(ConverterError): to_xml(BookForm())
def test_to_xml(self): self.assertEqual(None, to_xml(None)) self.assertEqual("1", to_xml(1)) self.assertEqual("1.5", to_xml(1.5)) self.assertEqual("true", to_xml(True)) self.assertEqual("false", to_xml(False)) self.assertEqual("optional", to_xml(UseType.OPTIONAL)) self.assertEqual("INF", to_xml(float("inf"))) self.assertEqual("INF", to_xml(float("+inf"))) self.assertEqual("-INF", to_xml(float("-inf"))) self.assertEqual("NaN", to_xml(float("nan"))) self.assertEqual("INF", to_xml(Decimal("inf"))) self.assertEqual("INF", to_xml(Decimal("+inf"))) self.assertEqual("-INF", to_xml(Decimal("-inf"))) self.assertEqual("8.77683E-8", to_xml(Decimal("8.77683E-8"))) self.assertEqual("8.77683E-08", to_xml(float("8.77683E-8"))) self.assertEqual(QName("a"), to_xml(QName("a"))) with self.assertRaises(ConverterError): to_xml(BookForm())
def set_tail(element: Element, value: Any, namespaces: Namespaces): value = to_xml(value, namespaces) if isinstance(value, str) and len(value) == 0: value = None element.tail = to_xml(value, namespaces)
def set_tail(cls, parent: Element, value: Any): parent.tail = to_xml(value)
def set_text(cls, parent: Element, value: Any): value = to_xml(value) if isinstance(value, str) and len(value) == 0: value = None parent.text = value
def set_attribute(cls, parent: Element, key: Any, value: Any): if key != QNames.XSI_NIL or (not parent.text and len(parent) == 0): parent.set(to_xml(key), to_xml(value))