xml tutorial.04
DESCRIPTION
XML Tutorial.04TRANSCRIPT
-
5/20/2018 XML Tutorial.04
1/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
1
TUTORIAL 4
WORKING WITH SCHEMAS
-
5/20/2018 XML Tutorial.04
2/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
2
SCHEMAS
A schemais 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.
-
5/20/2018 XML Tutorial.04
3/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
3
COMPARING SCHEMAS
AND DTDS
This figure compares schemas and DTDs
-
5/20/2018 XML Tutorial.04
4/50
XP
New Perspectives on XML, 2nd EditionTutorial 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.
-
5/20/2018 XML Tutorial.04
5/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
5
SCHEMA VOCABULARIES
This figure shows a few schema vocabularies
-
5/20/2018 XML Tutorial.04
6/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
6
STARTING A SCHEMA FILE
A schema is always placed in a separate XML
document that is referenced by the instance
document.
-
5/20/2018 XML Tutorial.04
7/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
7
ELEMENTS AND ATTRIBUTES OF
THE PATIENTS DOCUMENT
This figure shows the elements and attributes of the patients.xml document
-
5/20/2018 XML Tutorial.04
8/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
8
SCHEMA TYPES
XML Schema recognize two categories of elementtypes: complex and simple.
A complextypeelement has one or moreattributes, or is the parent to one or more childelements.
A simpletype element contains only characterdata and has no attributes.
-
5/20/2018 XML Tutorial.04
9/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
9
SCHEMA TYPES
This figure shows types of elements
-
5/20/2018 XML Tutorial.04
10/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
10
SIMPLE TYPE ELEMENTS
Use the following syntax to declare a simple type elementin XML Schema:
Here, nameis the name of the element in the instancedocument and type is the data type of the element.
If a namespace prefix is used with the XML Schemanamespace, any XML Schema tags must be qualified withthe namespace prefix.
-
5/20/2018 XML Tutorial.04
11/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
11
UNDERSTANDING DATA TYPES
XML Schema supports two data types: built-in and user-
derived.
A built-in data typeis part of the XML Schema
specifications and is available to all XML Schema authors.
A user-derived data typeis created by the XML Schemaauthor for specific data values in the instance document.
-
5/20/2018 XML Tutorial.04
12/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
12
DECLARING AN ATTRIBUTE
An attribute is another example of a simple type. The
syntax to define an attribute is
Where name is the name of the attribute, type is the
data type, default is the attributes default value, andfixed is a fixed value for the attribute.
-
5/20/2018 XML Tutorial.04
13/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
13
ASSOCIATING ATTRIBUTES
AND ELEMENTS
The basic structure for defining a complex type elementwith XML Schema is
declarations
Where name is the name of the element and declarations isschema commands specific to the type of complex element
being defined.
-
5/20/2018 XML Tutorial.04
14/50
XP
New Perspectives on XML, 2nd EditionTutorial 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.
-
5/20/2018 XML Tutorial.04
15/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
15
EMPTY ELEMENTS
AND ATTRIBUTES
The code to declare the attributes of an empty elementis
attributes
Where attributes is the set of declarations that define
the attributes associated with the element. Forexample, the empty element
-
5/20/2018 XML Tutorial.04
16/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
16
SIMPLE CONTENT
AND ATTRIBUTES
If an element is not empty and contains textual content (but nochild elements), the structure of the complex type element isslightly different.
attributes
-
5/20/2018 XML Tutorial.04
17/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
17
SPECIFYING THE USE OF
AN ATTRIBUTE
An attribute may or may not be required with a particularelement. To indicate whether an attribute is required, youadd 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 theelement
prohibitedThe attribute cannot be used with theelement
-
5/20/2018 XML Tutorial.04
18/50
XP
New Perspectives on XML, 2nd EditionTutorial 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 orattribute declaration is
Where elemName is the name used in an element
declaration and attName is the name used in an attribute
declaration
-
5/20/2018 XML Tutorial.04
19/50
XP
New Perspectives on XML, 2nd EditionTutorial 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 codestructure
elements
Where elements is the list of simple type element declarationsfor each child element, and compositor defines how the childelements are organized.
-
5/20/2018 XML Tutorial.04
20/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
20
USING COMPOSITORS
XML Schema supports the following compositors:
sequence defines a specific order for the child
elementschoice 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, theymust appear either only once or not all.
-
5/20/2018 XML Tutorial.04
21/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
21
WORKING WITH CHILD
ELEMENTS AND ATTRIBUTES
The code for a complex type element that containsboth attributes and child elements is
elements
attributes
-
5/20/2018 XML Tutorial.04
22/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
22
SPECIFYING MIXED CONTENT
When the mixed attribute is set to the value true, XML Schema assumes that theelement contains both text and child elements. The structure of the child elements canthen be defined with the conventional method. For example, the XML content
Patient Cynthia Davis was enrolled in
the Tamoxifen Study on 8/15/2003.
can be declared in the schema file using the following complex type:
-
5/20/2018 XML Tutorial.04
23/50
XP
New Perspectives on XML, 2nd EditionTutorial 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"
-
5/20/2018 XML Tutorial.04
24/50
XP
New Perspectives on XML, 2nd EditionTutorial 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"
-
5/20/2018 XML Tutorial.04
25/50
XP
New Perspectives on XML, 2nd EditionTutorial 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 typeis a collection of 25 data
types that the XML Schema developers created
based on the 19 primitive types.
-
5/20/2018 XML Tutorial.04
26/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
26
UNDERSTANDING DATA TYPES
This figure shows the 44 built-in data types
-
5/20/2018 XML Tutorial.04
27/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
27
UNDERSTANDING DATA TYPES
This figure shows a partial description of XML string data types
-
5/20/2018 XML Tutorial.04
28/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
28
UNDERSTANDING DATA TYPES
This figure shows a partial description of XML numeric data types
-
5/20/2018 XML Tutorial.04
29/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
29
UNDERSTANDING DATA TYPES
This figure shows a partial description of XML date and time data types
-
5/20/2018 XML Tutorial.04
30/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
30
DERIVING NEW DATA TYPES
Three components are involved in deriving new
data types:
Value space: the set of values that correspondto 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.
-
5/20/2018 XML Tutorial.04
31/50
XP
New Perspectives on XML, 2nd EditionTutorial 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.
-
5/20/2018 XML Tutorial.04
32/50
XP
New Perspectives on XML, 2nd EditionTutorial 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 forthis purpose.
-
5/20/2018 XML Tutorial.04
33/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
33
CONSTRAINING FACETS
This figure shows the 12 constraining facets
-
5/20/2018 XML Tutorial.04
34/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
34
The Patterns Facet
A pattern can be created with a formatted text string called a
regular expression orregex.
To apply a regular expression in a data type, you use the code
Where regex is a regular expression pattern.
-
5/20/2018 XML Tutorial.04
35/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
35
PATTERN QUANTIFIERS
This figure shows pattern quantifiers
-
5/20/2018 XML Tutorial.04
36/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
36
WORKING WITH NAMED TYPES
Since content can be either simple or complex, it is not surprising that XMLSchema also allows schema authors to create customized complex types.
The advantage of creating a complex type is that the complex structure can bereused in the document.
For example, the following code declares an element named client containingthe complex content of two child elements named firstName and lastName:
-
5/20/2018 XML Tutorial.04
37/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
37
NAMED MODEL GROUPS
A named model group is a collection, or group,
of elements. The syntax for creating a model
group is
elements
Where name is the name of the model group, and
elements is a collection of element declarations
-
5/20/2018 XML Tutorial.04
38/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
38
WORKING WITH NAMED
ATTRIBUTE GROUPS
Attributes can be grouped into collections called namedattribute groups.
This is particularly useful for attributes that you want to
use with several different elements in a schema. Thesyntax for a named attribute group is
attributes
Where name is the name of the attribute group and
attributes is a collection of attributes assigned to the group.
-
5/20/2018 XML Tutorial.04
39/50
XP
New Perspectives on XML, 2nd EditionTutorial 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:
-
5/20/2018 XML Tutorial.04
40/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
40
FLAT CATALOG DESIGN
This figure shows a Flat Catalog design
-
5/20/2018 XML Tutorial.04
41/50
XP
New Perspectives on XML, 2nd EditionTutorial 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.
-
5/20/2018 XML Tutorial.04
42/50
XP
New Perspectives on XML, 2nd EditionTutorial 4
42
RUSSIAN DOLL DESIGN
This figure shows a Russian Doll design
-
5/20/2018 XML Tutorial.04
43/50
XP
New Perspectives on XML, 2nd EditionTutorial 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 attributesare placed within element or attribute groups or, in the
case of the performance element, within a named
complex type
-
5/20/2018 XML Tutorial.04
44/50
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
44
VENETIAN BLIND DESIGN
-
5/20/2018 XML Tutorial.04
45/50
XP
New Perspectives on XML, 2nd Edition
Tutorial 4
45
COMPARING SCHEMA DESIGNS
This figure compares the three schema designs
-
5/20/2018 XML Tutorial.04
46/50
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"
Whereprefix is the prefix of the XML Schemanamespace and uri is the URI of the target namespace
-
5/20/2018 XML Tutorial.04
47/50
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
-
5/20/2018 XML Tutorial.04
48/50
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 andschema is the
location and name of the schema file.
All global elements and attributes from the schema must be
qualified in the instance document.
-
5/20/2018 XML Tutorial.04
49/50
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:
Whereschema is the name and location of the schema file.
To import a schema from a different namespace, use the
syntax
Where uri is the URI of the imported schemas namespace
andschema is the name and location of the schema file.
-
5/20/2018 XML Tutorial.04
50/50
XP
New Perspectives on XML 2nd Edition 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 simpleand complex types