Defining Elements and Attributes

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XML Schemas

Defining Elements and Attributes

When defining 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 the XML-Data Reduced (XDR) schema, specifying an ElementType element and an AttributeType element defines the elements and attributes, 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 defines four elements: title, author, pages, and book using the ElementType element. 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 the element element along with the type attribute that references the element type defined earlier.

The schema also declares an attribute, "copyright", for the book element by using the AttributeType element, which defines an attribute type, and then declares it using the attribute element.

<?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

<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.

<?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

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