import xml.dom import xml.dom.minidom import unittest class test_toxml(unittest.TestCase): def testElementNamespace(self): """ Tests wether namespaces are saved when serializing/ deserializing a document with a namespaced element""" implementation = xml.dom.getDOMImplementation("minidom"); namespace = "http://some.name.space.com" doc = implementation.createDocument(namespace, "root", None) serialized = doc.toxml(); deserialized = xml.dom.minidom.parseString(serialized); self.assertEqual( namespace, deserialized.documentElement.namespaceURI, "writting to text should preserve namespace") self.assertEqual( None, deserialized.documentElement.prefix, "Writting to text should preserve prefix") def testAttributeNamespace(self): """ Tests wether namespaces are saved when serializing/ deserializing a document with a namespaced attribute""" implementation = xml.dom.getDOMImplementation("minidom"); namespace = "http://some.name.space.com" doc = implementation.createDocument(None, "root", None) doc.documentElement.setAttributeNS(namespace, "prefix:attr", "value") serialized = doc.toxml(); # this fails due to unbound namespace deserialized = xml.dom.minidom.parseString(serialized); attrNode = doc.documentElement.getAttributeNodeNS(namespace, "attr"); self.assertTrue(attrNode is not None, "Should find attribute by namespace") self.assertEquals( attrNode.prefix, "prefix", "attributes should preserve prefix") def testAutomaticAttributeNamespace(self): """ Tests wether namespaces are saved when serializing/ deserializing a document with a namespaced attribute with no prefix""" implementation = xml.dom.getDOMImplementation("minidom"); namespace = "http://some.name.space.com" namespace2 = "differentnamespace" doc = implementation.createDocument(None, "root", None) doc.documentElement.setAttributeNS(namespace, "attr", "value1") doc.documentElement.setAttributeNS(namespace2, "attr2", "value2") serialized = doc.toxml(); # this fails due to unbound namespace deserialized = xml.dom.minidom.parseString(serialized); attrNode1 = doc.documentElement.getAttributeNodeNS(namespace, "attr"); self.assertTrue(attrNode1 is not None, "Should find attribute by namespace") attrNode2 = doc.documentElement.getAttributeNodeNS(namespace2, "attr2"); self.assertTrue(attrNode2 is not None, "Should find attribute by namespace") self.assertEquals( attrNode1.value, "value1", "attributes should preserve value") self.assertEquals( attrNode2.value, "value2", # according to spec "attributes should preserve value") def testAttributeConflictNamespace(self): """ Tests wether namespaces are saved when serializing/ deserializing a document with a namespaced attribute with no prefix""" implementation = xml.dom.getDOMImplementation("minidom"); namespace = "http://some.name.space.com" namespace2 = "differentnamespace" doc = implementation.createDocument(None, "root", None) doc.documentElement.setAttributeNS(namespace, "prefix:attr", "value1") doc.documentElement.setAttributeNS(namespace2, "prefix:attr2", "value2") serialized = doc.toxml(); # this fails due to unbound namespace deserialized = xml.dom.minidom.parseString(serialized); attrNode1 = doc.documentElement.getAttributeNodeNS(namespace, "attr"); self.assertTrue(attrNode1 is not None, "Should find attribute by namespace") attrNode2 = doc.documentElement.getAttributeNodeNS(namespace2, "attr2"); self.assertTrue(attrNode2 is not None, "Should find attribute by namespace") self.assertEquals( attrNode1.value, "value1", "attributes should preserve value") self.assertEquals( attrNode2.value, "value2", # according to spec "attributes should preserve value") def testAttributeConflictNamespace2(self): """ Tests wether namespaces are saved when serializing/ deserializing a document with a namespaced attribute with no prefix""" implementation = xml.dom.getDOMImplementation("minidom"); rootnamespace="rootnamespace" namespace = "http://some.name.space.com" namespace2 = "differentnamespace" doc = implementation.createDocument(rootnamespace, "prefix:root", None) doc.documentElement.setAttributeNS(namespace, "prefix:attr", "value1") doc.documentElement.setAttributeNS(namespace2, "prefix:attr2", "value2") serialized = doc.toxml(); # this fails due to unbound namespace deserialized = xml.dom.minidom.parseString(serialized); attrNode1 = doc.documentElement.getAttributeNodeNS(namespace, "attr"); self.assertTrue(attrNode1 is not None, "Should find attribute by namespace") attrNode2 = doc.documentElement.getAttributeNodeNS(namespace2, "attr2"); self.assertTrue(attrNode2 is not None, "Should find attribute by namespace") self.assertEquals( attrNode1.value, "value1", "attributes should preserve value") self.assertEquals( attrNode2.value, "value2", # according to spec "attributes should preserve value") def testAttributeConflictNamespace3(self): """ Tests wether namespaces are saved when serializing/ deserializing a document with a namespaced attribute with no prefix""" implementation = xml.dom.getDOMImplementation("minidom"); rootnamespace="rootnamespace" namespace = "http://some.name.space.com" namespace2 = "differentnamespace" doc = implementation.createDocument(rootnamespace, "prefix:root", None) doc.documentElement.setAttributeNS(rootnamespace, "pref2:attr", "value1") doc.documentElement.setAttributeNS(namespace2, "pref2:attr2", "value2") serialized = doc.toxml(); # this fails due to unbound namespace deserialized = xml.dom.minidom.parseString(serialized); attrNode1 = doc.documentElement.getAttributeNodeNS(rootnamespace, "attr"); self.assertTrue(attrNode1 is not None, "Should find attribute by namespace") attrNode2 = doc.documentElement.getAttributeNodeNS(namespace2, "attr2"); self.assertTrue(attrNode2 is not None, "Should find attribute by namespace") self.assertEquals( attrNode1.value, "value1", "attributes should preserve value") self.assertEquals( attrNode2.value, "value2", # according to spec "attributes should preserve value") def testAttributePrefixResolution(self): """ Tests wether namespaces are saved when serializing/ deserializing a document with a namespaced attribute with no prefix""" implementation = xml.dom.getDOMImplementation("minidom"); rootnamespace="rootnamespace" namespace = "http://some.name.space.com" namespace2 = "differentnamespace" doc = implementation.createDocument(rootnamespace, "prefix:root", None) doc.documentElement.setAttributeNS(rootnamespace, "pref2:attr", "value1") serialized = doc.toxml(); # this fails due to unbound namespace deserialized = xml.dom.minidom.parseString(serialized); attrNode1 = doc.documentElement.getAttributeNodeNS(rootnamespace, "attr"); self.assertTrue(attrNode1 is not None, "Should find attribute by namespace") self.assertEquals( attrNode1.value, "value1", "attributes should preserve value") # according to # http://www.w3.org/TR/DOM-Level-3-Core/namespaces-algorithms.html#normalizeDocumentAlgo # the attribute's prefix should have # changed to the root elements prefix self.assertEquals( attrNode1.prefix, "prefix", "attribute should adopt existing binding") def testNonamespace(self): """ Tests wether namespaces are saved when serializing/ deserializing a document with a namespaced attribute with no prefix""" implementation = xml.dom.getDOMImplementation("minidom"); doc = implementation.createDocument(None, "root", None) serialized = doc.toxml(); # this fails due to unbound namespace deserialized = xml.dom.minidom.parseString(serialized); self.assertEqual( None, deserialized.documentElement.namespaceURI, "Writting to text should preserve prefix") if __name__ == "__main__": unittest.main()