Best practices

Figure 749. Nested vs. reference Slide presentation Create comment in forum
<xs:element name="organizer">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="contact"
         minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="firstName"/>
            <xs:element name="lastName"/>
            <xs:element name="email" 
                           minOccurs="0"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<xs:element name="organizer">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="contact"
            minOccurs="0"
            maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<xs:element name="contact">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstName"/>
      <xs:element name="lastName"/>
      <xs:element name="email"
              minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Figure 750. Common pitfalls Slide presentation Create comment in forum
Common pitfalls

Figure 751. Get types fixed! Slide presentation Create comment in forum
<xs:element name="top" />
<!-- Valid instance! -->
<top ...="problem1.xsd">  
  <p>Weird <b>text</b></p>
</top>
<xs:element name="top"
   type="xs:string" />
<!-- Element 'top' is a simple type,
   so it must have no element
   information item [children]. -->
<top ...="problem.xsd">  
  <p>Weird <b>text</b></p>
</top>

Figure 752. Get type references right! Slide presentation Create comment in forum
<xs:element name="top">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="para"/> 
    </xs:sequence>
  </xs:complexType>
</xs:element>
    
<xs:element name="para" 
    type="xs:int"/>
<top ="problem2.xsd">
  <!-- Expected type int -->
  <para>1234</para>

  <!-- Non-expected mixed content -->
  <para><emphasis>Just</emphasis> a string</para>
</top>

The schema author likely intended referring to the subsequent <xs:element name="para" type="xs:int"/> element definition. This unfortunately requires a <xs:element ref="para"/> definition instead.

Even worse: Like in Figure 751, “Get types fixed! ” the unintended anonymous element definition of <para> lacks a type="..." declaration. Thus <para> is of Ur-type allowing even for <emphasis> element children.

The intended element definition of <para>.


Figure 753. References: Mind your types! Slide presentation Create comment in forum