[XML-SIG] Issue in XMLGenerator and proposed fix

Eric van der Vlist vdv@dyomedea.com
Fri, 14 Jun 2002 20:00:22 +0200


Hi,

XMLGenerator can be confused by unsual (but not illegal IMO) sequences 
of prefix mapping. For instance:

#!/usr/bin/python
from xml.sax.saxutils import XMLGenerator
from xml.sax.xmlreader import AttributesNSImpl

ch = XMLGenerator()
ch.startDocument()
ch.startPrefixMapping("foo", "bar")
ch.endPrefixMapping("foo")
ch.startPrefixMapping("foo", "baz")
ch.startElementNS((None, "foo"), "foo", AttributesNSImpl({},{}))
ch.endElementNS((None, "foo"), "foo")
ch.endDocument()

produces:

<?xml version="1.0" encoding="iso-8859-1"?>
<foo xxmlns:foo="bar" mlns:foo="baz"></foo>

To fix this, one should remove the prefix from the _undeclared_ns_maps 
list when needed:

     def endPrefixMapping(self, prefix):
         self._current_context = self._ns_contexts[-1]
         del self._ns_contexts[-1]
         # start of proposed modification
	i = 0
	for p, u in self._undeclared_ns_maps:
	   if p==prefix:
	      del self._undeclared_ns_maps[i]
	      break
	   i+=1

Eric

-- 
See you in San Diego.
                                http://conferences.oreillynet.com/os2002/
------------------------------------------------------------------------
Eric van der Vlist       http://xmlfr.org            http://dyomedea.com
http://xsltunit.org      http://4xt.org           http://examplotron.org
------------------------------------------------------------------------