Java Reference
In-Depth Information
Sometimes several different elements have the same set of attributes. To avoid having to repeat the defini-
tions for the elements in such a set for each element that requires them, you can define an attribute group.
Here's an example of a definition for an attribute group:
<xsd:attributeGroup name="coords">
<xsd:attribute name="x" type="xsd:integer" use="required"/>
<xsd:attribute name="y" type="xsd:integer" use="required"/>
</xsd:attributeGroup>
This defines a group of two attributes with names
x
and
y
that specify
x
and
y
coordinates for a point. The
name of this attribute group is
coords
. In general, an attribute group can contain other attribute groups. You
could use the
coords
attribute group within a complex type definition like this:
<xsd:complexType name="PointType">
<xsd:attributeGroup ref="coords"/>
</xsd:complexType>
This defines the element type
PointType
as having the attributes that are defined in the
coords
attribute
group. The
ref
attribute in the
xsd:attributeGroup
element specifies that this is a reference to a named
attribute group. You can now use the
PointType
element type to define elements. For example:
<xsd:element name="position" type="PointType"/>
This declares a
<point/>
element to be of type
PointType
, and thus have the required attributes
x
and
y
.
Specifying a Group of Element Choices
The
xsd:choice
element in the Schema Definition language enables you to specify that one out of a given
set of elements included in the choice definition must be present. This is useful in specifying a schema for
Sketcher documents because the content is essentially variable — it can be any sequence of any of the basic
types of elements. Suppose that you have already defined types for the geometric and text elements that can
occur in a sketch. You could use an
xsd:choice
element in the definition of a complex type for a
<sketch/>
element:
<xsd:complexType name="SketchType">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="line" type="LineType"/>
<xsd:element name="rectangle" type="RectangleType"/>
<xsd:element name="circle" type="CircleType"/>
<xsd:element name="curve" type="CurveType"/>
<xsd:element name="text" type="TextType"/>
</xsd:choice>
</xsd:complexType>
This defines that an element of type
SketchType
contains zero or more elements that are each one of
the five types identified in the
xsd:choice
element. Thus, each element can be any of the types
LineType
,
RectangleType
,
CircleType
,
CurveType
, or
TextType
, which are types for the primitive elements in a
sketch that are defined elsewhere in the schema. Given this definition for
SketchType
, you can declare the
content for a sketch to be the following: