This is not a bug. So please do not commit this change!

The problem is that you're using the DOM APIs incorrectly.
If you start creating attributes that look like qualified names,
then you'll confuse the writer (and lots of other things). If you
want to create a namespaced attribute, then there's a right way
to do it - you are doing it the wrong way. The key point is
that xmlns:s is *not* an attribute of the {testing}test element.

Sanjiva.

----- Original Message -----
From: "James M Snell" <[EMAIL PROTECTED]>
To: "Matthew Duftler" <[EMAIL PROTECTED]>; "Sam Ruby" <[EMAIL PROTECTED]>;
<[EMAIL PROTECTED]>
Sent: Friday, September 28, 2001 2:53 AM
Subject: Bug with DOM2Writer


> Package: org.apache.soap.util.xml.DOM2Writer
>
> Problem: In some cases, the DOM2Writer will output duplicate XML namespace
> declarations on a single element, thereby making the XML output invalid.
>
> Example, let's say I want to create the following XML output:
>
> <s:test xmlns:s="testing>
>    <hello />
> </s:test>
>
> To do so, I use the following code.
>
> Document doc = ... create document
> Element test = doc.createElementNS("testing", "test");
> test.setPrefix("s");
> test.setAttribute("xmlns:s", "testing");
> Element hello = doc.createElementNS("testing", "hello");
> test.appendChild(hello);
> doc.appendChild(test);
>
> Run this through the DOM2Writer, and the output is:
>
> <s:test xmlns:s="testing" xmlns:s="testing">
>   <hello />
> </s:test>
>
> Notice the duplicate xmlns:s declarations.
>
> This is caused by the fact that the xmlns:s declaration is set as an
> attribute on the element.  DOM2Writer checks to see if the element
> namespace and prefix have been declared before serializing all of the
> attributes.  In this case, if the "s" prefix for the "testing" namespace
> has not been declared, the xmlns:s="testing" is added to the XML output.
> Then, however, the attributes are looped through and serialized, causing a
> duplicate declaration to be printed.
>
> Why am I adding the test.setAttribute("xmlns:s", "testing")?  Because
> that's about the only ways to declare namespaces in the DOM API.  I could
> get around this problem by simply removing the
> test.setAttribute("xmlns:s", "testing") line, but unfortunately, I don't
> have access to the code that creates the real DOM document I'm trying to
> work with (the above is just an example).  So the setAttribute line
> remains and we have to work around the problem within the DOM2Writer.
>
> Proposed Solution:
>
> When looping through the attributes, check to see if the attribute is an
> XML namespace declaration, and if so, whether or not the namespace has
> already been declared.  If so, skip it and move on.
>
> The proposed modified DOM2Writer is attached.  Can somebody please review
> to make sure that my head is screwed on straight and I didn't overlook
> something before I commit it.
>
>
>
> - James Snell
>      Software Engineer, Internet Emerging Technologies, IBM
>      James M Snell/Fresno/IBM - [EMAIL PROTECTED]
> These things I have spoken to you, so that in Me you may have peace.
> In the world you have tribulation, but take courage; I have overcome the
> world.
> - John 16:33

Reply via email to