Web Development Tutorials




  
  

Indicators

It is possible to control how XML elements are used with indicators. The most common types of Schema indicators are group indicators and occurance indicators. In previous sections we have already worked with the <sequence> indicator. Here additional indicators types will be introduced. The table below contains the most often used indicators.

Schema Indicators

Indicator Description
All An order indicator. The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once.
Choice An order indicator. The <choice> indicator specifies that either one child element or another can occur
Sequence An order indicator. The <sequence> indicator specifies that the child elements must appear in a specific order
maxOccurs An occurance indicator. The maxOccurs indicator specifies the maximum number of times an element can occur
minOccurs An occurance indicator. The minOccurs indicator specifies the minimum number of times an element can occur

In the following example the <all> indicator is used. The Tree element contains two child elements CommonName and ScientificName. The <all> indicator specifies that the child elements can appear in any order and they can only occur once.

<xsd:element name="Tree">
    <xsd:complexType>
        <xsd:all>
            <xsd:element name="CommonName" type="xsd:string"/>
            <xsd:element name="ScientificName" type="xsd:string"/>
        </xsd:all>
    </xsd:complexType>
</xsd:element>

Listing 3-26. Indicators.

In the following example the <Choice> indicator is used. The Tree element contains two child elements CommonName and ScientificName. The <all> indicator specifies that either one child element or another can appear.

<xsd:element name="Tree">
    <xsd:complexType>
        <xsd:choice>
            <xsd:element name="CommonName" type="xsd:string" />
            <xsdelement name="ScientificName" type="xsd:string"/>
        </xsd:choice>
    </xsd:complexType>
</xsd:element>

Listing 3-27. Choice indicator.

In the next example the maxOccurs and minOccurs indicators are used. The Tree element contains two child elements CommonName and Type. The maxOccurs indicator specifies that the Tree element may contain a minimum of 1 common name and a maximum of 3 common names. The Type element contains the minOccurs indicator set to a value of 0. This is another way of stating that a tree is not required to have a type.

<xsd:element name="Tree">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="CommonName" type="xsd:string" maxOccurs="3"/>
            <xsd:element name="Type" type="xsd:string" minOccurs="0"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

Listing 3-28. Alternative ways of stating trees.

In some cases it is necessary to have an unlimited number of child elements within a parent element. This can be accomplished by assigning the value unbounded to the maxOccurs indicator: maxOccurs="unbounded".


TOP