XML Schemas

Because XML schemas are such an important component of the JAXB processing model--and because other data binding facilities like JAXP work with DTDs instead of schemas--it is useful to review here some basics about what XML schemas are and how they work.

XML Schemas are a powerful way to describe allowable elements, attributes, entities, and relationships in an XML document. A more robust alternative to DTDs, the purpose of an XML schema is to define classes of XML documents that must adhere to a particular set of structural and data constraints--that is, you may want to define separate schemas for chapter-oriented books, for an online purchase order system, or for a personnel database. In the context of JAXB, an XML document containing data that is constrained by an XML schema is referred to as a document instance, and the structure and data within a document instance is referred to as a content tree.

Note: In practice, the term "document" is not always accurate, as an XML instance document does not have to be a completely formed, selfstanding document file; it can instead take the form of streams of data passed between applications, or of sets of database fields, or of XML infosets in which blocks of information contain just enough information to describe where they fit in the schema structure.

The following sample code is taken from the W3C's Schema Part 0: Primer (http://www.w3.org/TR/2001/REC-xmlschema-0-20010502/), and illustrates an XML document, po.xml, for a simple purchase order.

<?xml version="1.0"?>
<purchaseOrder orderDate="1999-10-20">
  <shipTo country="US">
    <name>Alice Smith</name>
    <street>123 Maple Street</street>
    <city>Mill Valley</city>
  <billTo country="US">
    <name>Robert Smith</name>
    <street>8 Oak Avenue</street>
    <city>Old Town</city>
<comment>Hurry, my lawn is going wild!</comment>
    <item partNum="872-AA">
      <comment>Confirm this is electric</comment>
    <item partNum="926-AA">
      <productName>Baby Monitor</productName>

The root element, purchaseOrder, contains the child elements shipTo, billTo, comment, and items. All of these child elements except comment contain other child elements. The leaves of the tree are the child elements like name, street, city, and state, which do not contain any further child elements. Elements that contain other child elements or can accept attributes are referred to as complex types. Elements that contain only PCDATA and no child elements are referred to as simple types.

The complex types and some of the simple types in po.xml are defined in the purchase order schema below. Again, this example schema, po.xsd, is derived from the W3C's Schema Part 0: Primer (http://www.w3.org/TR/2001/REC-xmlschema-0-20010502/).

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
<xsd:element name="comment" type="xsd:string"/>
<xsd:complexType name="PurchaseOrderType">
    <xsd:element name="shipTo" type="USAddress"/>
    <xsd:element name="billTo" type="USAddress"/>
    <xsd:element ref="comment" minOccurs="0"/>
    <xsd:element name="items" type="Items"/>
  <xsd:attribute name="orderDate" type="xsd:date"/>

<xsd:complexType name="USAddress">
    <xsd:element name="name" type="xsd:string"/>
    <xsd:element name="street" type="xsd:string"/>
    <xsd:element name="city" type="xsd:string"/>
    <xsd:element name="state" type="xsd:string"/>
    <xsd:element name="zip" type="xsd:decimal"/>
  <xsd:attribute name="country" type="xsd:NMTOKEN" 

<xsd:complexType name="Items">
    <xsd:element name="item" minOccurs="1"
          <xsd:element name="productName"
          <xsd:element name="quantity">
              <xsd:restriction base="xsd:positiveInteger">
                <xsd:maxExclusive value="100"/>
          <xsd:element name="USPrice" type="xsd:decimal"/>
          <xsd:element ref="comment" minOccurs="0"/>
          <xsd:element name="shipDate" type="xsd:date"
        <xsd:attribute name="partNum" type="SKU"

<!-- Stock Keeping Unit, a code for identifying products -->
<xsd:simpleType name="SKU">
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="\d{3}-[A-Z]{2}"/>


In this example, the schema comprises, similar to a DTD, a main or root schema element and several child elements, element, complexType, and simpleType. Unlike a DTD, this schema also specifies as attributes data types like decimal, date, fixed, and string. The schema also specifies constraints like pattern value, minOccurs, and positiveInteger, among others. In DTDs, you can only specify data types for textual data (PCDATA and CDATA); XML schema supports more complex textual and numeric data types and constraints, all of which have direct analogs in the Java language.

Note that every element in this schema has the prefix xsd:, which is associated with the W3C XML Schema namespace. To this end, the namespace declaration, xmlns:xsd="http://www.w3.org/2001/XMLSchema", is declared as an attribute to the schema element.

Namespace support is another important feature of XML schemas because it provides a means to differentiate between elements written against different schemas or used for varying purposes, but which may happen to have the same name as other elements in a document. For example, suppose you declared two namespaces in your schema, one for foo and another for bar. Two XML documents are combined, one from a billing database and another from an shipping database, each of which was written against a different schema. By specifying namespaces in your schema, you can differentiate between, say, foo:address and bar:address.