XML Schemas (2/2) - exploring XML
Expressing cardinalities of elements
XML Schema has more possibilities than DTD for expressing cardinalities on the
elements of a document type.
In DTD you indicate that a sequence of one only (1),
zero or more (*), or one or more (+) elements from a given set can occur.
XML Schema uses
to define cardinalities:
<element ref="optionalElement" minOccurs="0"/>
<element ref="twoOrMoreElements" minOccurs="2" maxOccurs="unbounded"/>
<element ref="exactlyOneElement" />
The default values for
maxOccurs are 1.
Another possibility is to use the
all elements. The element
choice allows only one of its children
to appear in an instance, whereas
all defines that all child elements in the group
may appear at most once in any order. Such constraints are difficult to express in a DTD.
An XML Schema definition specifies one vocabulary, the target namespace, possibly using other vocabularies by including them through the use of namespaces:
<xsd:element name='units' type='units:Units'/>
<xsd:element name='measurement' type='measurement'/>
<element name='time' type='time'/>
<element name='value' type='value'/>
This mechanism offers the modularity and extensibility needed to build up a grand hierarchy of namespaces, from basic things like addresses up to very specific objects in any subject matter.
XML Schema offers a rich and flexible mechanism for defining XML vocabularies. It promises the next level of interoperability by describing meta-information about XML in XML. Various tools for validating and editing schemas are available from the Apache Project and IBM alphaworks. Start using XML Schema today!
Created: Oct 08, 2000
Revised: Oct 08, 2000