Вы находитесь на странице: 1из 50

XP

New Perspectives on XML, 2nd Edition


Tutorial 4
1

TUTORIAL 4
WORKING WITH SCHEMAS

XP
New Perspectives on XML, 2nd Edition
Tutorial 4
2
SCHEMAS
A schema is an XML document that defines the
content and structure of one or more XML
documents.

The XML document containing the content is
called the instance document.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
3
COMPARING SCHEMAS
AND DTDS
This figure compares schemas and DTDs
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
4
SCHEMA VOCABULARIES
There is no single schema form. Several schema
vocabularies have been developed in the XML
language.

Support for a particular schema depends on the
XML parser being used for validation.

XP
New Perspectives on XML, 2nd Edition
Tutorial 4
5
SCHEMA VOCABULARIES
This figure shows a few schema vocabularies
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
6
STARTING A SCHEMA FILE
A schema is always placed in a separate XML
document that is referenced by the instance
document.

XP
New Perspectives on XML, 2nd Edition
Tutorial 4
7
ELEMENTS AND ATTRIBUTES OF
THE PATIENTS DOCUMENT
This figure shows the elements and attributes of the patients.xml document
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
8
SCHEMA TYPES
XML Schema recognize two categories of element
types: complex and simple.

A complex type element has one or more
attributes, or is the parent to one or more child
elements.

A simple type element contains only character
data and has no attributes.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
9
SCHEMA TYPES
This figure shows types of elements
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
10
SIMPLE TYPE ELEMENTS
Use the following syntax to declare a simple type element
in XML Schema:

<element name=name type =type/>

Here, name is the name of the element in the instance
document and type is the data type of the element.

If a namespace prefix is used with the XML Schema
namespace, any XML Schema tags must be qualified with
the namespace prefix.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
11
UNDERSTANDING DATA TYPES
XML Schema supports two data types: built-in and user-
derived.

A built-in data type is part of the XML Schema
specifications and is available to all XML Schema authors.

A user-derived data type is created by the XML Schema
author for specific data values in the instance document.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
12
DECLARING AN ATTRIBUTE
An attribute is another example of a simple type. The
syntax to define an attribute is
<xs:attribute name="name" type="type
default="default fixed="fixed" />

Where name is the name of the attribute, type is the
data type, default is the attributes default value, and
fixed is a fixed value for the attribute.

