[XML-SIG] Attribute namespace bug

Uche Ogbuji uche.ogbuji@fourthought.com
Tue, 30 Oct 2001 14:02:02 -0700


> On Tue, 30 Oct 2001 13:37:53 -0700, Uche Ogbuji wrote:
> =

> >Whoa!  I'm way behind, and still catching up, but I hope someone stopp=
ed you =

> >from committing this.  You would have introduced a bug because unquali=
fied =

> >attributes are *not* in the default namespace.
> =

> Too late. ;)
> =

> >          for (name, value) in attrs.items():
> > -            name =3D self._current_context[name[0]] + ":" + name[1]
> > +            if name[0] is None:
> > +                name =3D name[1]
> > +            elif self._current_context[name[0]] is None:
> > +                # default namespace
> > +                name =3D name[1]
> > +            else:
> > +                name =3D self._current_context[name[0]] + ":" + name=
[1]
> >              self._out.write(' %s=3D"%s"' % (name, escape(value)))
> =

> But do you agree that it is wrong to always try to echo a namespace pre=
fix, =

> even if an attribute originally has none?
> =

> Maybe just my comment is wrong (s/# default namespace/# no namespace/),=
 or the =

> elif is wrong, but the assumption that "self._current_context.has_key(N=
one)" is =

> always true is also wrong.

You're right that your fix is mostly right, but you didn't quite go far e=
nough.

I've pored over the code a bit, and here's what I'm about to check in.  W=
hat =

do you think?

diff -u -r1.23 saxutils.py
--- xml/sax/saxutils.py	2001/09/27 21:42:28	1.23
+++ xml/sax/saxutils.py	2001/10/30 21:00:45
@@ -155,6 +155,8 @@
     def _outputwrapper(stream,encoding):
         return stream
 =

+GENERATED_PREFIX =3D "genprefix%s"
+
 class XMLGenerator(handler.ContentHandler):
 =

     def __init__(self, out=3DNone, encoding=3D"iso-8859-1"):
@@ -167,6 +169,8 @@
         self._current_context =3D self._ns_contexts[-1]
         self._undeclared_ns_maps =3D []
         self._encoding =3D encoding
+        self._generated_prefix_ctr =3D 0
+        return
 =

     # ContentHandler methods
 =

@@ -214,7 +218,13 @@
                 name =3D name[1]
             elif self._current_context[name[0]] is None:
                 # default namespace
-                name =3D name[1]
+                #If an attribute has a nsuri but not a prefix, we must
+                #create a prefix and add a nsdecl
+                prefix =3D GENERATED_PREFIX % self._generated_prefix_ctr=

+                self._generated_prefix_ctr =3D self._generated_prefix_ct=
r + 1
+                name =3D prefix + ':' + name[1]
+                self._out.write(' xmlns:%s=3D%s' % (prefix, name[0]))
+                self._current_context[name[0]] =3D prefix
             else:
                 name =3D self._current_context[name[0]] + ":" + name[1]
             self._out.write(' %s=3D%s' % (name, quoteattr(value)))


-- =

Uche Ogbuji                               Principal Consultant
uche.ogbuji@fourthought.com               +1 303 583 9900 x 101
Fourthought, Inc.                         http://Fourthought.com =

4735 East Walnut St, Boulder, CO 80301-2537, USA
XML strategy, XML tools (http://4Suite.org), knowledge management