• No results found

Declaring an Element Using a Built-In Simple Type

In document XML Step by Step, Second Edition (Page 146-150)

To declare an element with a built-in simple type, assign the name of that type to the type attribute in the xsd:element start-tag. For instance, in the example schema of Listing 7-1, the TITLE element is assigned the xsd:string built-in simple type, which allows the element to contain any sequence of legal XML characters:

<xsd:element name=”TITLE” type=”xsd:string”/>

Of the built-in types, xsd:string is the least restrictive. Table 7-1 describes a sam-pling of other useful built-in simple types that you can assign to the elements you declare. For a complete list of these types, some of which are fairly intricate, see the section “2.3 Simple Types” in the “XML Schema Part 0: Primer” page at http://www.w3.org/TR/xmlschema-0/.

Built-in Description Example(s)

simple type

xsd:string A sequence of any of This is a string.

the legal XML characters xsd:boolean The value true or false, true

or 1 or 0 (indicating true false or false, respectively) 1

0 xsd:decimal A number that may cont- -5.2

ain a decimal component -3.0 1 2.5

170 XML Step by Step

Built-in Description Example(s)

simple type

xsd:time A time of day, represented 11:30:00.00 (11:30 A.M.) as hh:mm:ss.ss 14:29:03 (2:29 P.M. and

3 seconds)

05:16:00.0 (5:16 A.M.) xsd:dateTime A date and time of day, 1948-05-21T17:28:00.00

represented as CCYY-MM-DD Thh:mm:ss.ss

xsd:gMonth A Gregorian calendar --05-- (May) month, represented as --12-- (December)

--MM--xsd:gYear A Gregorian calendar year, 1948 represented as CCYY 2001 xsd:gDay A day of a Gregorian ---05 calendar month, ---31 represented as ---DD

xsd:gYearMonth A Gregorian calendar year 1948-05 (May, 1948) and month, represented as

CCYY-MM

xsd:anyURI A URI (Uniform Resource http://www.mjyOnline.com Identifier; see the sidebar

“URIs, URLs, and URNs”

on page 73)

Table 7-1.Useful built-in simple types you can use for declaring elements or attributes.

You can control the number of occurrences of the element within the context where it is declared by including the minOccurs attribute (the minimum number of occurrences), the maxOccurs attribute (the maximum number of occur-rences), or both attributes. The default value of each of these attributes is 1, so if you omit them, the element must appear exactly once in the context where it’s declared.

note

It’s an error for the minOccurs value to be greater than the maxOccurs value.

continued

Chapter 7 Creating Valid XML Documents Using XML Schemas 171

7XML Schemas

For instance, in the example schema of Listing 7-1, each of the elements TITLE, AUTHOR, BINDING, PAGES, and PRICE must occur exactly once as a child of the BOOK element (and as you’ll learn later in the chapter, these elements must occur in the order in which they are declared). You can assign either attribute an integer greater than or equal to zero. You can also assign maxOccurs the value unbounded, which means the element can occur an unlimited number of times.

For example, the following element is declared as optional—that is, it can ap-pear once or not at all:

<xsd:element name=”PUBLISH_DATE” type=”xsd:gYearMonth”

minOccurs=”0"/>

And, the following element can be included any number of times, or it can be omitted:

<xsd:element name=”AUTHOR” type=”xsd:string” minOccurs=”0"

maxOccurs=”unbounded”/>

note

You can’t use the minOccurs or maxOccurs attribute with the declaration of the document element, which must occur exactly once.

note

For a description of additional xsd:element attributes you can use when declar-ing an element, see the section “3.3.2 XML Representation of Element Decla-ration Schema Components” in the “XML Schema Part 1: Structures” page at http://www.w3.org/TR/xmlschema-1/.

172 XML Step by Step

<xsd:element name=”PRICE” type=”xsd:decimal”>

Because the xsd:decimal type would allow the element to contain values such as -10.50 and 5000, you might want to define a new type for the BOOK element that restricts the values to a reasonable range. You could do this with the

following declaration:

<xsd:element name=”PRICE”>

<xsd:simpleType>

<xsd:restriction base=”xsd:decimal”>

<xsd:minExclusive value=”0"/>

<xsd:maxExclusive value=”100"/>

</xsd:restriction>

</xsd:simpleType>

</xsd:element>

This declaration assigns the PRICE element a new defined type that is derived from the built-in type xsd:decimal. The new type has all the features of

xsd:decimal except that the value entered into the element must be greater than 0 and less than 100.

You always define a new simple type using the xsd:simpleType schema element.

You can simultaneously define the type and assign it to the element you’re de-claring by including the xsd:simpleType element inside the xsd:element element and omitting the type attribute from xsd:element, as done in the example PRICE declaration given above. (For an alternative way to define a type, see the sidebar

“Anonymous vs. Named Types,” later in this section.)

The most common way to define a simple type is to start with a built-in type and restrict its possible values in various ways. You do this by including the xsd:restriction element within the xsd:simpleType element, as in the example PRICE declaration shown above. The xsd:restriction element specifies the base type (that is, the starting type) and includes special schema elements known as facets, which indicate the precise way the base type is to be restricted. (For alter-natives to the xsd:restriction element, see the Tip at the end of this section.) The example PRICE element declaration uses the xsd:minExclusive and xsd:maxExclusive facets to indicate a permissible range of values that doesn’t include the specified end values (0 and 100). To indicate a range that does in-clude the specified end values, you can use the similar xsd:minInclusive and xsd:maxInclusive facet elements.

Chapter 7 Creating Valid XML Documents Using XML Schemas 173

7XML Schemas

In document XML Step by Step, Second Edition (Page 146-150)