XML

Attributes for simple data types

Notice in the code below that the simpleType element has the following attributes: name, base, abstract, final, and derivedBy. The name attribute can be either a built-in type or a user-defined type. The base attribute is the basetype that is being used to define the new type. The final attribute is discussed in detail later in this chapter. For more information about this attribute, refer to the schema specification.

The derivedBy attribute can be set to list, restriction, or reproduction. The list value allows you to create a data type that consists of a list of items separated by space. For example, you can use the following declaration to create a list data type:

  <simpleType name='StringList' base='string' derivedBy='list'/>

This data type can then be used in an XML document to create a new list type, as shown here:

  <myListElement xsi:type='StringList'>
     This is not list item 1.
     This is not list item 2.
     This is not list item 3.
  </myListElement>

By using xsi, you overrode the default declaration of the myListElement and made it a StringList data type. Since a StringList data type contains a list of strings, you can now use a list of strings as content for the myListElement. The xsi namespace will be discussed in more detail later in the chapter.

Up to this point, we have been discussing the XML schema 2 specification, which covers simple data types. The XML schema 1 specification covers all the general issues involving schemas and also covers complex data types. Let's now take a look at the complex data types described in the first schema specification.

Complex Data Types

A data type can either be simple or complex. Simple data types include the data types discussed in the previous section. Complex data types contain the child elements that belong to an element, as well as all the attributes that are associated with the element. If you visit http://www.w3.org/TR/xmlschema-1/, you'll find the first schema specification. Combining this specification with that for simple data types, you will have the complete schema specification. Our discussion of complex data types in this section will include an explanation of all of the elements of a schema, a sample DTD for a schema, and numerous examples showing how to create a schema and complex data types.

Viewing Schemas in XML Tools

The schema was generated from the XHTML DTD we created in Chapter 5. You can open XHTMLschema.xsd in XML Authority version 1.2 or higher.

Because schemas are written as well-formed XML documents, you can also view the schema in any other XML tools, such as XML Spy or Microsoft XML Notepad. For example, Figure 7-1 shows the schema as it would appear in XML Spy.

Figure 7-1. The schema in XML Spy.

As you can see in Figure 7-1, a schema has a well-defined structure. This structure includes a root element named schema, with one or more element child elements. These element elements can have complexType child elements; the complexType elements can in turn have annotation, group, attributeGroup, and attribute child elements. Clearly, this schema is a well-formed XML document.