Attributes Entitites   «Prev  Next»

Lesson 7XML schemas
ObjectiveExplain the purpose of XML schemas.

XML Schemas

A DTD defines the structure and syntax of an XML document and, to some extent, provides constraints on the types of data contained in XML elements. XML schemas are designed to extend the functionality and usefulness of a DTD.
Consider the following element declaration from a DTD:

A validating XML parser would consider the following two entries in an XML document to be both well-formed and valid:
<PHONE-NUMBER> (512) 555-9981 </PHONE-NUMBER>

A phone number value of HXW2VQ is certainly not within the range of values that are considered valid phone numbers. In cases where XML element content must be constrained to certain values, XML schemas offer distinct advantages over DTDs.
With schemas, you not only can define structure and syntax for XML documents (as in DTDs), but you can use many other useful features. These features include setting data types for elements, the minimum number of times and element can occur, the maximum number of times an element can occur, and placing restrictions on the ranges of values for elements.
While DTDs are written in EBNF, schemas are written in XML itself. This makes schemas easier to understand.

Creating XML schemas

Elements declared in an XML schema are two types: 1) complex and 2) simple. Complex types are elements that may contain sub-elements or attributes. Simple types are elements that contain numbers, strings, dates, and so on, but no sub-elements.
Simple types are declared using the simpleType element.
Many simple types are built into the XML schema recommendation. The following diagram contains a list of these simple types.

XML Simple Table
Complex types contain simple types declared using the element keyword and attributes declared using the attribute element. Examine the slideshow below to see how an XML schema is created.

  1. An XML schema declaration begins with the schema element that specifies a URI for a namespace.
  2. The first statement declares an element named Order. Its element type is OrderType.
  3. The next group of statements defines the OrderType declared previously.
  4. This group of statements defines a simple type named SKU

Creating XML Schemas

XML Schemas consist of XML

Recall that XML schemas are created using XML itself. As such we say that a complex type in an XML schema is created using the element element and the attribute element.
The next lesson concludes this module.