Question

A mixed complex type element can have text as well between its children. Does the children inherit this mixed feature as well? In other words, if the children are not of a mixed type, can they have text between their children as well?

Was it helpful?

Solution

No, mixed is not inherited by children elements.

Given this XSD:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType mixed="true">
      <xs:sequence>
        <xs:element name="child" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="grandchild" minOccurs="0" maxOccurs="unbounded">
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

This XML document instance:

<root>
  text1
  <child>
    text2
    <grandchild/>
  </child>
</root>

Would be invalid because the mixed content model of root is not passed along to the content model of child.

A validating parser would issue an error such as the following:

Element 'child' cannot have character [children], because the type's content type is element-only.

See also the similar but different question Is mixed inherited when a complexType is extended?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top