WebReference.com - Part 2 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (3/6) | WebReference

WebReference.com - Part 2 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (3/6)

To page 1To page 2current pageTo page 4To page 5To page 6
[previous] [next]

Professional XML Schemas

Element Occurrence Indicators

By default, when we declare an element in an XML Schema it is required to appear once and once only. However there are times when we might want to make the appearance of an element in a document optional. For example, we might want to make the MiddleInitial child element of our Customer element optional in case the customer does not have a middle name. Indeed there may be times when we want an element to be repeatable; for example, we might want to allow several MiddleInitial elements if the customer has several middle names.

To replicate the functionality offered by the cardinality operators in DTDs, namely ?, *, and +, which indicate how many times an element could appear in an instance document, XML Schema introduces two occurrence constraints which take the form of attributes on the element declaration: minOccurs and maxOccurs. Their value indicates how many times the element can appear, and are a lot simpler to use than the cardinality operators in DTDs because we just specify a minimum and maximum number of times that an element can appear. The maxOccurs attribute can also take a value of unbounded, which means that there is no maximum number of times the element can appear in the document instance.

The following table shows the mapping of DTD cardinality operators to the equivalent values of minOccurs and maxOccurs XML Schema attributes:

Cardinality Operator minOccurs Value maxOccurs Value Number of Child Element(s)
[none] 1 1 One and only one
? 0 1 Zero or one
* 0 unbounded Zero or more
+ 1 unbounded One or more

Let's look at some examples. To start, if we want an element to appear once and once only, then we do not have to add anything to the declaration, as the default values for both attributes if not included are 1. However, for clarity we could explicitly state that the MiddleInitial element must appear once and only once:

<element name = "MiddleInitial" type = "string" minOccurs = "1" 
         maxOccurs = "1" />

If we wanted to make the element optional, so that the element could appear but is not required to do so, and that when it did appear it could only appear once we could use the following:

<element name = "MiddleInitial" type = "string" minOccurs = "0" 
         maxOccurs = "1" />

If we wanted to require at least one MiddleInitial element, yet allow no more than 4 we could use the following:

<element name = "MiddleInitial" type = "string" minOccurs = "1" 
         maxOccurs = "4" />

If we wanted to make sure that there were at least two MiddleInitial elements, but that there were no upper limits on the number of times the element could appear, we could use the following:

<element name = "MiddleInitial" type = "string" minOccurs = "2" 
         maxOccurs = "unbounded" />

Note that you cannot declare minOccurs and maxOccurs on global elements, only on local element declarations.

While we cannot use the minOccurs and maxOccurs attributes on a global element declaration, we can add them to a local element declaration that references a global declaration using the ref attribute:

<?xml version = "1.0" ?>
<schema>
   <element name = "Customer">
      <complexType>
         <sequence>
            <element ref = "FirstName" minOccurs = "0" maxOccurs = "1" />
            <element ref = "MiddleInitial" 
                     minOccurs = "0" maxOccurs = "unbounded" />
            <element ref = "LastName" minOccurs = "1" maxOccurs = "1" />
         </sequence>
         <attribute name = "customerID" type = "integer" />
      </complexType>
   </element>
   <element name = "FirstName" type = "string" />
   <element name = "MiddleInitial" type = "string" />
   <element name = "LastName" type = "string" />
</schema>

Here the FirstName is optional, the MiddleInitial element is optional although it can appear as many times as the document author requires, and the LastName is required.


To page 1<current pageTo page 4To page 5To page 6
[previous] [next]

Created: October 22, 2001
Revised: October 22, 2001


URL: https://webreference.com/authoring/languages/xml/schemas/chap1/2/3.html