def test_attribute_object(): document = Document("<root/>") node = document.root attributes = node.attributes attributes["ham"] = "spam" assert str(node) == '<root ham="spam"/>' attribute = node["ham"] assert attribute.namespace is None assert attribute.universal_name == "ham" attribute.namespace = "kitchen.sink" assert str(node) == '<root xmlns:ns0="kitchen.sink" ns0:ham="spam"/>' assert attribute.universal_name == "{kitchen.sink}ham" attribute.local_name = "clam" assert str(node) == '<root xmlns:ns0="kitchen.sink" ns0:clam="spam"/>' assert attribute.universal_name == "{kitchen.sink}clam" attribute.namespace = None document.cleanup_namespaces() assert str(node) == '<root clam="spam"/>' assert attribute.universal_name == "clam" assert attribute.value == "spam" attribute.value = "sham" assert str(node) == '<root clam="sham"/>' with pytest.raises(TypeError): attribute.value = None attributes.pop("clam") with pytest.raises(InvalidOperation): attribute.value with pytest.raises(InvalidOperation): attribute.value = "obsolete"
def test_cleanup_namespaces(): document = Document( '<root xmlns="D" xmlns:y="Y"><x:a xmlns:x="X"/></root>') document.cleanup_namespaces(retain_prefixes=("y", )) assert str( document) == '<root xmlns="D" xmlns:y="Y"><x:a xmlns:x="X"/></root>' document.cleanup_namespaces() assert str(document) == '<root xmlns="D"><x:a xmlns:x="X"/></root>' document.cleanup_namespaces(namespaces={"x": "X"}) assert str(document) == '<root xmlns="D" xmlns:x="X"><x:a/></root>'