XP
New Perspectives on XML, 2nd Edition
Tutorial 4
13
ASSOCIATING ATTRIBUTES
AND ELEMENTS
The basic structure for defining a complex type element
with XML Schema is
<xs:element name="name">
<xs:complexType>
declarations
</xs:complexType>
</xs:element>
Where name is the name of the element and declarations is
schema commands specific to the type of complex element
being defined.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
14
ASSOCIATING ATTRIBUTES
AND ELEMENTS
Four complex type elements that usually appear in an
instance document are the following:
The element is an empty element and contains only
attributes.
The element contains textual content and attributes but
no child elements.
The element contains child elements but not attributes.
The element contains both child elements and
attributes.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
15
EMPTY ELEMENTS
AND ATTRIBUTES
The code to declare the attributes of an empty element
is
<xs:element name="name">
<xs:complexType>
attributes
</xs:complexType>
</xs:element>
Where attributes is the set of declarations that define
the attributes associated with the element. For
example, the empty element
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
16
SIMPLE CONTENT
AND ATTRIBUTES
If an element is not empty and contains textual content (but no
child elements), the structure of the complex type element is
slightly different.
<xs:element name="name">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="type">
attributes
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
17
SPECIFYING THE USE OF
AN ATTRIBUTE
An attribute may or may not be required with a particular
element. To indicate whether an attribute is required, you
add the use attribute to the element declaration or
reference. The use attribute has the following values:
requiredThe attribute must always appear with the
element
optionalThe use of the attribute is optional with the
element
prohibitedThe attribute cannot be used with the
element
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
18
REFERENCING AN ELEMENT
OR ATTRIBUTE
XML Schema allows for a great deal of flexibility in
designing complex types. Rather than nesting the attribute
declaration within the element, you can create a reference
to it. The code to create a reference to an element or
attribute declaration is
<xs:element ref="elemName" />
<xs:attribute ref="attName" />
Where elemName is the name used in an element
declaration and attName is the name used in an attribute
declaration
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
19
WORKING WITH
CHILD ELEMENTS
Another kind of complex type element contains child elements,
but no attributes. To define these child elements, use the code
structure
<xs:element name="name">
<xs:complexType>
<xs:compositor>
elements
</xs:compositor>
</xs:complexType>
</xs:element>
Where elements is the list of simple type element declarations
for each child element, and compositor defines how the child
elements are organized.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
20
USING COMPOSITORS
XML Schema supports the following compositors:
sequence defines a specific order for the child
elements
choice allows any one of the child elements to
appear in the instance document
all allows any of the child elements to appear in
any order in the instance document; however, they
must appear either only once or not all.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
21
WORKING WITH CHILD
ELEMENTS AND ATTRIBUTES
The code for a complex type element that contains
both attributes and child elements is
<xs:element name="name">
<xs:complexType>
<xs:compositor>
elements
</xs:compositor>
</xs:complexType>
attributes
</xs:element>
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
22
SPECIFYING MIXED CONTENT
When the mixed attribute is set to the value true, XML Schema assumes that the
element contains both text and child elements. The structure of the child elements can
then be defined with the conventional method. For example, the XML content
<Summary>
Patient <Name>Cynthia Davis</Name> was enrolled in
the <Study>Tamoxifen Study</Study> on 8/15/2003.
</Summary>

can be declared in the schema file using the following complex type:

<element name="Summary">
<complexType mixed="true">
<sequence>
<element name="Name" type="string"/>
<element name="Study" type="string"/>
</sequence>
</complexType>
</element>

XP
New Perspectives on XML, 2nd Edition
Tutorial 4
23
APPLYING A SCHEMA
To attach a schema to the document, you must do the
following:
Declare a namespace for XML Schema in the instance
document.
Indicate the location of the schema file.
To declare the XML Schema namespace in the instance
document, you add the following attribute to the
documents root element:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
24
APPLYING A SCHEMA
If there is no namespace for the contents of the
instance document, add the following attribute to
the root element:
xsi:noNamespaceSchemaLocation="schema"

XP
New Perspectives on XML, 2nd Edition
Tutorial 4
25
UNDERSTANDING DATA TYPES
A primitive data type, also called a base type, is
one of 19 fundamental data types not defined in
terms of other types.

A derived data type is a collection of 25 data
types that the XML Schema developers created
based on the 19 primitive types.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
26
UNDERSTANDING DATA TYPES
This figure shows the 44 built-in data types
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
27
UNDERSTANDING DATA TYPES
This figure shows a partial description of XML string data types
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
28
UNDERSTANDING DATA TYPES
This figure shows a partial description of XML numeric data types
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
29
UNDERSTANDING DATA TYPES
This figure shows a partial description of XML date and time data types
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
30
DERIVING NEW DATA TYPES
Three components are involved in deriving new
data types:
Value space: the set of values that correspond
to the data type.
Lexical space: the set of textual representations
of the value space.
Facets: the properties of the data type that
distinguish one data type from another.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
31
USER DERIVED DATA
New data types fall into three categories:
List: a list of values where each list is derived
from a base type.
Union: the combination of two or more data
types.
Restriction: a limit placed on the facet of a
base type.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
32
DERIVING A RESTRICTED
DATA TYPE
The most common way to derive a new data type
is to restrict the properties of a base type. XML
Schema provides twelve constraining facets for
this purpose.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
33
CONSTRAINING FACETS
This figure shows the 12 constraining facets
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
34
The Patterns Facet
A pattern can be created with a formatted text string called a
regular expression or regex.
To apply a regular expression in a data type, you use the code
<xs:simpleType name="name">
<xs:restriction base="type">
<xs:pattern value="regex"/>
</xs:restriction>
</xs:simpleType>
Where regex is a regular expression pattern.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
35
PATTERN QUANTIFIERS
This figure shows pattern quantifiers
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
36
WORKING WITH NAMED TYPES
Since content can be either simple or complex, it is not surprising that XML
Schema also allows schema authors to create customized complex types.
The advantage of creating a complex type is that the complex structure can be
reused in the document.
For example, the following code declares an element named client containing
the complex content of two child elements named firstName and lastName:
<xs:element name="client">
<xs:complexType>
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
37
NAMED MODEL GROUPS
A named model group is a collection, or group,
of elements. The syntax for creating a model
group is
<xs:group name="name">
elements
</xs:group>
Where name is the name of the model group, and
elements is a collection of element declarations

