Q. The elements immediately under the top level element in XML serialized by Axis all have an attribute 'xmlns=""'. Why are they there?

From Anne Thomas Manes (posting on the axis-user list):

xmlns="" is the proper way to declare no default namespace, and its the only way to override a previously declared default namespace.

Let me demonstrate with an example. Let's say that you have this schema:

<s:schema targetNamespace="some-uri" elementFormDefault="unqualified"
   xmlns:s="http://www.w3.org/2001/XMLSchema">
  <s:element name="foobar">
    <s:complexType>
      <s:sequence>
        <s:element name="foo" type="s:string"/>
        <s:element name="bar" type="s:string"/>
      </s:sequence>
    </s:complexType>
  </s:element>
</s:schema>

In this schema, "foobar" is in the "some-uri" namespace, but "foo" and "bar" are local elements to "foobar" and they belong to no namespace. Therefore in a valid document instance, these elements must be unqualified.

A valid instance of this schema might look like this:

<tns:foobar xmlns:tns="some-uri">
  <foo>one</foo>
  <bar>two</bar>
</tns:foobar>

But another valid instance might look like this:

<foobar xmlns="some-uri">
  <foo xmlns="">one</foo>
  <bar xmlns="">two</bar>
</foobar>

Since "foo" and "bar" are in no namespace, you must override the default namespace in each element.

According to XML Schema, if you don't specify elementFormDefault="qualified", then all local elements default to unqualified -- but .NET has a bug, and it defaults to qualified. Therefore to fix your problem, add elementFormDefault="qualified" to your schema.

Axis sets the default namespace to null because your schema defines the child elements as unqualified. If it didn't set the default namespace to null, then the message would not match the schema.

If you add elementFormDefault="qualified" to your schema element, then the child elements will be defined as qualified.

For the curious, the relevant bit of the 2001 RFC is: http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#declare-element

Q. Uh, what's the short answer?

Put attribute elementFormDefault="qualified" on your <schema> tag. The sub elements of the elements you declare aren't in any namespace unless you force them to be.

FrontPage/Axis/OnlyTopLevelElementHasNamespace (last edited 2009-09-20 22:48:47 by localhost)