开发者

Where should I put a URI declaration for a namespace in an XML doc?

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/">
    <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/">
       <head:MyFunctionResponse>
          <body:DataObject xmnls:body="http://mywebsite.com/ns/body/">
              <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/">
       <head:MyFunctionResponse>
          <error:ThrownException xmnls:body="http://mywebsite.com/ns/error/">
              <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开发者_C百科, 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?


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/">
       <MyFunctionResponse>
          <DataObject xmnls="http://mywebsite.com/ns/body/">
              <Parameter>Foo</Parameter>
              <Parameter>Bar</Parameter>
          </DataObject>
       </MyFunctionResponse>
    </Response>
</schema>

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