Re: [xml] Invalid namespace definitions after namespace change


Jacek Konieczny wrote:
On Mon, Feb 28, 2005 at 11:45:34AM +0100, Kasimier Buchcik wrote:

Jacek Konieczny wrote:


I have a big problem with libxml2 python bindings. The problem also
occurs in the C API, but it is easily fixable there (so it can be just
called a 'feature').

Here is the test case (python code):

import libxml2
doc=libxml2.parseDoc("<a xmlns='http://a/'><b xmlns='http://b/'/></a>")
a = doc.getRootElement()
b = a.children
print `doc.serialize()`
print "Ns of <b/>: %r" % (b.ns().content,)
print `doc.serialize()`
print "Ns of <b/>: %r" % (b.ns().content,)

The output is:
'<?xml version="1.0"?>\n<a xmlns="http://a/";><b xmlns="http://b/"/></a>\n'
Ns of <b/>: 'http://b/'
'<?xml version="1.0"?>\n<a xmlns="http://a/";><b xmlns="http://b/"/></a>\n'
Ns of <b/>: 'http://a/'


The biggest problem is, that I cannot fix that using only the Python
API. xmlNode.reconciliateNs() does nothing about that.

There was a bug in xmlSearchNsByHref, which caused xmlReconciliateNs
to fail in this scenario. Fixed in CVS (tree.c 1.339).

After applying the patch on libxml2-2.6.17 the output is:

'<?xml version="1.0"?>\n<a xmlns="http://a/";><b xmlns="http://b/"/></a>\n'
Ns of <b/>: 'http://b/'
'<?xml version="1.0"?>\n<a xmlns="http://a/";><default:b xmlns="http://b/"; xmlns:default="http://a/"/></a>\n'
Ns of <b/>: 'http://a/'

That is a correct XML, but still does not satisfy me. A prefix has been
added to <b/> and the unneccessary namespace declaration is still there.

Yes, xmlReconciliateNs does not remove unreferenced namespace
declarations, since it cannot know if those declarations are needed
for following processing or not; it goes the save way in preserving
them. I recommend implementing a "xmlCleanupNsDecl" function, removing
unreferenced namespaces from a given branch.
Adding generated prefixes is OK - take a look at the namespace
normalization algorithm of the W3C [1]; preserving prefixes is not
always possible if serializing, so you should not rely on it.

For my needs that is still unacceptable. I implement an XMPP
implementation and for backward-compatibility with many Jabber clients
I need to use default namespaces (no prefixes) in some places.

I see, OK so you need total control of the declarations.

I think a function to remove (or override) namespace declaration from
a node is what I need and I will request it via Bugzilla.





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]