The most important part of any schema is the formal specification of what elements and attributes are allowed within a particular class of document, and how those elements and attributes are related to each other. Elements and attributes are defined in an XML Schema document by specifying an <ElementType ...> and <AttributeType ...>, respectively. These provide the definition and type of the element or attribute. An instance of an element or an attribute is declared using <element ...> or <attribute ...> tags. This can be thought of as being analogous to declaring a typedef in the C programming language and then declaring a variable of that type.
<?xml version="1.0"?> <Schema xmlns="schemas-microsoft-com:xml-data"> <ElementType name="title" /> <ElementType name="author" /> <ElementType name="pages" /> <ElementType name="book" model="closed"> <element type="title" /> <element type="author" /> <element type="pages" /> <AttributeType name="copyright" /> <attribute type="copyright" /> </ElementType> </Schema>
In this example, there are four <ElementType> elements: "title," "author," "pages," and "book." These are simply definitions of the elements. Within the ElementType for "book," however, you can declare the content model for a book. Each book contains "title," "author," and "pages" elements using the <element> with a type attribute that references the ElementType. You can also define an <AttributeType> for the copyright attribute and then declare its usage using the <attribute> element with a type attribute that references its definition.
Note also that the definition of the copyright attribute is contained within the ElementType for "book." Attribute definitions are distinct from ElementType definitions in that they can be declared within the scope of an ElementType, allowing different element types to declare attributes of the same name but with (potentially) different meaning. <AttributeType> elements can be declared globally by placing them outside the context of an ElementType. In this way, multiple elements can share the definition of a common attribute without having to redeclare the AttributeType within each ElementType.