tags:

views:

960

answers:

1

When defining XSD you can either choose to define your types as nested types or global types (complexType).

I understand that global types are of much more use when it comes to morphism or reusing of elements.

However, if you have a big data model you would have to define for each level a global complexType and then create an element that references the global type.

Nested

<xs:element name="person">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="name">
        <xs:complexType>
         <xs:sequence>
          <xs:element name="firstname"/>
          <xs:element name="lastname"/>
         </xs:sequence>
        </xs:complexType>
       </xs:element>
       <xs:element name="address">
        <xs:complexType>
         <xs:sequence>
          <xs:element name="street"/>
          <xs:element name="city"/>
         </xs:sequence>
        </xs:complexType>
       </xs:element>
      </xs:sequence>
     </xs:complexType>

Global

<xs:element name="person">
 <xs:complexType>
  <xs:sequence>
   <xs:element name="name" type="nameType"/>
   <xs:element name="address" type="addressType"/>
  </xs:sequence>
 </xs:complexType>
</xs:element>
<xs:complexType name="nameType">
 <xs:sequence>
  <xs:element name="firstname"/>
  <xs:element name="lastname"/>
 </xs:sequence>
</xs:complexType>
<xs:complexType name="addressType">
 <xs:sequence>
  <xs:element name="street"/>
  <xs:element name="city"/>
 </xs:sequence>
</xs:complexType>

Hence, my question: When are you using nested types instead of making them global?

For anyone interested: My question somehow relates to this question (XML attribute vs element).

+2  A: 

In the example given, there's no real difference between the two - and no significant advantages or disadvantages to either.

However, in larger schemas things can get very untidy and difficult to manage when the practice for choosing nested over global isn't clearly defined.

The obvious reasons for using global types (primarily reuse, also nesting) tend to dictate - in general I prefer one mode or the other. I.e. if you're reusing some complexTypes but not others, make them all global. If you're not reusing anything, make them all nested.

The exception to this (and this is something I've come across frequently) is if the definition of the types make up the bulk of the complexity (!) of your schema, and the their containment is relatively simple. In this case, regardless of whether they're reused, I'd recommend making them global as it's far easier to restructure/reorder your document when you don't have to wade through massive complexType definitions. They're also theoretically more portable.

There are also cases where you can't acheive certain document structures with nested types - an example of this is using two complexTypes in a sequence that can contain 0 to unbounded instances of each type, in any mixed order. This isn't possible with nested types, but it is with referenced global types.

Gareth Jenkins