Please visit our sponsors !
XSD Attributes
All attributes are declared as simple types.
Only complex elements can have attributes!
What is an Attribute?
Simple elements cannot have attributes. If an element has attributes, it
is considered to be of complex type. But the attribute itself is always declared
as a simple type. This means that an element with attributes always has a
complex type definition.
How to Define an Attribute
The syntax for defining an
attribute is:
<xs:attribute name="xxx" type="yyy"/>
|
where xxx is the name of the attribute and yyy is the data type of the
attribute.
Here are an XML element with an attribute:
<lastname lang="EN">Smith</lastname>
|
And here are a corresponding simple attribute definition:
<xs:attribute name="lang" type="xs:string"/>
|
Common XML Schema Data Types
XML Schema has a lot of built-in data types. Here is a list of the most
common types:
- xs:string
- xs:decimal
- xs:integer
- xs:boolean
- xs:date
- xs:time
Declare Default and Fixed Values for Attributes
Attributes can have a default value OR a fixed value specified.
A default value is automatically assigned to the attribute when no other
value is specified. In the following example the default value is "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
|
A fixed
value is also automatically assigned to the attribute. You cannot specify another value. In the following example the fixed value is "EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
|
Creating Optional and Required Attributes
All attributes are optional by default. To explicitly specify that the
attribute is optional, use the "use" attribute:
<xs:attribute name="lang" type="xs:string" use="optional"/>
|
To make an attribute required:
<xs:attribute name="lang" type="xs:string" use="required"/>
|
Restrictions on Content
When an XML element or attribute has a type defined, it puts a restriction
for the
element's or attribute's content. If an XML element is of type "xs:date" and contains a string like "Hello
Mother", the element will not validate.
But, there is more... with XML Schemas, you can add your own restrictions to your XML
elements and attributes. These restrictions are called facets. You can read more about
facets in the next chapter.
Jump to: Top of Page
or HOME or
Printer friendly page
Search W3Schools:
What Others Say About Us
Does the world know about us? Check out these places:
Dogpile
Alta Vista
MSN
Google
Excite
Lycos
Yahoo
Ask Jeeves
We Help You For Free. You Can Help Us!
W3Schools is for training only. We do not warrant its correctness or its fitness for use.
The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our
terms of use and
privacy policy.
Copyright 1999-2002 by Refsnes Data. All Rights Reserved
|