XSD Mixed Content


Mixed complex types can contain attributes, elements, and text.


Complex types with mixed content

The XML element "letter" contains text as well as other elements:

<letter>
  Dear Mr.<name>John Smith</name>.
  Your order <orderid>1032</orderid>
  will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>

The following schema declares this "letter" element:

<xs:element name="letter">
  <xs:complexType mixed="true">
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="orderid" type="xs:positiveInteger"/>
      <xs:element name="shipdate" type="xs:date"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Note:For character data to appear between the child elements of "letter", the mixed attribute must be set to "true". The <xs:sequence> tag (name, orderid, and shipdate) means that the defined elements must appear in order inside the "letter" element.

We can also give a name to the complexType element, and let the "letter" element's type attribute refer to this name of the complexType (in this way, multiple elements can reference the same complex type):

<xs:element name="letter" type="lettertype"/>

<xs:complexType name="lettertype" mixed="true">
  <xs:sequence>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="orderid" type="xs:positiveInteger"/>
    <xs:element name="shipdate" type="xs:date"/>
  </xs:sequence>
</xs:complexType>
Other Extensions