Specifying Attribute Constraints and Defaults

As with elements, you can specify the type of attributes. Unlike with elements, however, attributes must be of a simple type. In addition, you don't use minOccurs and maxOccurs for attributes because attributes can appear only once, at most. Instead, you use a different syntax when constraining attributes.

You declare attributes with the <xsd:attribute> element. The <xsd:attribute> element itself has a type attribute that gives the attribute's (simple) type. So how do you indicate if an attribute is required or optional, or if there's a default value, or even if the value of the attribute is fixed at a certain value? You use the <xsd:attribute> element's use and value attributes.

The use attribute ...

Get Inside XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.