XML Schema Indicators

|  | Search W3Schools : |

HOME | HTML | CSS | XML | JAVASCRIPT | ASP | PHP | SQL | MORE... |  | References | Examples | Forum | About

ADVERTISEMENTS XML Certification Download XML editor Custom Programming |  |

Table of contents

Schema Tutorial
XSD HOME
XSD Intro
XSD Why Use
XSD How To
XSD <schema>
Simple Types
XSD Elements
XSD Attributes
XSD Restrictions
Complex Types
XSD Elements
XSD Empty
XSD Elements Only
XSD Text Only
XSD Mixed
XSD Indicators
XSD <any>
XSD <anyAttribute>
XSD Substitution
XSD Example
Data Types
XSD String
XSD Date
XSD Numeric
XSD Misc
XSD Editor
XSD Summary
References
XSD Reference
XSD Validator

Selected Reading

Web Statistics
Web Glossary
Web Hosting
Web Quality
W3Schools Tutorials
W3Schools Forum
Helping W3Schools

# XSD Complex Types Indicators

We can control HOW elements are to be used in documents with indicators.

## Indicators

There are seven indicators:

Order indicators:

- All
- Choice
- Sequence

Occurrence indicators:

- maxOccurs
- minOccurs

Group indicators:

- Group name
- attributeGroup name

## Order Indicators

Order indicators are used to define the order of the elements.

All Indicator

The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once:

    <xs:element name="person">
      <xs:complexType>
        <xs:all>
          <xs:element name="firstname" type="xs:string"/>
          <xs:element name="lastname" type="xs:string"/>
        </xs:all>
      </xs:complexType>
    </xs:element>

Note: When using the <all> indicator you can set the <minOccurs> indicator to 0 or 1 and the <maxOccurs> indicator can only be set to 1 (the <minOccurs> and <maxOccurs> are described later).

Choice Indicator

The <choice> indicator specifies that either one child element or another can occur:

    <xs:element name="person">
      <xs:complexType>
        <xs:choice>
          <xs:element name="employee" type="employee"/>
          <xs:element name="member" type="member"/>
        </xs:choice>
      </xs:complexType>
    </xs:element>

Sequence Indicator

The <sequence> indicator specifies that the child elements must appear in a specific order:

    <xs:element name="person">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="firstname" type="xs:string"/>
          <xs:element name="lastname" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>

## Occurrence Indicators

Occurrence indicators are used to define how often an element can occur.

Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1.

maxOccurs Indicator

The <maxOccurs> indicator specifies the maximum number of times an element can occur:

    <xs:element name="person">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="full_name" type="xs:string"/>
          <xs:element name="child_name" type="xs:string" maxOccurs="10"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>

The example above indicates that the "child_name" element can occur a minimum of one time (the default value for minOccurs is 1) and a maximum of ten times in the "person" element.

minOccurs Indicator

The <minOccurs> indicator specifies the minimum number of times an element can occur:

    <xs:element name="person">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="full_name" type="xs:string"/>
          <xs:element name="child_name" type="xs:string"
          maxOccurs="10" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>

The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element.

Tip: To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement:

A working example:

An XML file called "Myfamily.xml":

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="family.xsd">

    <person>
    <full_name>Hege Refsnes</full_name>
    <child_name>Cecilie</child_name>
    </person>

    <person>
    <full_name>Tove Refsnes</full_name>
    <child_name>Hege</child_name>
    <child_name>Stale</child_name>
    <child_name>Jim</child_name>
    <child_name>Borge</child_name>
    </person>

    <person>
    <full_name>Stale Refsnes</full_name>
    </person>

    </persons>

The XML file above contains a root element named "persons". Inside this root element we have defined three "person" elements. Each "person" element must contain a "full_name" element and it can contain up to five "child_name" elements.

Here is the schema file "family.xsd":

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">

    <xs:element name="persons">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="person" maxOccurs="unbounded">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="full_name" type="xs:string"/>
                <xs:element name="child_name" type="xs:string"
                minOccurs="0" maxOccurs="5"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:element>

    </xs:schema>

## Group Indicators

Group indicators are used to define related sets of elements.

Element Groups

Element groups are defined with the group declaration, like this:

    <xs:group name="groupname">
      ...
    </xs:group>

You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "persongroup", that defines a group of elements that must occur in an exact sequence:

    <xs:group name="persongroup">
      <xs:sequence>
        <xs:element name="firstname" type="xs:string"/>
        <xs:element name="lastname" type="xs:string"/>
        <xs:element name="birthday" type="xs:date"/>
      </xs:sequence>
    </xs:group>

After you have defined a group, you can reference it in another definition, like this:

    <xs:group name="persongroup">
      <xs:sequence>
        <xs:element name="firstname" type="xs:string"/>
        <xs:element name="lastname" type="xs:string"/>
        <xs:element name="birthday" type="xs:date"/>
      </xs:sequence>
    </xs:group>

    <xs:element name="person" type="personinfo"/>

    <xs:complexType name="personinfo">
      <xs:sequence>
        <xs:group ref="persongroup"/>
        <xs:element name="country" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>

Attribute Groups

Attribute groups are defined with the attributeGroup declaration, like this:

    <xs:attributeGroup name="groupname">
      ...
    </xs:attributeGroup>

The following example defines an attribute group named "personattrgroup":

    <xs:attributeGroup name="personattrgroup">
      <xs:attribute name="firstname" type="xs:string"/>
      <xs:attribute name="lastname" type="xs:string"/>
      <xs:attribute name="birthday" type="xs:date"/>
    </xs:attributeGroup>

After you have defined an attribute group, you can reference it in another definition, like this:

    <xs:attributeGroup name="personattrgroup">
      <xs:attribute name="firstname" type="xs:string"/>
      <xs:attribute name="lastname" type="xs:string"/>
      <xs:attribute name="birthday" type="xs:date"/>
    </xs:attributeGroup>

    <xs:element name="person">
      <xs:complexType>
        <xs:attributeGroup ref="personattrgroup"/>
      </xs:complexType>
    </xs:element>

## Get Your Diploma!

W3Schools' Online Certification Program is the perfect solution for busy professionals who need to balance work, family, and career building.

The HTML Certificate is for developers who want to document their knowledge of HTML, XHTML, and CSS.

The JavaScript Certificate is for developers who want to document their knowledge of JavaScript and the HTML DOM.

The XML Certificate is for developers who want to document their knowledge of XML, XML DOM and XSLT.

The ASP Certificate is for developers who want to document their knowledge of ASP, SQL, and ADO.

The PHP Certificate is for developers who want to document their knowledge of PHP and SQL (MySQL).

WEB HOSTING
Web based charting for ASP.NET
$15 Domain Name Registration Save $20 / year!
Buy UK Domain Names Register Domain Names
Cheap Domain Names Cheap Web Hosting
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting

WEB BUILDING
Website Templates Flash Templates Website Builder Internet Business Opportunity
Custom Programming
FREE Trial or Demo Web Content Manager Forms,Web Alerts,RSS
Download XML editor
FREE Flash Website
FREE Web Templates

EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification

HOME or Top of Page |  | Printer Friendly
W3Schools is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy. Copyright 1999-2009 by Refsnes Data. All Rights Reserved.