Simple elements cannot have attribute. If an element has attributes, it is considered to be of a complex types.
But the attribute itself is declared as a simple type.
How to Define an Attribute?
The syntax for defining an attribute is as follows:
<xs:attribute name="xxx" type="yyy"/>
where xxx is the name of the attribute and yyy specify the
data type of the attribute.
XML Schema has a lot of built-in data type. The most common
types are as follows:
xs:string
xs:decimal
xs:boolean
xs:integer
xs:date
xs:time
Example
Here is an XML element with attribute:
<lastname lang="EN">Smith</lastname>
And here is the corresponding attribute definitions:
<xs:attribute name="lang" type="xs:string"/>
Default and Fixed Values for Attributes
Attributes may havedefault value OR a fixed value specified.
A default value is automatically assigned to the attributes when no other value is specified.
In the following example the default value for the attribute is "EN":
When an XML elements or attributes has a data type defined, it
puts restrictions on the element's or attribute's content.
If
an XML element is of type "xs:date" and contain a string like
"Hello World", the element will not validate.
With XML Schema, you can also add your own restrictions to
your XML elements and attributes. These restrictions are called
facet. You can also read more about facets in the next chapter.
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.