[4suite] Namespace bug in 4DOM (Pretty)Print ?

Uche Ogbuji uche.ogbuji at fourthought.com
Tue Sep 19 10:24:52 MDT 2000


Alexandre Fayolle wrote:

> I have a problem in (the old version of) Print / PrettyPrint regarding
> attributes and namespaces:

> >>> from xml.dom.ext.reader import Sax2
> >>> from xml.dom.ext import PrettyPrint
> >>> d=Sax2.FromXml('<doc/>')
> >>> e = d.createElement('elt')
> >>> d.documentElement.appendChild(e)
> >>> e.setAttributeNS('http://logilab','att','value1')

Oops.  We should throw an exception here.  This is a NAMESPACE_ERR
according to DOM L2.  You won't be able to do this in the coming
release.

> >>> PrettyPrint(d)
> <doc>
>   <elt xmlns = 'http://logilab' att='value1'/>
> 
> this causes a default namespace to be used for <elt> and no namespace for
> 'att'. Notice that no end tag for doc was printed.

I think the lack of end tag might be an illusion.  Because the printer
does not append a carriage return, the Python prompt comes right after
the closing doc.  In some cases I've seen Python interpreter then
overwrite this with a new prompt so that the old one (and the preceding
end tag) can't be seen.  In any case, I tried with both 0.10.2 code and
the pending release code and could not reproduce the lack of end tag.

BTW, should we have the printers append a carriage return to the output
to avoid such confusion?  I think maybe it's a good idea...

As for your main question, if you add a prefix to att, it should work.


> >>> e = d.createElement('elt')
> >>> d.documentElement.appendChild(e)
> <Element Node at 136157472: Name = 'elt' with 0 attributes and
> 0 children>
> >>> e.setAttributeNS('http://logilab','toto:att','value1')
> >>> PrettyPrint(d)
> <doc>
>   <elt xmlns = 'http://logilab' att='value1'/>
>   <elt xmlns:toto = 'http://logilab' toto:att='value1'/>
> </doc>
> 
> If I use a qualified name, things go better.
> 
> >>> print e.attributes
> <NamedNodeMap at 136099048: {('http://logilab', 'att'): <Attrib
> ute Node at 136141952: Name = "toto:att", Value = "value1">}>
> >>> print d.documentElement.firstChild.attributes
> <NamedNodeMap at 135780976: {('http://logilab', 'att'): <Attrib
> ute Node at 136041960: Name = "att", Value = "value1">}>
> 
> In both cases the attributes look fine, so I think this a purely a Print
> bug. However, since I use this to save my documents to file, and try to
> read the files using Sax2.FromXml afterwards, it is really an important
> issue.

Hmm.  Considering that you won't be able to do this for long, we should
discuss why you need to use mal-formed namespaces?


-- 
Uche Ogbuji                               Principal Consultant
uche.ogbuji at fourthought.com               +1 303 583 9900 x 101
Fourthought, Inc.                         http://Fourthought.com 
4735 East Walnut St, Ste. C, Boulder, CO 80301-2537, USA
Software-engineering, knowledge-management, XML, CORBA, Linux, Python



More information about the 4suite mailing list