The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

XML Namespaces

I'm validating an XML document against its schema, and I'm noticing that only the following two declarations work:

<myPrefix:abc xmlns:myPrefix="http://my.company/schema/">
  <xyz>
    ...
  </xyz>
</myPrefix:abc>

== or ==

<abc xmlns="http://my.company/">
  <xyz xmlns="">
    ...
  </xyz>
</abc>

---------

Since I don't want to use a prefix, then option #2 is what I'm currently using.  I've noticed that Axis-1.0 is using this trick as well when generating a response. What I can't figure out is why the empty default namespace is required.  Why can't I do:

<abc xmlns="http://my.company/">
  <xyz>
    ...
  </xyz>
</abc>

For this last case, I get an error:

Line #2: cvc-complex-type.2.4.a: Invalid content was found starting with element 'xyz'. One of '{"":xyz}' is expected.

Thanks!
Manes
Monday, March 05, 2007
 
 
ehh, sorry. The first namespace is the same as the others. I forgot to change it as I edittd. All are: "http://my.company/"
Manes
Monday, March 05, 2007
 
 
Because your schema defines the xyz element to be in the default namespace.

<abc xmlns="http://my.company/">
  <xyz xmlns="">
    ...
  </xyz>
</abc>

says "put abc (and everything underneath it, unless specified otherwise) in the http://my.company/ namespace, and put xyz (and everything underneath it, unless specified otherwise) in the default namespace".
Inigo
Tuesday, March 06, 2007
 
 
XML namespaces are something that you cannot just figure out through trial and error.  You need to be able to look at a document like this:

  <a xmlns="foo" xmlns:x="bar">
      <b xmlns="" xmlns:x="foo">
        <c/>
        <x:d/>
      </b>
      <x:e/>
      <f xmlns="bat"/>
      <g/>
  </a>

and know what namespace each element is in.  (a, d, and g are in foo, b and c are in the default namespace, e is in bar, f is in bat.)

It's easy to look at (say) a schema and think of all of the namespace information as cruft that you just have to jigger with to get your document to pass validation, but in the long run your life will be made miserable by namespaces unless you understand how they work.
Robert Rossney Send private email
Tuesday, March 06, 2007
 
 
Actually, I figured it out. Everything is actually in the same target namespace, but the schema author mixed qualified and unqualified defaults. If I change the parent and child schemas to have the same defaults (qualified) then it works. If instead I set both to unqualified, it fails as before.

I believe making the change would cause clients who generated stubs against the child schema some pain, so I'll leave it as is for now. I still don't quite grasp it in its entirety, but its starting to make sense. Thanks for the help!

<!-- Referenced schema in SAX validator -->
<xs:schema targetNamespace="http://my.company/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://my.company/" elementFormDefault="qualified" version="1.0" xml:lang="en">
  <!-- Included Types -->
  <xs:include schemaLocation="CommonTypes.xsd"/>
  <xs:element name="abc" type="dataSetType">
    <xs:annotation>
      <xs:documentation>ABC Set</xs:documentation>
    </xs:annotation>
  </xs:element>
</xs:schema>

<!-- Schema of common data-types -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://my.company/" targetNamespace="http://my.company/" version="1.0" xml:lang="en">
...
  <xs:complexType name="dataSetType">
    <xs:annotation>
      <xs:documentation>List of data</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="xyz" type="dataSetType" minOccurs="0" maxOccurs="unbounded">
        <xs:annotation>
          <xs:documentation>Data</xs:documentation>
        </xs:annotation>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
...
</xs:schema>
Manes
Tuesday, March 06, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz