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

L595 XML@IU logo

Creating Schemas

Complex types

This is an element that can be defined in terms of other elements, attributes, and text

It is used to represent the structure of a document

This is in contrast to the simple type, which can only contain content

There are five main complex types

An element may contain

Other elements

Other elements, attributes, and no text

Other elements, attributes, and text

Attributes and text

Nothing (empty) and attributes

The first complex type is an element can be defined in terms of other elements and attributes, but no text

The generic form is:

<xsd:element name="somename">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="name" type="type" />
            <xsd:element name="name2" type="type" />
        </xsd:sequence>
        < xsd:attribute name="name" type="someValue" />
    </xsd:complexType>
</xsd:element>

Because two elements are used here, the element xsd:sequence is used, which sets the order of the sibling elements

This is an example of a complex type element

<xsd:element name="alien_tools">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="probe" type="xsd:string" />
            <xsd:element name="blastulator" type="xsd:string" />
        </xsd:sequence>
        < xsd:attribute name="number" type="xsd:integer" />
    </xsd:complexType>
</xsd:element>

In the XML file, it would look like this

<alien_tools number="5">
    <probe>
model 5.2</probe>
    <blastulator>
supersoaker model</blastulator>
<alien_tools>

It is also possible to define a complex type element so that is can be used as an attribute in other complex type elements.

The generic form is:

<xsd:complexType name="nameType">
    <xsd:sequence>
        <xsd:element name="name" type="type"/>
        <xsd:element name="name2" type="type"/>
    </xsd:sequence>
</xsd:complexType>

Because two elements are used here, the element xsd:sequence is used, which sets the order of the sibling elements

Also, this complexType is global because it has been named (as a ...Type) and is not attached to any element

This means that it must be called to be used with any element

An example is:

<xsd:complexType name="alien_probesType">
    <xsd:sequence>
        <xsd:element name="tools" type="xsd:string"/>
        <xsd:element name="attachments" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>

And here's how it might be called in the schema

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

In the XML file, this element would look like this

<alien_tools>
    <tools>
knives and baseball bats</tools>
    <attachments>
wires and hoses</attachments>
</alien_tools>

Because of xsd:sequence the elements in this range must be used in the specified order

The generic form is:

<xsd:complexType name="name">
    <xsd:sequence>
        <xsd:element name="name1" type="type"/>
        <xsd:element name="name2" type="type"/>
        <xsd:element name="name3" type="type"/>
    </xsd:sequence>
</xsd:complexType>

An example is

<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>

To use this complexType in the definition of an element, it must be called

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

In the XML file, this element would look like this

<alien_tools>
    <tubes>
25cm</tubes>
    <shape>
rectangular</shape>
    <duration>
3 hours</duration>
</alien_tools>

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/schema11.html