Creating a Simple Schema

While all schemas use the same core parts, there are a number of different structural alternatives and key pieces worth examining before diving into all of the parts. Examine the structure of Example 1-8.

Example 1-8. A simple XML document for definition in a schema
<?xml version="1.0" encoding="us-ascii"?>
<authors>
    <person id="lear">
        <name>Edward Lear</name>
        <nationality>British</nationality>
    </person>
    <person id="asimov">
        <name>Isaac Asimov</name>
        <nationality>American</nationality>
    </person>
    <person id="mysteryperson"/>
</authors>

This document contains an authors element, which itself contains multiple person elements. Each person element has an id attribute and may contain a name and a nationality element. For now, we’ll treat all of the textual content of the elements and attributes as text. One way to define this document is in a schema whose structure mirrors the document, called a “russian doll” schema after the wooden matruschkas; see Example 1-9. The names of the elements being defined are boldfaced to make it easier to read.

Example 1-9. A “russian doll” schema describing the Example 1-8 document
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
  <xs:element name="authors">
     <xs:complexType>
        <xs:sequence>
           <xs:element name="person" maxOccurs="unbounded">
              <xs:complexType>
                <xs:sequence minOccurs="0" >
                   <xs:element name="name" type="xs:string" />
                   <xs:element name="nationality" type="xs:
                     string"  />
                </xs:sequence>
                <xs:attribute name="id" type="xs:string" ...

Get XML Pocket Reference, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.