Workshops --> XML --> Demo --> schema16.html
SLIS@IU logo

L595 XML@IU logo

Creating Schemas

Complex types

Declaring complex type elements with mixed content

Note that a mixed-content complex type can have elements, attributes, and text

The generic form is:

<xsd:element name="element_name">
    <xsd:complexType mixed="true">
        <xsd:element name="name" type="dataType" />
        <xsd:element name="other_name" type="dataType" />
        <xsd:attribute name="name" type="someValue" />
    </xsd:complexType>
</xsd:element>

The attribute mixed="true" indicates that this complex type can have different types of content

This is a valid element:

<xsd:element name="alien_probe">
    <xsd:complexType mixed="true">
        <xsd:element name="shape" type="xsd:string" />
        <xsd:element name="composition" type="xsd:string" />
        <xsd:attribute name="size" type="xsd:string" />
    </xsd:complexType>
</xsd:element>

Here is how the element appears in the XML file

<alien_probe size="120cm">
    The probe was shaped like a shaped like a <shape>baseball bat</shape> and looked it was made out of <composition>fruitcake</composition>
</alien_probe>

This can also be done as a custom complex type

The generic form is:

<xsd:complexType name="nameType" mixed="true">
    <xsd:element name="name" type="dataType" />
    <xsd:element name="other_name" />
    <xsd:attribute name="name" type="someValue" />
</xsd:complexType>

This is a valid element:

<xsd:complexType name="alien_probeType" minOccurs="2" mixed="true">
    <xsd:element name="shape" type="xsd:string">
    <xsd:element name="composition" />
     <xsd:attribute name="size" type="xsd:string" />
</xsd:complexType>

This is how the element is called in the schema

<xsd:element name="alien_probe" type="alien_probeType">

This is how the element appears in the XML file

<alien_probe size="14cm">
    Ouch! The probe was like a <shape>fishing pole</shape> and was made of <composition>hurtium</composition>
</alien_probe>

Complex type elements can be defined in terms of other complex types

This complex type has all of the characteristics of the complex type used to define it plus any new components added to the definition

Here's a complex type defined earlier:

<xsd:complexType name="alien_toolsType">
    <xsd:sequence>
        <xsd:element name="tubes" type="xsd:string"/>
        <xsd:element name="shape" type="xsd:string"/>
        <xsd:element name="duration" type="xsd:integer"/>
    </xsd:sequence>
</xsd:complexType>

This is how the element is called in the schema

<xsd:element name="alien_tools" type="alien_toolsType">

In the XML file, this element would look like this

<alien_tools>
    <tubes>
metallic</tubes>
    <shape>
rectangular</shape>
    <duration>
3 hours</duration>
</alien_tools>

The generic form that makes use of this complex type to define a new one is:

<xsd:complexType name="nameType">
    <xsd:complexContent>
        <xsd:extension base="xsd:previousType" >
            <xsd:element name="secondname" type="xsd:string" />
            <xsd:element name="thirdname" type="xsd:string" />
            < xsd:attribute name="name" type="someValue" />
         </xsd:extension>
     </xsd:complexContent>
</xsd:complexType>

The elements enclosed in the extension tag are the new elements being added to the previously defined complex type

This is a valid element:

<xsd:complexType name="alien_probeType">
    <xsd:complexContent>
        <xsd:extension base="xsd:alien_toolsType" >
        <xsd:element name="power_source" type="xsd:string" />
        <xsd:element name="attachments" type="xsd:string" />
            < xsd:attribute name="size" type="xsd:string" />
         </xsd:extension>
     </xsd:complexContent>
</xsd:complexType>

This is how the element is called in the schema

<xsd:element name="alien_probe" type="alien_probeType">

This is how the element appears in the XML file

<alien_probe size="14cm">
    <alien_tools>
        <tubes>
metallic </tubes>
        <shape>
rectangular </shape>
        <duration>
3 hours </duration>
    </alien_tools>
    <power_source>
photonic</power_source>
    <attachments>
extension_handle</attachments>
</alien_probe>

Home
schema schema 2 schema 3 schema 4 schema 5 schema 6 schema 7 schema 8 schema 9
Next

schema 10 schema 11 schema 12 schema 13 schema 14 schema 15 schema 16 schema 17

Page by Howard Rosenbaum
Find me at hrosenba@indiana.edu You are here: http://www.slis.indiana.edu/hrosenba/www/Workshops/XML/Demo/schema16.html