The <AttributeType> element specifies the type of attribute used within elements. Using this element, you can even specify whether the attribute is required for the element. This is done with the required attribute, as follows:
<AttributeType name="shipTo" dt:type="idref" required="yes"/>
The <attribute> element specifies instances of an attribute defined within the <AttributeType> element. You use the <attribute> element within an <ElementType> element.
Attributes are more limited in some ways than elements. For example, attributes cannot contain child elements, and you cannot require attributes to appear in any particular order; nor can you pose alternatives, such as a "product" or a "backOrderedProduct". You can specify whether an attribute is required or optional, but an attribute can appear only once per element.
At the same time, attributes have the following capabilities that elements do not:
<AttributeType name="priority" dt:type="enumeration" dt:values="high medium low" />
<AttributeType name="quantity" dt:type="int">
<attribute type="quantity" default="1"/>
Although different element types can have attributes with the same name, these attributes are independent and unrelated.
To specify the default value of an attribute, use the default attribute. You specify this attribute on <AttributeType> and <attribute> elements in the schema.
For example, the following schema assigns the default value of "New York" to the City attribute.
<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data" >
<ElementType name="Customer" >
<AttributeType name="CustomerID" />
<AttributeType name="ContactName" />
<AttributeType name="City" default="New York" />
<attribute type="CustomerID" />
<attribute type="ContactName" />
<attribute type="City" />
</ElementType>
</Schema>
If you have a document instance that has a <Customer> element with a missing City attribute, the parser assumes the default value ("New York") for the attribute and validates the document. For example, consider the following document instance.
<Customer CustomerID="ALFKI" ContactName="Maria Anders" City="London" /> <Customer CustomerID="ANATR" ContactName="Ana Trujillo" />
The customer ALFKI specifies a city ("London"), so the default value is ignored. On the other hand, the customer ANATR has no City attribute, so it receives the default value ("New York").
The behavior is slightly different if the schema specifies both a default attribute and a required attribute. For example, the following <AttributeType> specifies the City attribute as required with a default value of "New York".
<AttributeType name="City" default="New York" required="yes" />
The <Customer> element is required to have a City attribute and it must have "New York" as its value.
| This HTML Help has been published using the chm2web software. |