Java Reference
In-Depth Information
Restrictions on Values
You can place restrictions on values for element content and element attributes. Such restrictions are referred
to as facets . Quite often you want to restrict the values that can be assigned to attributes. For example, the
diameter of a circle certainly cannot be zero or negative, and a color may be restricted to standard colors.
You could do this by adding a simple type definition that defines the restrictions for these values. For ex-
ample:
<xsd:complexType name="circle">
<xsd:attribute name="x" type="xsd:integer" use="required"/>
<xsd:attribute name="y" type="xsd:integer" use="required"/>
<xsd:attribute name="diameter" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:double">
<xsd:minExclusive value="1.0"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="color" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="red"/>
<xsd:enumeration value="blue"/>
<xsd:enumeration value="green"/>
<xsd:enumeration value="yellow"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
The diameter and color attributes have facets that specify the values that are acceptable. The sim-
pleType element that appears within the xsd:attribute elements specifies the constraints on the values for
each attribute. You can also use the simpleType element with an xsd:element element definition to con-
strain the content for an element in a similar way. The xsd:restriction element defines the constraints,
and you have a considerable range of options for specifying them, many more than I can possibly explain
here. The base attribute in the xsd:restriction element defines the type for the value that is being restric-
ted, and this attribute specification is required.
I've used an xsd:minExclusive specification to define an exclusive lower limit for values of the dia-
meter attribute, and this specifies that the value must be greater than "1.0." Alternatively, you might prefer
to use xsd:minInclusive with a value of "2.0" to set a sensible minimum value for the diameter. You
also have the option of specifying an upper limit on numerical values by specifying either maxInclusive
or maxExclusive values. For the color attribute definition, I've introduced a restriction that the value must
be one of a fixed set of values. Each value that is allowed is specified in an xsd:enumeration element, and
there can be any number of these. Obviously, this doesn't just apply to strings; you can restrict the values
for numeric types to be one of an enumerated set of values. For the color attribute the value must be one of
the four string values specified.
Defining Groups of Attributes
Search WWH ::




Custom Search