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

L595 XML@IU logo

Creating Schemas

Complex types

Providing choice in complex types

Using the choice facet, an element can be defined so that one, two, or all of the elements can be used in the XML document

This serves the same purpose as the | (pipe) in the DTD

The generic form is:

<xsd:complexType name="nameType">
    <xsd:choice>
        <xsd:element name="name" type="attribute">
        <xsd:element name="name" type="attribute">
        <xsd:element name="name" type="attribute">
    </xsd:choice>
</xsd:complexType>

This is a valid element:

<xsd:complexType name="alien_probeType">
    <xsd:choice>
        <xsd:element name="tubes" type="metallic">
        <xsd:element name="shapes" type="triangular">
        <xsd:element name="duration" type="xsd:time">
    </xsd:choice>
</xsd:complexType>

In the schema, this complexType can be used in an element by calling it

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

In the XML file, this is a valid element

<alien_probe>
    <tubes type="metallic">
outer_spacium</tubes>
    <shape type="triangular">
long</shape>
</alien_probe>

So is this

<alien_probe>
    <tubes type="metallic">
outer_spacium</tubes>
    <duration type="time">
4 hours</duration>
</alien_probe>

Using the all facet, an element can be defined so that all of the elements can be used in any order in the XML file

The generic form is:

<xsd:complexType name="nameType">
    <xsd:all>
        <xsd:element name="name1" type="attribute">
        <xsd:element name="name2" type="attribute">
        <xsd:element name="name3" type="attribute">
    </xsd:all>
</xsd:complexType>

This is the complexType with all three elements:

<xsd:complexType name="alien_probeType">
    <xsd:choice>
        <xsd:element name="tubes" type="metallic">
        <xsd:element name="shapes" type="triangular">
        <xsd:element name="duration" type="xsd:time">
    </xsd:choice>
</xsd:complexType>

In the schema, this complexType can be used in an element by calling it

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

Because of the use of xsd:all, order no longer matters, so this is a valid element :

<alien_probe>
    <duration type="time">
4 hours</duration>
    <tubes type="metallic">
outer_spacium</tubes>
    <shape type="triangular">
long</shape>
</alien_probe>

So is this

<alien_probe>
    <shape type="triangular">
long</shape>
    <tubes type="metallic">
outer_spacium</tubes>
    <duration type="time">
4 hours</duration>
</alien_probe>

There are several restrictions when using the all facet

An element may only be used once in this type of facet

It may not contain groups of elements

It must be the only child of the complex type

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