Java Reference
In-Depth Information
fieselementshavingasimplecontentmodelandattributes,orelementsfromothercon-
tentmodelsascomplextypes.Furthermore,XMLSchemaclassifiesattributesassimple
typesbecausetheyonlycontaintextvalues—attributesdon'thavechildelements. List-
ing 10-1 ' s title and instructions elements, and its qty attribute are simple
types. Its recipe , ingredients , and ingredient elements are complex types.
At this point, we can begin to declare the schema. The following example presents
the introductory schema element:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
The schema elementintroducesthegrammar.Italsoassignsthecommonlyused xs
namespace prefix to the standard XML Schema namespace; xs: is subsequently pre-
pended to XML Schema element names.
Next, we use the element element to declare the title and instructions
simple type elements, as follows:
<xs:element name="title" type="xs:string"/>
<xs:element name="instructions" type="xs:string"/>
XMLSchemarequiresthateachelementhaveanameand(unlikeDTD)beassociated
with a type, which identifies the kind of data stored in the element. For example, the
first element declaration identifies title as the name via its name attribute and
string asthetypeviaits type attribute(stringorcharacterdataappearsbetweenthe
<title> and </title> tags). The xs: prefix in xs:string is required because
string is a predefined W3C type.
Continuing,wenowusethe attribute elementtodeclarethe qty simpletypeat-
tribute, as follows:
<xs:attribute
name="qty"
type="xs:unsignedInt"
de-
fault="1"/>
This attribute element declares an attribute named qty . I've chosen un-
signedInt as this attribute's type because quantities are nonnegative values. Fur-
thermore,I'vespecified 1 asthe default valueforwhen qty isnotspecified— at-
tribute elements default to declaring optional attributes.
Note The order of element and attribute declarations is not significant within a
schema.
Nowthatwe'vedeclaredthesimpletypes,wecanstarttodeclarethecomplextypes.
To begin, we'll declare recipe , as follows:
Search WWH ::




Custom Search