Title: New Perspectives on XML, 2nd Edition
1 TUTORIAL 4
WORKING WITH SCHEMAS
2SCHEMAS
- 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.
3COMPARING SCHEMAS AND DTDS
- This figure compares schemas and DTDs
4SCHEMA 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.
5SCHEMA VOCABULARIES
- This figure shows a few schema vocabularies
6STARTING A SCHEMA FILE
- A schema is always placed in a separate XML
document that is referenced by the instance
document.
7Example Schema
- lt?xml version"1.0"?gtlt xsschema
xmlnsxs"http//www.w3.org/2001/XMLSchema"gtltxs
element name"note"gt ltxscomplexTypegt
ltxssequencegt ltxselement name"to"
type"xsstring"/gt ltxselement name"from"
type"xsstring"/gt ltxselement
name"heading" type"xsstring"/gt
ltxselement name"body" type"xsstring"/gt
lt/xssequencegt lt/xscomplexTypegtlt
/xselementgtlt /xsschemagt
8ELEMENTS AND ATTRIBUTES OF THE PATIENTS DOCUMENT
- This figure shows the elements and attributes of
the patients.xml document
See pages 147 and 148
9SCHEMA TYPES
- XML Schema recognize two categories of types
- Simple type contains only a single value
- Value of an attribute
- Or the textual content of an element
- Complex type
- Contains a structure
10SCHEMA TYPES
- This figure shows types of elements
11SIMPLE TYPE ELEMENTS
- Use the following syntax to declare a simple type
element in XML Schema - ltelement namename type type/gt
- name the name of the element in the instance
document - type the data type of the element.
- Example
- ltxselement name lastName type xsstring
/gt
Unlike DTD, schema is an XML language
12UNDERSTANDING DATA TYPES
- XML Schema supports two data types
- 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.
13DECLARING AN ATTRIBUTE
- An attribute is another example of a simple type.
The syntax to define an attribute is - ltxsattribute namename type"type
defaultdefault fixedfixed /gt - name the name of the attribute
- type the data type,
- default the attributes default value
- fixed a fixed value for the attribute
- For examle
- ltxsattribute nameGender typexsstring
defaultfemale /gt
By convention, use xs for namespace for XML Schma
namespace
optional
14ASSOCIATING ATTRIBUTES AND ELEMENTS
- The basic structure for defining a complex type
with XML Schema is - ltxselement name"name"gt
- ltxscomplexTypegt
- declarations
- lt/xscomplexTypegt
- lt/xselementgt
- name the name of the element
- declarations schema commands specific to the
type of complex element being defined.
15ASSOCIATING 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.
The following slides describe each of these
16EMPTY ELEMENTS AND ATTRIBUTES
- The code to declare the attributes of an empty
element is - ltxselement name"name"gt
- ltxscomplexTypegt
- attributes
- lt/xscomplexTypegt
- lt/xselementgt
- attributes the set of declarations that define
the attributes associated with the element
17EMPTY ELEMENTS AND ATTRIBUTES
- For example
- ltxs element namesubjectgt
- ltxscomplexTypegt
- ltxsattriubute namename typexsstring /gt
- ltxsattriubute nameage typexsstring /gt
- lt/xs complexTypegt
- lt/xselementgt
- describes the following empty element
- ltsubject nameCynthia Dibbs age62 /gt
18SIMPLE 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. - ltxselement name"name"gt
- ltxscomplexTypegt
- ltxssimpleContentgt
- ltxsextension base"type"gt
- attributes
- lt/xsextensiongt
- lt/xssimpleContentgt
- lt/xscomplexTypegt
- lt/xselementgt
19SIMPLE CONTENT AND ATTRIBUTES
Indicates text but no child
- For example
- ltxselement nameperformancegt
- ltxscomplexTypegt
- ltxssimpleContentgt
- ltxsextension basexsstringgt
- ltxsattribute namescale
typexsstring/gt - lt/xsextensiongt
- lt/xssimpleContentgt
- lt/xscomplexTypegt
- lt/xselementgt
- defines
- ltperformance scaleKarnofskykgt0.81lt/performancegt
Extended to include an attribute
20SPECIFYING 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 - optional(default) The use of the attribute is
optional with the element - prohibitedThe attribute cannot be used with the
element - For example, the previous attribute declaration
is modified as - ltxsattribute namescale typexsstring
userequired/gt
21REFERENCING AN ELEMENT OR ATTRIBUTE
- 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 - ltxselement ref"elemName" /gt
- ltxsattribute ref"attName" /gt
- where
- elemName the name used in an element
declaration - attName the name used in an attribute
declaration
22REFERENCING AN ELEMENT OR ATTRIBUTE
- ltxsattribute namescale typexsstring /gt
- ltxselement nameperformancegt
- ltxscomplexTypegt
- ltxssimpleContentgt
- ltxsextension base xsstringgt
- ltxsattributegt refscale userequired /gt
- lt/xsextensiongt
- lt/xssimpleContentgt
- lt/xscomplexTypegt
- lt/xselementgt
23WORKING WITH CHILD ELEMENTS
- Another kind of complex type element contains
child elements, but no attributes. To define
these child elements, use the code structure - ltxselement name"name"gt
- ltxscomplexTypegt
- ltxscompositorgt
- elements
- lt/xscompositorgt
- lt/xscomplexTypegt
- lt/xselementgt
- where
- elements the list of simple type element
declarations for each child element - compositor defines how the child elements are
organized.
24USING 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.
25USING COMPOSITORS
- ltelement nameaddressgt
- ltxscomplexTypegt
- ltxssequencegt
- ltxselement name street type
xsstring/gt - ltxselement name city type xsstring/gt
- ltxselement name state type
xsstring/gt - lt/xssequencegt
- ltxscomplexTypegt
- lt/elementgt
Must be in sequence
26USING COMPOSITORS
- ltelement nameFamilygt
- ltxscomplexTypegt
- ltxsallgt
- ltxselement name Father type
xsstring/gt - ltxselement name Mother type
xsstring/gt - lt/xsallgt
- ltxscomplexTypegt
- lt/elementgt
Family may contain Father and/or Mother in no
particular order
27SPECIFYING THE OCCURENCES OF AN ITEM
- ltxselement namepatient typexsstring
minOccurs1 maxOccurs3/gt
28WORKING WITH CHILD ELEMENTS AND ATTRIBUTES
- The code for a complex type element that contains
both attributes and child elements is - ltxselement name"name"gt
- ltxscomplexTypegt
- ltxscompositorgt
- elements
- lt/xscompositorgt
- lt/xscomplexTypegt
- attributes
- lt/xselementgt
29CHILD ELEMENTS AND ATTRIBUTES EXAMPLE
- ltxselement namepatientgt
- ltxscomplexTypegt
- ltxssequencegt
- ltxselement reflastname/gt
- ltxselement reffirstName/gt
- lt/xssequencegt
- ltxsattribute refpatID userequiredgt
- lt/xscomplexTypegt
- ltxselementgt
30SPECIFYING MIXED CONTENT
- ltSummarygt
- Patient ltNamegtCynthia Davislt/Namegt was enrolled
in - the ltStudygtTamoxifen Studylt/Studygt on 8/15/2003.
- lt/Summarygt
- can be declared in the schema file using the
following complex type - ltelement name"Summary"gt
- ltcomplexType mixed"true"gt
- ltsequencegt
- ltelement name"Name" type"string"/gt
- ltelement name"Study" type"string"/gt
- lt/sequencegt
- lt/complexTypegt
- lt/elementgt
element contains both text and child elements
31APPLYING 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.
- Example
- xmlnsxsihttp//www.w3.org/2001/XMLSchema-instanc
e - xsiSchemaLocationpschema.xsdgt
Commonly used for XML Schema instances
Schema instance namespace
32APPLYING A SCHEMA
- Specifying a schema in an XML document
- is treated only as a hint by validating parsers
- ignored by some parsers
- Not allowed to prevent improper financial
documents from being fraudulently submitted - If there is no namespace for the contents of the
instance document, add the following attribute to
the root element - xsinoNamespaceSchemaLocation"schema"
Schema instance namespace
33Pause and breath
34UNDERSTANDING 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.
35UNDERSTANDING DATA TYPES
- This figure shows the 44 built-in data types
Page XML 168
Premitive types
Dates times
Derived Types
36UNDERSTANDING DATA TYPES
- This figure shows a partial description of XML
string data types
Page XML 169
37USING DATA TYPES
- Examples
- ltxsattribute namepatID typexsID /gt
- ltxsattribute nameonStudy typexsstring /gt
- ltxsattribute namescale typexsstring /gt
38UNDERSTANDING DATA TYPES
- This figure shows a partial description of XML
numeric data types
Page XML 170
39UNDERSTANDING DATA TYPES
- This figure shows a partial description of XML
date and time data types
Page XML 171
40DERIVING 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. - Text string length, range of allowable values,
41USER 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.
42LIST
- List data type is a list of values separated by
white space - To create a list data type
- ltxssimpleType namewbcListgt
- ltxslist itemTypexsdecimal /gt
- ltxssimpleTypegt
- To use the data type
- ltxselement namewbc typewbcList /gt
- ltwbegt15.1 15.8. 20.0 9.3 7.1 5.2 lt/wbcgt
43DERIVING 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.
44CONSTRAINING FACETS
- This figure shows the 12 constraining facets
45CONSTRAINING FACETS EXAMPLE
- ltxssimpleType nameageTypegt
- ltxsrestriction basexsintegergt
- ltxsminInclusive value 21 /gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
Constrains the data type to be greater than or
equal to the value
46The 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 - ltxssimpleType name"name"gt
- ltxsrestriction base"type"gt
- ltxspattern value"regex"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- Where regex is a regular expression pattern.
47Regular Expressions
- Figure 4-30 page 184 shows character types, for
example - \d a digit from 0 to 9
- \D non digit character
- Etc.
- Figure 4-31 page 185 shows character sets, for
example - chars Match any character in the chars list
- a-z Match any lower case letter
- 0-9 matach any digit form 0-9
- Etc.
48PATTERN QUANTIFIERS
- This figure shows pattern quantifiers
49EXAMPLE REGULAR EXPRESSIONS
- ltxspattern value\d3 /gt
- ltxspattern valueA-Z /gt
- ltxspattern valueA-Z0,10 /
- ltxsximpleType namemrTypegt
- ltsxrestrictiion basexsIDgt
- ltxspattern valueMR\d3-\d3-d22
/gt - lt/xsrestrictiongt
- ltxssimpleTypegt
50WORKING WITH NAMED TYPES
- Schema authors can create customized complex
types. - Advantage reuse the complex structure in several
places in the document. - For example
- ltxselement namefullName"gt
- ltxscomplexTypegt
- ltxssequencegt
- ltxselement name"firstName"
type"xsstring"/gt - ltxselement name"lastName" type"xsstring"
/gt - lt/xssequencegt
- lt/xscomplexTypegt
- lt/xselementgt
- Use
- ltxselement nameclient typefullName/gt
- ltxselement namesalesperson typefullName/gt
51NAMED MODEL GROUPS
- A named model group is a collection, or group, of
elements. - Syntax
- ltxsgroup name"name"gt
- elements
- lt/xsgroupgt
- Where name is the name of the model group, and
elements is a collection of element declarations
52NAMED MODEL GROUPS EXAMPLE
- Definition
- ltxsgroup namefullNamegt
- ltxssequencegt
- ltxselement name"firstName"
type"xsstring"/gt - ltxselement name"lastName" type"xsstring"
/gt - lt/xssequencegt
- lt/xsgroupgt
- Use
- ltxselement nameclient"gt
- ltxscomplexTypegt
- ltxsgroup reffullName/gtgt
- lt/xscomplexTypegt
- lt/xselementgt
53WORKING 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 - ltxsattributeGroup name"name"gt
- attributes
- lt/xsattributeGroupgt
- Where name is the name of the attribute group and
attributes is a collection of attributes assigned
to the group.
54WORKING WITH NAMED ATTRIBUTE GROUPS
- Example
- ltxsattributeGroup nameDRInfogt
- ltxsattribute nameDRID typexsstring
userequired /gt - ltxsattribute namedept typexsstring
userequired /gt - ltxsattributeGroupgt
- ltxselement namedoctor typedeptData/gt
- ltxscomplexType name deptDatagt
- ltxssimplecontentgt
- ltxs extension base stringgt
- ltxsattributegroup
refDRIinfo/gt - lt/xsextensongt
- lt/xssimplecontentgt
- lt/xscomplexTypegt
ltdoctor DRIDDR251 deptPediatricsgt curt
Hurley ltdoctorgt
55SCOPE
- An item has global scope it is a child of a
root schema element - Can be referenced and reused anywhere in the
schema - An item has local scope it is nested within
another element - Can be referenced and reused only within the item
in which it is declared
56STRUCTURING A SCHEMA
- Schemas can be structured in several ways
- Flat catalog (also called salami slice)
- All declarations are made globally
- Russian doll
- Has only one global element
- Everything else is nested inside of that element
- Venetian Blind Design
- Similar to flat catalog, except
- Creates types and references within a single
global element
57STRUCTURING A SCHEMA
- One schema design is a Flat Catalog Design.
- Sometimes called salami slice design
- In this design, all element declarations are made
globally. - The structure of the instance document is created
by referencing the global element declarations.
58FLAT CATALOG DESIGN
Figure 4-36 page 192
This figure shows a Flat Catalog design
59STRUCTURING 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.
60RUSSIAN DOLL DESIGN
Figure 4-37 page 193
This figure shows a Russian Doll design
61VENETIAN BLIND DESIGN
- A Venetian blind design
- Compromise between flat catalogs and Russian
dolls - Element and attribute groups and named types are
declared globally (and can be reused throughout
the schema) - Elements and attributes for the instance document
are local and nested
62VENETIAN BLIND DESIGN
Figure 4-38 page 194
63COMPARING SCHEMA DESIGNS
Figure 4-39 page 195
- This figure compares the three schema designs
64PLACING A SCHEMA IN A NAMESPACE TARGETING A
NAMESPACE
- To associate a schema with a namespace
- declare the namespace
- prefixxmlns"uri"
- make that namespace the target of the schema.
- targetNamespace"uri"
- Where
- prefix prefix of the XML Schema namespace
- uri URI of the target namespace
65PLACING A SCHEMA IN A NAMESPACE TARGETING A
NAMESPACE
- Example (Figure 4-40)
- ltxsschema xmlnsxshttp//www.ww3.org/2001/XMLSch
ema - xmlnshttp/uhosp.edu/patients/na
- targetNamespacehttp//uhosp.edu/patien
ts/nagt - ltxs element namepatientsgt
- ltxscomplexTypegt
- ltxssequencegt
- ltxselement namepatient
typepType - Minoccurs 1
maxoccursunbounded /gt - lt/xssequencegt
- lt/xscomplexTypegt
- lt/xselementgt
-
Declare the namespace
Make the new namespace the target of the schema
66APPLYING 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 - xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
ce" - xsischemaLocation"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.
67APPLYING A SCHEMA TO A DOCUMENT WITH A NAMESPACE
- Example
- ltpatients xmlnsxsihttp//www.W3.org/200/XMLSchem
a-instance - xmlnshttp//uhosp.edu/patients/ns
- xsischemaLocationhttp//uhosp.edu/patients/
ns patvb.xsdgt - ltpatient patID MR890-041-02
- .
- lt/patientgt
68INCLUDING AND IMPORTING SCHEMAS
- To include a schema from the same namespace, add
the following element as a child of the schema
element - ltxsinclude schemaLocation"schema" /gt
- Where schema is the name and location of the
schema file. - To import a schema from a different namespace,
use the syntax - ltxsimport namespace"uri" schemaLocation"schema"
/gt - Where uri is the URI of the imported schemas
namespace and schema is the name and location of
the schema file.
69REFERENCING 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
70Assignment Tutorial 4 Case Problem 1
- Perform the tasks listed on pages 212 and 213
- Create a CSS for the resulting jw.xml
- Post your work, including the CSS on your web
site. - End an e-mail with the subject Tutorial 4 Case
Problem 1 by ltyour namegt to jim_at_larson-tech.com
by Wednesday 1150 May 1