Java Reference
In-Depth Information
the
complexType
start and end tags. You also place the definitions for any attributes for a complex element
between the
complexType
start and end tags. You can define a simple type using an
xsd:simpleType
defin-
ition in the schema. You would use a simple type definition to constrain attribute values or element content
in some way. You see examples of this a little later in this chapter.
In the example you specify that any element of type
AddressType
contains a sequence of
simpleelements
— a
buildingnumber
element, a
street
element, a
city
element, a
state
element, and a
zip
element. A
simple element is an element that does not have child elements or attributes; it can contain only data, which
can be of a variety of standard types or of a type that you define. The definition of each simple element that
appears within an element of type
AddressType
uses an
xsd:element
element in which the
name
attribute
specifies the name of the element being defined and the
type
attribute defines the type of data that can ap-
pear within the element.
You can also control the number of occurrences of an element by specifying values for two further attrib-
TABLE 22-4
:
Attributes Specifying the Number of Element Occurrences
ATTRIBUTE DESCRIPTION
minOccurs
The value defines the minimum number of occurrences of the element and must be a positive integer
(which can be 0). If this attribute is not defined, then the minimum number of occurrences of the ele-
ment is 1.
The value defines the maximum number of occurrences of the element and can be a positive integer or
the value unbounded. If this attribute is not defined, then the maximum number of occurrences of the
element is 1.
maxOccurs
Thus, if both of these attributes are omitted, as is the case with the child element definitions in the sample
schema for elements of type
AddressType
, the minimum and maximum numbers of occurrences are both
one, so the element must appear exactly once. If you specify
minOccurs
as 0, then the element is option-
al. Note that you must not specify a value for
minOccurs
that is greater than
maxOccurs
, and the value for
maxOccurs
must not be less than
minOccurs
. You should keep in mind that both attributes have default val-
ues of 1 when you specify a value for just one attribute.
Specifying Data Types
In the example, each of the definitions for the five simple elements within an address element has a type
specified for the data that it contains, and you specify the data type by a value for the
type
attribute. The
data in a
buildingnumber
element is specified to be of type
positiveInteger
, and the others are all of
type
string
. These two types are relatively self-explanatory, corresponding to positive integers greater than
or equal to 0, and strings of characters. The XML Schema Definition language enables you to specify many
different values for the
type
attribute in an
element
definition. Here are a few other examples:
DATA TYPE EXAMPLES OF DATA
integer
25, -998, 12345, 0, -1
negativeInteger
-1, -2, -3, -12345, and so on
nonNegativeInteger
0, 1, 2, 3, and so on
hexBinary
0DE7, ADD7
25, 123456789, -9999999
long
2.71828, 5E5, 500.0, 0, -3E2, -300.0, NaN, INF, -INF
float
3.14159265, 1E30, -2.5, NaN, -INF, INF
double
true, false, 1, 0
boolean