XML Schemas (2/2) - exploring XML | WebReference

XML Schemas (2/2) - exploring XML

XML Schemas

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 minOccurs and maxOccurs attributes to define cardinalities:

<element ref="optionalElement" minOccurs="0"/>
<element ref="twoOrMoreElements" minOccurs="2" maxOccurs="unbounded"/>
<element ref="exactlyOneElement" />

The default values for minOccurs and maxOccurs are 1. Another possibility is to use the choice and 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.

<xsd:choice>
<element ref="EitherThis"/>
<element ref="OrThat"/>
</xsd:choice>

<xsd:all>
<element ref="Ying"/>
<element ref="Yang"/>
</xsd:all/>

Namespaces

An XML Schema definition specifies one vocabulary, the target namespace, possibly using other vocabularies by including them through the use of namespaces:

<xsd:schema targetNamespace='http://www.physics.com/measurements' xmlns:xsd='http://www.w3.org/1999/XMLSchema' xmlns:units= 'http://www.physics.com/units'>

<xsd:element name='units' type='units:Units'/>
<xsd:element name='measurement' type='measurement'/>
<complexType name='measurement'>
<element name='time' type='time'/>
<element name='value' type='value'/>
</complexType>

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.

Conclusion

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!

http://www.internet.com

Produced by Michael Claßen
All Rights Reserved. Legal Notices.

URL: http://www.webreference.com/xml/column21/2.html
Created: Oct 08, 2000
Revised: Oct 08, 2000