XML Schemas help you control what elements appear in your XML documents. Similar to Document Type Definitions, they offer distinct advantages over the older syntax. Dan Wellman provides a basic overview, with examples to get you started working on your own.
An Introduction to XML Schemas - Element Declarations (Page 3 of 7 )
Next come the element declarations, which list the elements that must appear, may appear and in which order they appear. If you’ve read my Introduction to XML article, the news story xml document will be familiar to you; if you haven’t read it, the XML Schema document we’ll create will define an XML document consisting of a root element of <news> containing multiple child elements called <article> which in turn have child elements called <headline> and <story>.
When it comes to element declarations in schema documents, you can create either simple or complex definitions. Simple definitions declare elements that do not have any children or attributes and can only contain text, while complex definitions declare elements that can have children and attributes as well as text.
For our news document then, we will need to define the root element directly after the XML declaration. Add the following code:
What this markup does is declare the root element of the XML document with a complex type definition. As <news> is the root element, it will clearly contain other elements, in this case, the <article> element, which is declared using the <element ref> element. The value of this element must be the same as the value of the name attribute in each child elements declaration. You’ll see what I mean in the next section of code. The <sequence> declaration also dictates the order that the XML elements must appear in.
You’ll notice also that each element has a prefix of xsd: which is in accordance with the schema and namespace declaration.
Declare Your Children
You declare child elements using sequences which specify which elements should appear, in the order that they appear. The maxOccurs attribute value of unbounded tells us that it can appear one to many times, with no upper limit. If this wasn’t specified, the default value for this and the minOccurs attribute is one.
The sequence element is what is known as a compositor, which is an element that contains references to other elements. In addition to the <sequence> compositor, there are also <choice> and <all> compositors; choice compositors give a choice of element references, any one of which may be used, and all compositors declare that any or all of the referenced elements may occur.
We now need another complex definition for the <article> element, as this element will also contain child elements:
As we only want one headline and one story per article, we set both the min and max Occurs values to 1.
We’ve now declared our two bottom level child elements. As both the <headline> and <story> elements in the XML document contain only text, and no other elements or attributes, we can declare both of these as simple definitions directly below the above code: