Schema Simple Attributes
# XSD Attributes
* * *
All attributes are declared as simple types.
* * *
## What Is an Attribute?
Simple elements cannot have attributes. If an element has attributes, it is considered a complex type. However, attributes themselves are always declared as simple types.
* * *
## How to Declare an Attribute?
The syntax for defining an attribute is:
Here, xxx is the attribute name, and yyy specifies the data type of the attribute. XML Schema provides many built-in data types.
### The most commonly used types are:
* xs:string
* xs:decimal
* xs:integer
* xs:boolean
* xs:date
* xs:time
### Example
This is an XML element with an attribute:
Smith
This is the corresponding attribute definition:
* * *
## Default and Fixed Values for Attributes
Attributes may have specified default or fixed values.
A default value is automatically assigned to the element when no other value is specified.
In the following example, the default value is "EN":
A fixed value is also automatically assigned to the element, and you cannot specify another value.
In the following example, the fixed value is "EN":
* * *
## Optional and Required Attributes
By default, attributes are optional. To specify that an attribute is required, use the "use" attribute:
* * *
## Restrictions on Content
When an XML element or attribute has a defined data type, restrictions are applied to its content.
If an XML element's type is "xs:date", and its content is a string like "Hello World", the element will fail validation.
With XML Schema, you can also add your own restrictions to XML elements and attributes. These restrictions are called facets.
YouTip