Microsoft XML Core Services (MSXML) 4.0 - XML Schemas

Defining Elements and Attributes

When defining a schema for a particular class of documents, you specify which elements and attributes are allowed in a complying XML document, and how those elements and attributes are related to each other. In an XML-Data Reduced (XDR) schema, you define elements and attributes by specifying an <ElementType> element and an <AttributeType> element, respectively. You can then declare an instance of an element or an attribute using <element> or <attribute> element tags.

Example

The following example shows an XDR schema that uses the <ElementType> element to define four elements: <title>, <author>, <pages>, and <book>. The definition of the <book> element includes the content model for that element, which specifies that each <book> element contains <title>, <author>, and <pages> child elements. This content model is specified using an <element> element with a type attribute that references the element type defined earlier.

The schema also defines the copyright attribute for the <book> element by using the <AttributeType> element. Then the schema declares the attribute using the <attribute> element.

XDR Schema File (abovefile.xml)

<?xml version="1.0"?>
<Schema xmlns="urn: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>

Data File (data.xml)

<r xmlns:a="x-schema:abovefile.xml">
<a:book copyright="">
  <a:title/>
  <a:author/>
  <a:pages/>
</a:book>
</r>

Specifying Global <AttributeType> Elements

You can specify the <AttributeType> element globally by placing it outside the context of any <ElementType> element. This allows multiple elements to share the definition of a common attribute.

Example

The following example shows an XDR schema with multiple elements that share the definition of the copyright attribute.

XDR Schema File (abovefile.xml)

<?xml version="1.0"?>
<Schema xmlns="schemas-microsoft-com:xml-data">
  <ElementType name="title"/>
  <ElementType name="author"/>
  <ElementType name="pages"/>
  <AttributeType name="copyright"/>
  <ElementType name="book" model="closed">
    <element type="title"/>
    <element type="author"/>
    <element type="pages"/>
    <attribute type="copyright"/>
  </ElementType>
</Schema>

Data File (data_global.xml)

<r xmlns:a="x-schema:abovefile.xml">
<a:book copyright="">
  <a:title/>
  <a:author/>
  <a:pages/>
</a:book>
</r>

This HTML Help has been published using the chm2web software.