title: XSD: Nested Types vs Global Types
body: <p>When defining XSD you can either choose to define your types as <em>nested</em> types or <em>global</em> types (complexType). </p>
  
  <p>I understand that global types are of much more use when it comes to morphism or reusing of elements. </p>
  
  <p>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.</p>
  
  <p><strong>Nested</strong></p>
  
  <pre><code>&lt;xs:element name="person"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="name"&gt;
                      &lt;xs:complexType&gt;
                          &lt;xs:sequence&gt;
                              &lt;xs:element name="firstname"/&gt;
                              &lt;xs:element name="lastname"/&gt;
                          &lt;/xs:sequence&gt;
                      &lt;/xs:complexType&gt;
                  &lt;/xs:element&gt;
                  &lt;xs:element name="address"&gt;
                      &lt;xs:complexType&gt;
                          &lt;xs:sequence&gt;
                              &lt;xs:element name="street"/&gt;
                              &lt;xs:element name="city"/&gt;
                          &lt;/xs:sequence&gt;
                      &lt;/xs:complexType&gt;
                  &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
  </code></pre>
  
  <p><strong>Global</strong></p>
  
  <pre><code>&lt;xs:element name="person"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="name" type="nameType"/&gt;
              &lt;xs:element name="address" type="addressType"/&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  &lt;xs:complexType name="nameType"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element name="firstname"/&gt;
          &lt;xs:element name="lastname"/&gt;
      &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  &lt;xs:complexType name="addressType"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element name="street"/&gt;
          &lt;xs:element name="city"/&gt;
      &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  </code></pre>
  
  <p>Hence, my question: When are you using nested types instead of making them global?</p>
  
  <p>For anyone interested: My question somehow relates to this question (<a href="https://stackoverflow.com/questions/33746/xml-attribute-vs-xml-element">XML attribute vs element</a>).</p>