HOME

Schema Tutorial
XSD HOME
XSD Intro
XSD Why
XSD HowTo
XSD <schema>

Simple Types
XSD Elements
XSD Attributes
XSD Restrictions

Complex Types
XSD Elements
XSD Empty
XSD Elements Only
XSD Text Only
XSD Mixed
XSD Indicators
XSD <any>
XSD <anyAttribute>
XSD Substitution
XSD Example

Data Types
XSD String
XSD Date
XSD Numeric
XSD Misc

References
XSD Reference
Download XMLSpy here
Please visit our sponsors !

XSD Attributes

prev next

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.


prev next

Jump to: Top of Page or HOME or Printer Friendly 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


Validate How we converted to XHTML Validate

IISCart
ASP Ecommerce & Shopping Cart


Your own Web Site?

Read W3Schools
Hosting Tutorial



$15 Domain Name
Registration
Save $20 / year!



Advertise
at W3Schools

Only 0.5 CPM



SELECTED LINKS

University Online
Master Degree
Bachelor Degree


Web Software

The Future of
Web Development


Jobs and Careers

Web Security
Web Statistics
Web Standards


PARTNERS

W3Schools
TopXML
VisualBuilder
XMLPitstop
DevelopersDex
DevGuru
Programmers Heaven
The Code Project
Tek Tips Forum
ZVON.ORG
TopXML Search