Java Reference
In-Depth Information
<!--This defines an element type that is used in the declaration of content. -->
<xsd:complexType name="AddressType">
<xsd:sequence>
<xsd:element name="buildingnumber" type="xsd:positiveInteger"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
You might like to contrast this schema with the DTD you saw earlier that defined XML documents with
similar content. This schema defines documents that consist of an
<address/>
root element that contains a
sequence of child elements with the names
buildingnumber
,
street
,
city
,
state
, and
zip
.
The root element in the schema definition is the
xsd:schema
element, and that has an attribute with the
name
xmlns
that identifies an XML namespace. The value you specify for the
xmlns
attribute is a URI
that is the namespace name for the content document, and because the current document is a schema, the
namespace is the one corresponding to elements in the XML Schema Definition language. The
xsd
that
follows the colon is the prefix that is used to identify element names from the
“
http://www.w3.org/2001/
sequence
, and
element
are all names of elements in a namespace defined for the XML Schema Definition
language. The root element for every XML Schema is a
schema
element. Don't lose sight of what a schema
is; it's a definition of the form of XML documents of a particular type, so it declares the elements that can be
used in such a document and how they may be structured. A document that conforms to a particular schema
does not have to identify the schema, but it can. I come back to how you reference a schema when you are
defining an XML document a little later in this chapter.
The example uses an
<annotation/>
element to include some simple documentation in the schema
definition. The text that is the documentation appears within a child
<documentation/>
element. You can
also use an
<appInfo/>
child element within an
<annotation/>
element to reference information located
at a given URI. Of course, you can also use XML comments,
<!--comment-->
, within a schema, as the
example shows.
In an XML Schema, a
declaration
specifies an element that is content for a document, whereas a
defini-
tion
defines an element type. The
xsd:element
element is a declaration that the content of a document con-
sists of an
<address/>
element. Contrast this with the
xsd:complexType
element, which is a definition of
the
AddressType
type for an element and does not declare document content. The
xsd:element
element in
the schema declares that the
address
element is document content and happens to be of type
AddressType
,
which is the type defined by the
xsd:complexType
element.
Now let's take a look at some of the elements that you use to define a document schema in a little more
detail.
Defining Schema Elements
As I said, the
xsd:complexType
element in the sample schema defines a type of element, not an element in
the document. A complex element is simply an element that contains other elements, or that has attributes,
or both. Any elements that are complex elements need an
xsd:complexType
definition in the schema to
define a type for the element. You place the definitions for child elements for a complex element between