[4suite] DomWriter problem

Uche Ogbuji uche.ogbuji at fourthought.com
Thu Sep 21 10:00:32 MDT 2000


Alexandre Fayolle wrote:
> 
> Yes, I know who wrote this piece of code ;o)
> 
> Let me tell him: it's broken!
> 
> It looks to me as if the bug fix with setAttributeNS of the other day
> (remember : passing a non-empty namespace with a non prefixed attribute
> name) showed a bug in DomWriter, but I have problem understanding what's
> wrong.
> 
> Let's take this transformation:
> <?xml version="1.0"?>
> <xsl:transform
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
> <xsl:template match="/">
>   <foo/>
> </xsl:template>
> </xsl:transform>
> 
> if you process any dom tree with this transformation, using DomWriter,
> you'll get a NAMESPACE_ERR:
> 
>   File "/usr/lib/python1.5/site-packages/xml/xslt/LiteralElement.py", line
> 225, in instantiate
>     processor.writers[-1].startElement(self._nodeName, self._uri,
> self._outputNss)
>   File "/usr/lib/python1.5/site-packages/xml/xslt/DomWriter.py", line 48,
> in startElement
>     new_element.setAttributeNS( extraNss[''],'xmlns','')
>   File "/usr/lib/python1.5/site-packages/xml/dom/Element.py", line 265, in
> setAttributeNS
>     att = self.ownerDocument.createAttributeNS(namespaceURI,
> qualifiedName)
>   File "/usr/lib/python1.5/site-packages/xml/dom/Document.py", line 391,
> in createAttributeNS
>     raise DOMException(NAMESPACE_ERR)
> 
> The problem is on this line:
> new_element.setAttributeNS( extraNss[''],'xmlns','')
> 
> the new element is 'foo' and at this point, extraNss is {'': 'xsl'}, so
> we have an non empty namespace-uri. I have the feeling that I completely
> misunderstood what this parameter stands for when I wrote DowWriter. Could
> someone enlighten me?

Ah, mon ami, we let you down.

When you submitted DomWriter.py, it was correct according to 4DOM at
that time.  The problem is that 4DOM was not correct according to the
very latest DOM.  The May DOM CR changed the entire mechanism for
namespace declaration attributes.

So we fixed 4DOM but we forgot to fix DOMWriter.

Your code should look something like this:

        from xml.dom import XMLNS_NAMESPACE
        for prefix in extraNss.keys():
            if prefix:
                new_element.setAttributeNS(XMLNS_NAMESPACE,
                                           'xmlns:'+prefix,
                                           extraNss[prefix])
            else:
                new_element.setAttributeNS(XMLNS_NAMESPACE,
                                           'xmlns',
                                           extraNss[''])

Please confirm that this works for you.  Also, do you mind putting
together a small test suite for DOMWriter that we can incorporate into
our code?  This will make sure we don't overlook it again.

I would do it, but it looks like I'm back to packaging today (look out
for 4Suite 0.9.0.1 -- sigh)

Thanks.

-- 
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