XP
New Perspectives on XML, 2nd Edition
Tutorial 4
38
WORKING WITH NAMED
ATTRIBUTE GROUPS
Attributes can be grouped into collections called named
attribute groups.
This is particularly useful for attributes that you want to
use with several different elements in a schema. The
syntax for a named attribute group is
<xs:attributeGroup name="name">
attributes
</xs:attributeGroup>
Where name is the name of the attribute group and
attributes is a collection of attributes assigned to the group.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
39
STRUCTURING A SCHEMA
One schema design is a Flat Catalog Design.
In this design, all element declarations are made
globally.
The structure of the instance document is created
by referencing the global element declarations.
The syntax is:

<element ref=name>
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
40
FLAT CATALOG DESIGN
This figure shows a Flat Catalog design
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
41
STRUCTURING A SCHEMA
Schemas can be structured in a number of ways.
One structure is called a Russian Doll design.
This design involves sets of nested declarations.

While this design makes it easy to associate the
schema with the instance document, it can be
confusing and difficult to maintain.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
42
RUSSIAN DOLL DESIGN
This figure shows a Russian Doll design
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
43
VENETIAN BLIND DESIGN
A Venetian blind design is similar to a flat catalog,
except that instead of declaring elements and
attributes globally, it creates named types and
references those types within a single global element

In this layout, the only globally declared element is
the patients element; all other elements and attributes
are placed within element or attribute groups or, in the
case of the performance element, within a named
complex type
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
44
VENETIAN BLIND DESIGN
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
45
COMPARING SCHEMA DESIGNS
This figure compares the three schema designs
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
46
PLACING A SCHEMA IN A
NAMESPACE: TARGETING A
NAMESPACE
To associate a schema with a namespace, you first
declare the namespace and then make that namespace
the target of the schema. To do this, you add the
following attributes to the schemas root element:
prefix:xmlns="uri"
targetNamespace="uri"
Where prefix is the prefix of the XML Schema
namespace and uri is the URI of the target namespace
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
47
VALIDATING A COMBINED
DOCUMENT
This figure shows how schemas are combined when the data is combined
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
48
APPLYING A SCHEMA TO A
DOCUMENT WITH A NAMESPACE
To apply a schema to a document with a namespace, add
the following attributes to the instance documents root
element:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="uri schema"
Where uri is the URI of the namespace and schema is the
location and name of the schema file.
All global elements and attributes from the schema must be
qualified in the instance document.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
49
INCLUDING AND IMPORTING
SCHEMAS
To include a schema from the same namespace, add the
following element as a child of the schema element:
<xs:include schemaLocation="schema" />
Where schema is the name and location of the schema file.
To import a schema from a different namespace, use the
syntax
<xs:import namespace="uri" schemaLocation="schema" />
Where uri is the URI of the imported schemas namespace
and schema is the name and location of the schema file.
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
50
REFERENCING OBJECTS FROM
OTHER SCHEMAS
Once a schema is imported, any objects it contains
with global scope can be referenced
To reference an object from an imported schema, you
must declare the namespace of the imported schema in
the schema element
You can then reference the object using the ref
attribute or the type attribute for customized simple
and complex types

Вам также может понравиться