views:

13

answers:

1

I am currently building an XML schema, with a head, a body, and potentially errors. I am dividing those three parts into different namespaces, so I'm wondering where and when I should put the URI namespace declarations in.

For example, should it be:

<head:schema xmlns:head="http://mywebsite.com/ns/head/" xmnls:body="http://mywebsite.com/ns/body/" xmlns:error="http://mywebsite.com/ns/error/"&gt;
    <head:Response>
       <head:MyFunctionResponse>
          <body:DataObject>
              <body:Parameter>Foo</body:Parameter>
              <body:Parameter>Bar</body:Parameter>
          </body:DataObject>
       </head:MyFunctionResponse>
    </head:Response>
</head:schema>

So despite there being no "error" namespace in this particular document, it is still defined, so that when the "body" namespace is replaced by an "error" namespace, <schema> stays the same.

Or should it be more like this?

<head:schema>
    <head:Response xmlns:head="http://mywebsite.com/ns/head/"&gt;
       <head:MyFunctionResponse>
          <body:DataObject xmnls:body="http://mywebsite.com/ns/body/"&gt;
              <body:Parameter>Foo</body:Parameter>
              <body:Parameter>Bar</body:Parameter>
          </body:DataObject>
       </head:MyFunctionResponse>
    </head:Response>
</head:schema>

OR

<head:schema>
    <head:Response xmlns:head="http://mywebsite.com/ns/head/"&gt;
       <head:MyFunctionResponse>
          <error:ThrownException xmnls:body="http://mywebsite.com/ns/error/"&gt;
              <error:Id>21</error:Id>
              <error:Message>Something went wrong.</error:Message>
          </error:ThrownException>
       </head:MyFunctionResponse>
    </head:Response>
</head:schema>

where the namespace is only declared just as it is needed.

Or is this a question of style, opinion, or preference. Like "Window seat, or aisle seat?".

If it's not style, opinion, or preference, what are the guiding ideas behind the placement of the namespaces?

+1  A: 

Put them all at the top. You aren't saving anything by putting them closer to their use.

Or, put them where they are used, and then make them the default namespace so you can save a bunch of clutter:

<schema>
    <Response xmlns="http://mywebsite.com/ns/head/"&gt;
       <MyFunctionResponse>
          <DataObject xmnls="http://mywebsite.com/ns/body/"&gt;
              <Parameter>Foo</Parameter>
              <Parameter>Bar</Parameter>
          </DataObject>
       </MyFunctionResponse>
    </Response>
</schema>

BTW: It's odd that your root element (schema) is un-namespaced.

Ned Batchelder
So when you set a namespace, unless otherwise defined, all children are set with the namespace, or does it work for all futher siblings as well? And this was something put together for the purposes of demonstration so I'll change the root to be namespaced. :-)
Drew
Namespaces apply to the element they're defined on, and all children of that element. In fact, now your second samples are incorrect, because they have a head:schema root element, but the head namespace isn't defined until the Response element.
Ned Batchelder