XSD Mixed Content

The attributes, elements, and text can be included in a mixed complex type element.

Complex Types with Mixed Content:

Example:

<letter>
 Dear Ms. <name>Vigesa</name>.
 You ranked <rank>10</rank>
 in the exam conducted on <examdate>2020-06-10</examdate>.
</letter>

Schema:

<xs:element name="letter">
  <xs:complextype mixed="true">
    <xs:sequence>
      <xs:element name="name" type="xs:string"></xs:element>
      <xs:element name="rank" type="xs:positiveInteger"></xs:element>
      <xs:element name="examdate" type="xs:date"></xs:element>
    </xs:sequence>
  </xs:complextype>
</xs:element>

Explanation:

In the above example, we are defining an XML element, “letter”, that contains both text and other elements. We have also created a schema to declare the “letter” element. The mixed attribute must be set to “true”, for enabling the character data to appear between the child-elements of “letter”. For the elements defined (name, orderid and shipdate) to appear in an order inside a “letter” element, we are using the <xs:sequence> tag.

Example 2:

<xs:element name="letter" type="lettertype"></xs:element>
 
<xs:complextype name="lettertype" mixed="true">
  <xs:sequence>
    <xs:element name="name" type="xs:string"></xs:element>
    <xs:element name="rank" type="xs:positiveInteger"></xs:element>
    <xs:element name="examdate" type="xs:date"></xs:element>
  </xs:sequence>
</xs:complextype>

Explanation:

In the above example, we are giving the complexType element a name. Here, the “letter” element has a type attribute that refers to the name of the complexType. Several elements can refer to the same complex type, by using this method.

Please Share