[XML-SIG] xml namespace prefix and xml.sax.saxutils.XMLGenerator

Martijn Faassen faassen@vet.uu.nl
Tue, 4 Mar 2003 15:10:32 +0100

Hi there,

I'm using xml.sax.saxutils.XMLGenerator to generate XML from sax
events automatically, and I think I've discovered a bug.

The XML standard defines that an attribute called 'xml:lang' may be
defined. The XML namespace standard expands on this in Section 4,
'Using Qualified Names':

Namespace Constraint: Prefix Declared

The namespace prefix, unless it is xml or xmlns, must have been declared in a
namespace declaration attribute in either the start-tag of the element where 
the prefix is used or in an an ancestor element (i.e. an element in whose
content the prefixed markup occurs). The prefix xml is by definition bound to
the namespace name http://www.w3.org/XML/1998/namespace. The prefix xmlns is
used only for namespace bindings and is not itself bound to any namespace name.

The generator works fine for 'xmlns', but it doesn't seem to handle 'xml'
properly. What should be the case if I understand this right is that the
'xml' namespace is always declared (and thus does not get a special
xmlns declaration). The XMLGenerator and perhaps other code does not
recognize the 'xml' prefix however, and barfs over it when generating 

Trying to introduce it by using startPrefixMapping() fails, as then
the generator automatically will add an explicit definition to the document
element (xmlns:xml="http://www.w3.org/XML/1998/namespace"), which shouldn't

The fix seems to be as following. Change line 221 or thereabouts from:

        self._ns_contexts = [{}] # contains uri -> prefix dicts

into this:

        # contains uri -> prefix dicts 
        self._ns_contexts = [{'http://www.w3.org/XML/1998/namespace': 'xml'}]

If this is deemed to be a good fix I can check this one in myself. I wonder
though if there would be any other problems with 'xml:' in sax handlers I'm less
familiar with (reading it in appears to work fine). Anyone?