GNOME Bugzilla – Bug 168764
xmlCleanupNsDecl()
Last modified: 2009-08-15 18:40:50 UTC
I would like to see xmlCleanupNsDecl() function and xmlNode.cleanupNsDecl() Python method implemented, which would remove unused namespace declaration from a subtree. After changing a namespace of an element the namespace prefix (or default namespace) declaration is still left on the element, even it is not used any more. It is unneeded and sometime causes real problems — e.g. when default namespace declared on the element is changed to default namespace of parent element the serialized XML would be invalid unless xmlReconcilateNs() (which introduces a new prefix, sill leaving unused declaration) is called. Unneded namespace declaration may be easily removed using C code, but the structures are not directly available from Python API, and even if it was searching for unused declaration in Python would be costly.
> which would remove unused namespace declaration from a subtree. You can't know if a namespace is unused ! <a xmlns:f="http://exampe.org/f" value="f:a"/> namespaces prefixes may be used in text node content, and you cannot make any guess about a namespace being unused ! > I would like to see xmlCleanupNsDecl() function and xmlNode.cleanupNsDecl() > Python method implemented, without signature and proper description of the function behaviour I really can't comment on it, it is hugely undefined in my opinion. > After changing a namespace of an element the namespace prefix (or default > namespace) declaration is still left on the element Hum, depends how you changed the namespace, if you changed the namespace pointer yes. If you changed the namespace pointer, i.e. the definition, then there is no extra namespace. Again this is way too unprecise to be commented in an intelligent way. > Unneded namespace declaration may be easily removed using C code true assuming you know they are uneeeded. And that property can only be decided by the application. In the example above f namespace may be needed to process the value attribute or not, it all depend on the semantic of the instance and that is application specific. At this point you request is not complete and can't be processed as is.. Daniel
> without signature and proper description of the function behaviour I > really can't comment on it, it is hugely undefined in my opinion. Proposed: void xmlCleanupNsDecl()(xmlNodePtr tree); The function would traverse down the tree and find any namespace declarations. For each declaration it would traverse down the subtree under (and including) the element with declaration, to see if the namespace is referenced by any element or attribute there. If it is not, then the declaration would be unlinked and freed. > a xmlns:f="http://exampe.org/f" value="f:a"/> > > namespaces prefixes may be used in text node content, and you cannot > make any guess about a namespace being unused ! [...] > In the example above f namespace may be > needed to process the value attribute or not, it all depend on the > semantic of the instance and that is application specific. By writting "unused" I meant not used in the libxml2 document tree representation.
Here is the mailing list thread describing the problem I encountered which could be solved using such function: http://mail.gnome.org/archives/xml/2005-February/msg00123.html
As I pointed in the example this is extremely dangerous. And from an Jabber/xmpp this is even completely wrong ! Example: Assume you pass an XSLT instance using Jabber, this function would just wipe out for example the extension namespace declarations which are needed for the stylesheets but referenced only as QNames in attribute values ! And XSLt is just one example, there is a lot of specs rquiring QNames in attribute values. The function as you suggest should not be implemented. The part of "guessing" that a namespace is not used because it's not referenced by element or attribute descendant is fundamentally wrong even for the use case you were expecting ! A function removing a *given namespace declaration* on a *given node* might be defined, but it cannot be made fool-proof, at least it forces the application to explicitely specify the namespace minimizing the risks. Daniel
OK, you have convinced me. In fact I need to remove only a known namespace declaration, which is never used the way you mention. You are right -- in many circumstances such function could be harmful. However, a function to remove a given namespace declaration on a given node would be usefull, especially in the Python API, where it cannot be done directly. But that is another feature request. I think this one may be closed. Thanks for explanation.