Designing Your own XML Schema: Constraining with Restrictions (Page 1 of 6 )
If you are new to XML Schema, I strongly suggest you go through my first article in this series, which you can check out here.
Declaring attributes in XML Schema
Since I already explained certain aspects of XML Schemas (defining them, simple types, complex types, and so on) in my previous article, I shall not repeat them again in this article. Before discussing “constraints” (or restrictions), I need to cover two more topics. Let us first complete them and then proceed to “restrictions” in XML schema.
An “attribute” is very similar to characteristics of an element. The best example would be the <IMG> tag in HTML. It accepts the file name of the image with the help of the “SRC” attribute. Similarly, you can also define your own attributes (reasonably) within an XML Schema.
Let us have a look at the following XML schema now:
According to the above example, the document root element would be “Employees.” The element “Employees” internally can contain “Employee” elements. The element “Employee” internally contains two more elements, “Name” and “Age.” You should observe that there could be any number of “Employee” elements within “Employee.”
You should also observe that “Name” and “Age” are defined with “simple types” rather than with “complex types.” The only complex elements are “Employees” and “Employee.” The most important declaration within the above schema is as follows:
The above statement makes sure that every “Employee” element is provided with the attribute “ID” (which is of type “int”).
Now, how would the XML document look when it conformed to the above XML Schema? The following would be a sample XML document which conforms to the XML Schema above.