XML Schema Fall 2001

Download Report

Transcript XML Schema Fall 2001

.opennet Technologies
XML Schema
Fall Semester 2001 MW 5:00 pm - 6:20 pm CENTRAL (not Indiana) Time
Geoffrey Fox and Bryan Carpenter
PTLIU Laboratory for Community Grids
Computer Science,
Informatics, Physics
Indiana University
Bloomington IN 47404
[email protected]
7/16/2015
xmlschemafall01
1
Course Playback I
http://ararat.ucs.indiana.edu:8080/elearning/user/index.html
7/16/2015
xmlschemafall01
• Look at Past
Sessions
• They are not
in order as we
rerecorded
Lecture 1 and
last part of
Lecture 4
• Click
Playback
2
Course Playback II
• When Playback Screen
appears, scroll to Start
A box will spring up and replay starts
7/16/2015
xmlschemafall01
3
Course Playback III
• Note
current
system
doesn’t
display
skipped
messages in
text and
participator
category
• Also
labeling of
“chapters”
bad
7/16/2015
xmlschemafall01
4
Course playback IV
• You can
skip
anywhere
7/16/2015
xmlschemafall01
5
Outline
• XML Schema discussion based on
http://www.w3.org/TR/xmlschema-0/
– Note that this is reasonably complete – parts 1 and 2
(http://www.w3.org/TR/xmlschema-1/ etc.) are more formal
discussions not extensions in coverage
• We summarize all the topics in this primer
– Basic Schema
– Groups
– Namespaces
– Include
– Derived types
– Import
• Remember: Schema and DTD are like classes in Java
– XML files are instances of objects defined in Schema or DTD’s
7/16/2015
xmlschemafall01
6
XML Schema PO Example po.xml I
• <?xml version="1.0"?>
• <purchaseOrder orderDate="1999-10-20">
•
<shipTo country="US">
•
<name>Alice Smith</name>
•
<street>123 Maple Street</street>
•
<city>Mill Valley</city>
•
<state>CA</state>
•
<zip>90952</zip>
•
</shipTo>
•
<billTo country="US">
•
<name>Robert Smith</name>
•
<street>8 Oak Avenue</street>
•
<city>Old Town</city>
•
<state>PA</state>
•
<zip>95819</zip>
•
</billTo>
7/16/2015
xmlschemafall01
Need to add
mechanism to
associate schema
with this XML
instance
7
XML Schema PO Example po.xml II
•
<comment>Hurry, my lawn is going wild!</comment>
•
<items>
•
<item partNum="872-AA">
•
<productName>Lawnmower</productName>
•
<quantity>1</quantity>
•
<USPrice>148.95</USPrice>
•
<comment>Confirm this is electric</comment>
•
</item>
•
<item partNum="926-AA">
•
<productName>Baby Monitor</productName>
•
<quantity>1</quantity>
•
<USPrice>39.98</USPrice>
•
<shipDate>1999-05-21</shipDate>
•
</item>
•
</items>
• </purchaseOrder>
7/16/2015
xmlschemafall01
8
The Purchase Order Schema, po.xsd I
• <xsd:schema xmlns:xsd="http://www.w3.org/2000/08/XMLSchema">
• <xsd:annotation>
• <xsd:documentation>
•
Purchase order schema for Example.com.
•
Copyright 2000 Example.com. All rights reserved.
• </xsd:documentation>
• </xsd:annotation>
• <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
• <xsd:element name="comment" type="xsd:string"/>
• <xsd:complexType name="PurchaseOrderType">
• <xsd:sequence>
•
<xsd:element name="shipTo" type="USAddress"/>
•
<xsd:element name="billTo" type="USAddress"/>
•
<xsd:element ref="comment" minOccurs="0"/>
•
<xsd:element name="items" type="Items"/>
• </xsd:sequence>
• <xsd:attribute name="orderDate" type="xsd:date"/>
• </xsd:complexType>
7/16/2015
xmlschemafall01
9
The Purchase Order Schema, po.xsd II
• <xsd:complexType name="USAddress">
• <xsd:sequence>
•
<xsd:element name="name" type="xsd:string"/>
•
<xsd:element name="street" type="xsd:string"/>
•
<xsd:element name="city" type="xsd:string"/>
•
<xsd:element name="state" type="xsd:string"/>
•
<xsd:element name="zip" type="xsd:decimal"/>
• </xsd:sequence>
•
•
•
<xsd:attribute name="country" type="xsd:NMTOKEN"
use="fixed" value="US"/>
</xsd:complexType>
7/16/2015
xmlschemafall01
10
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
The Purchase Order Schema, po.xsd III
<xsd:complexType name="Items">
<xsd:sequence>
<xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="productName" type="xsd:string"/>
<xsd:element name="quantity">
<xsd:simpleType>
Anonymous type
<xsd:restriction base="xsd:positiveInteger">
quantity
<xsd:maxExclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
Specify
</xsd:element>
item
<xsd:element name="USPrice" type="xsd:decimal"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="partNum" type="SKU"/>
</xsd:complexType>
7/16/2015
xmlschemafall01
11
The Purchase Order Schema, po.xsd IV
•
•
•
</xsd:element> <!– End item specification 
</xsd:sequence> <!– End sequence for items specification 
</xsd:complexType> <!– End items specification 
•
•
•
•
•
•
<!-- Stock Keeping Unit, a code for identifying products -->
<xsd:simpleType name="SKU">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{3}-[A-Z]{2}"/>
</xsd:restriction>
</xsd:simpleType>
• </xsd:schema>
7/16/2015
xmlschemafall01
12
Comments on po.xsd I
• The prefix xsd: is Namespace specified by
xmlns:xsd=http://www.w3.org/2000/08/XMLSchema
– The label xsd is conventional; you could use a different one
• Schema does two types of things:
– Defines new types of elements or attributes e.g.
PurchaseOrderType with xsd:complexType or xsd:simpleType
– Defines internal elements and attributes for tags e.g.
PurchaseOrder using xsd:element and xsd:attribute
So an element like shipTo in po.xml has at
most one attribute country which if it
appears must have value US. The
elements name, street, city, state, zip must
appear in this order and the first four are
strings; zip is a xsd:decimal
7/16/2015
xmlschemafall01
13
Comments on po.xsd II
• The prefix xsd: is Namespace specified by
xmlns:xsd=http://www.w3.org/2000/08/XMLSchema
– The label xsd is conventional; you could use a different one
The Purchase Order Schema, po.xsd I
7/16/2015
• <xsd:schema xmlns:xsd="http://www.w3.org/2000/08/XMLSchema">
• <xsd:annotation>
•
<xsd:documentation>
•
Purchase order schema for Example.com.
•
Copyright 2000 Example.com. All rights reserved.
•
</xsd:documentation>
• </xsd:annotation>
• <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
• <xsd:element name="comment" type="xsd:string"/>
• <xsd:complexType name="PurchaseOrderType">
•
<xsd:sequence>
•
<xsd:element name="shipTo" type="USAddress"/>
•
<xsd:element name="billTo" type="USAddress"/>
•
<xsd:element ref="comment" minOccurs="0"/>
•
<xsd:element name="items" type="Items"/>
•
</xsd:sequence>
•
<xsd:attribute name="orderDate" type="xsd:date"/>
• </xsd:complexType>
xmlschemafall01 xmlschemafall01
149
9/17/2001
Comments on po.xsd III
• Schema does two types
of things:
– Defines new types of
elements or attributes
e.g.
PurchaseOrderType
with xsd:complexType
– Defines internal
elements and attributes
for tags e.g.
PurchaseOrder using
xsd:element and
xsd:attribute
7/16/2015
The Purchase Order Schema, po.xsd I
• <xsd:schema xmlns:xsd="http://www.w3.org/2000/08/XMLSchema">
• <xsd:annotation>
•
<xsd:documentation>
•
Purchase order schema for Example.com.
•
Copyright 2000 Example.com. All rights reserved.
•
</xsd:documentation>
• </xsd:annotation>
• <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
• <xsd:element name="comment" type="xsd:string"/>
• <xsd:complexType name="PurchaseOrderType">
•
<xsd:sequence>
•
<xsd:element name="shipTo" type="USAddress"/>
•
<xsd:element name="billTo" type="USAddress"/>
•
<xsd:element ref="comment" minOccurs="0"/>
•
<xsd:element name="items" type="Items"/>
•
</xsd:sequence>
•
<xsd:attribute name="orderDate" type="xsd:date"/>
• </xsd:complexType>
9/17/2001
xmlschemafall01
xmlschemafall01
9
15
Comments on po.xsd IV
• So an element like shipTo in po.xml has at most one attribute country which if it
appears must have value US. The elements name, street, city, state, zip must appear
in this order and the first four are strings; zip is a xsd:decimal
The Purchase Order Schema, po.xsd II
• <xsd:complexType name="USAddress">
• <xsd:sequence>
•
<xsd:element name="name" type="xsd:string"/>
•
<xsd:element name="street" type="xsd:string"/>
•
<xsd:element name="city" type="xsd:string"/>
•
<xsd:element name="state" type="xsd:string"/>
•
<xsd:element name="zip" type="xsd:decimal"/>
• </xsd:sequence>
•
•
•
<xsd:attribute name="country" type="xsd:NMTOKEN"
use="fixed" value="US"/>
</xsd:complexType>
7/16/2015
9/17/2001
xmlschemafall01
xmlschemafall01
10
16
xsd:complexType Example
• Anything of PurchaseOrderType is allowed one attribute (orderDate) of type
xsd:date.
– Further it must consists of 4 elements shipTo, billTo, comment and items in that
order
– comment can be absent as minOccurs = 0
• Note that comment is a global element defined in the schema. It is accessed by
ref= rather than type=
• One can define global elements or attributes which cannot themselves use ref
(global elements are particularly important if one imports this schema into
other schema as only global components can be re-used)
• minOccurs and maxOccurs have default values of 1 and so shipTo, billTo and
items must appear once and once only
7/16/2015
xmlschemafall01
17
Specifying xsd:element and xsd:attribute
• Any xsd:element tag can have attribute minOccurs, maxOccurs,
fixed and default
• Any xsd:attribute can have attribute use and value
• Note attributes can NEVER appear more than once
- means not present
7/16/2015
xmlschemafall01
18
Simple Types
• Attributes must be Simple Types; Elements can be complex types
or simple types
• Simple types cannot themselves have attributes or contain other
elements
• The xsd:restriction tag allows you to build new simple types by
adding constraints to existing simple types. These constraints are
specified by a set of “constraining facets”
xsd:minInclusive and xsd:maxInclusive
restrict 10000 <= myInteger <= 99999
xsd:pattern restricts using
a regular expression to 3 single digits
followed by two capital letters
7/16/2015
xmlschemafall01
19
Constraining Facets
• These are defined in http://www.w3.org/TR/2000/CR-xmlschema2-20001024/ and are
• length, minLength, maxLength, pattern, enumeration,
whiteSpace, maxInclusive, maxExclusive, minExclusive,
minInclusive, precision, scale, encoding, duration, period
• Tables tell you which constraining facets can be used with which
simple type
• enumeration can be used with all simple types except boolean. In
example below, you could define a USState simple type which
could take any of conventional abbreviations of the US States
7/16/2015
xmlschemafall01
20
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Simple Types Built In to XML Schema I
Simple Type
Examples (delimited by commas)
Red is Note
string
Confirm this is electric
CDATA
Confirm this is electric (white space(tabs) to blanks etc.)
token
Confirm this is electric (trailing/leading white space removed)
byte
-1, 126
unsignedByte
0, 126
binary
62696E617279
integer
-126789, -1, 0, 1, 126789
positiveInteger
1, 126789
negativeInteger -126789, -1
nonNegativeInteger 0, 1, 126789
nonPositiveInteger -126789, -1, 0
int
-1, 126789675
unsignedInt
0, 1267896754
long
-1, 12678967543233
unsignedLong
0, 12678967543233
short
-1, 12678
unsignedShort
0, 12678
7/16/2015
xmlschemafall01
21
Simple Types Built In to XML Schema II
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
decimal
-1.23, 0, 123.4, 1000.00
float
-INF, -1E4, -0, 0, 12.78E-2, 12, INF, NaN
double
-INF, -1E4, -0, 0, 12.78E-2, 12, INF, NaN
Red is Note
boolean
true, false
time
13:20:00.000, 13:20:00.000-05:00
timeInstant
1999-05-31T13:20:00.000-05:00 [May 31st 1999 at 1.20pm
Eastern Standard Time which is 5 hours behind Coordinated Universal Time]
timePeriod
1999-05-31T13:20
timeDuration P1Y2M3DT10H30M12.3S [1 year, 2 months, 3 days, 10 hours,
30 minutes, 12.3 seconds]
date
1999-05-31
month
1999-05
[May 1999]
year
1999
[1999]
century
19
[the 1900's]
recurringDay ----31
[every 31st day]
recurringDate --05-31
[every May 31st]
recurringDuration --05-31T13:20:00
[May 31st every year at 1.20pm
Coordinated Universal Time, format similar to timeInstant]
7/16/2015
xmlschemafall01
22
Simple Types Built In to XML Schema III
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Simple Type Examples (delimited by commas)
Notes in []
Name
shipTo
[XML 1.0 Name type]
QName
po:USAddress
[XML Namespace QName]
NCName
USAddress
[XML Namespace NCName, i.e. a
QName without the prefix and colon]
uriReference http://www.example.com/, http://www.example.com/doc.html#ID5
language
en-GB, en-US, fr
[valid values for xml:lang as defined in
XML 1.0]
ID
[XML 1.0 ID attribute type]
IDREF
[XML 1.0 IDREF attribute type]
IDREFS
[XML 1.0 IDREFS attribute type]
ENTITY
[XML 1.0 ENTITY attribute type]
ENTITIES
[XML 1.0 ENTITIES attribute type]
NOTATION
[XML 1.0 NOTATION attribute type]
NMTOKEN
US, Brésil
[XML 1.0 NMTOKEN attribute type]
NMTOKENS US UK, Brésil Canada Mexique
[XML 1.0 NMTOKENS
attribute type, i.e. a whitespace separated list of NMTOKEN's]
7/16/2015
xmlschemafall01
23
List Types
• List types are gotten from Simple types as white space separated atomic types
– NMTOKENS IDREFS and ENTITIES are built-in list types
– <listofMyInt>20003 15038 95976 95942</listofMyInt>
• Length, minLength, maxLength and enumeration are good facets of list types
<sixStates>PA NY CA FL
LA AK</sixStates> is an
example
7/16/2015
xmlschemafall01
24
Anonymous and Union types
• Union types define elements or
attributes whose value can fall in
union of one or more atomic types
The Purchase
•
Examples:
<zips>CA</zips>
Order
Schema,
po.xsd
<zips>32308
32306</zips>
III
• <xsd:complexType name="Items">
•
<xsd:sequence>
Anonymous types are declared
as name="item" minOccurs="0" maxOccurs="unbounded">
•
<xsd:element
• our
<xsd:complexType>
for item and quantity in
earlier
•
<xsd:sequence>
example without a type• attribute
<xsd:element name="productName" type="xsd:string"/>
•
<xsd:element name="quantity">
•
<xsd:simpleType>
Anonymous type
Use <xsd:complexType>
•
<xsd:restriction base="xsd:positiveInteger">
quantity
•
<xsd:maxExclusive value="100"/>
Or <xsd:simpleType>
•
</xsd:restriction>
after an xsd:element tag Specify
without
•
</xsd:simpleType>
•
</xsd:element>
a type= attribute
item
•
<xsd:element name="USPrice" type="xsd:decimal"/>
•
<xsd:element ref="comment" minOccurs="0"/>
•
<xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
•
</xsd:sequence>
•
<xsd:attribute name="partNum" type="SKU"/>
•
</xsd:complexType>
7/16/2015
9/17/2001
xmlschemafall01
xmlschemafall01
2511
Adding Attributes to Simple Types
• We used
<xsd:element name="USPrice" type="xsd:decimal"/>
in po.xsd. Suppose we wish to give this an attribute to specify
currency. This is done by deriving a new complex type from the
simple type decimal
• internationalPrice is defined as an anonymous type by
• With example <internationalPrice currency=“EUR” >123.4
</internationalPrice>
7/16/2015
xmlschemafall01
26
Mixed Content
• The mixed=“true” attribute in xsd:complexType allows one to
specify an XML element that meaningfully mixes character data
with elements as in example
This schema defines
two mixed contents
elements
salutation and
letterbody.
Note order must be
correct and is checked
on validation
7/16/2015
xmlschemafall01
27
Just Attributes. Empty Content
• <internationalPrice currency=“EUR” value=“123.4” /> can be defined in two
ways
– Note use of xsd:anyType in full form as we are defining an element with no
content – therefore type irrelevant.
By definition
anyType puts no
constraints on the
content model
This is equivalent
to top form
7/16/2015
xmlschemafall01
28
Annotations
• Here <xsd:annotation> with sub element <xsd:documentation> is
used to explain schema of internationalPrice
• Can also use xsd:appInfo element as a child of xsd:annotation to
provide information to tools and stylesheets
• <xsd:annotation> can appear in most Schema xsd: elements
7/16/2015
xmlschemafall01
29
Generalized Groups
• <xsd:sequence> is rather strict as it requires order of elements
declared within sequence.
– Some flexibility is allowed from minOccurs and maxOccurs
• <xsd:choice> with <xsd:group> and <xsd:all> allow more general
structures
This allows either
a single address
<singleUSAddress>
or separate shipTo
and billTo addresses
This is the
shipAndBill group
referenced with
ref= above
7/16/2015
xmlschemafall01
30
The <xsd:all> Group
• <xsd:all> MUST appear at top-level of any content model and the
the elements in the all group must have minOccurs and
maxOccurs as 0 or 1
Elements in all
group can
appear in
any order
Top of
Content Model
for
PurchaseOrderType
Here <xsd:all>
is NOT at top
7/16/2015
xmlschemafall01
31
Attribute Groups
• Often one wishes to use the same attributes in several elements
– This is done using parameter entities in DTD syntax.
Rather than
“crude”
macro
substitution,
Schema
allows groups
to be defined
and
referenced
New Attributes
7/16/2015
xmlschemafall01
32
Use of Attribute Groups
Note Attribute
declarations and
attributeGroup
references must
appear at end of
a complexType
definition
Reference
attributeGroup
Define
attributeGroup
7/16/2015
xmlschemafall01
33
Null Values
• As well as setting explicit values for an element, one may wish to
indicate that element has NO set value
• Taking example of shipDate (which was absent for the
Lawnmower in po.xml), one indicates this that is allowed in
Schema definition by
<xsd:element name="shipDate" type="xsd:date"
nullable="true"/>
• And to explicitly represent that shipDate has a null value in the
instance document, we set the null attribute (from the XML
Schema namespace for instances) to true:
• <shipDate xsi:null="true"></shipDate>
• This assumes one has set
xmlns:xsi = http://www.w3.org/1999/XMLSchema-instance
• Such null values MUST not values set in <shipDate ..></shipDate>
but can have any allowed attributes
7/16/2015
xmlschemafall01
34
Schema and Namespaces I
• Schema do a better job than DTD in making it clear how
Namespaces can be used effectively
• Here we define http://www.example.com/PO1 as the
targetNamespace for this schema
• We also define po: as the
same URL and the default
Namespace to be W3C
Schema central
• Then we do NOT need xsd:
as in original po.xml as we
have default Schema central
to be default
• In name=‘..’, we do NOT
need to specify a Namespace
but in type=“..” or ref=“..”
we do not as these could
reference another Namespace
7/16/2015
No Namespace as string type
define in Schema Central
xmlschemafall01
35
Schema and Namespaces II
• We must distinguish global and local elements
– The attribute definitions in po1.xml
elementFormDefault="unqualified"
attributeFormDefault="unqualified“
• Imply that all local elements are assumed to come from
targetNamespace and MUST not be qualified
• purchaseOrder and comment are global; remainder local
• apo: must be used with global elements but not with other ones
7/16/2015
xmlschemafall01
36
elementFormDefault I
We can require local
elements to be
qualified by setting
elementFormDefault
as “qualified”
Schema
Attributes need not be
qualified as we left
attributeFormDefault
as “unqualified”
Example of this Schema
7/16/2015
xmlschemafall01
37
elementFormDefault II
• If we make po1.xml the default Namespace, then whatever setting
of elementFormDefault we need no prefixes at all.
7/16/2015
xmlschemafall01
38
Selective Qualification
• One can set
defaults and then
override defaults
for selected
attributes or
elements
• This is illustrated
for attribute
publicKey here
• Qualification and
its default is to be
designed to ensure
no ambiguities and
mistakes when
there are multiple
Namespaces
7/16/2015
xmlschemafall01
39
Global Elements in Schema
7/16/2015
• We can make schema where
all elements and attributes
are global as in this po.xsd
• This is just the DTD version
and all element names must
be unique and all must be
qualified unless po.xml is
declared the default
Namespace
• We will find that global
elements can be re-used
xmlschemafall01 more effectively
40
xsd:include Tag
• Here we include another
schema file which itself is a
valid schema definition.
• The net result is a single
Namespace referenced
through original file
• One can any number of
include statements and
arbitrary nesting but there
will always be a single
Namespace
<include schemaLocation=
"http://www.example.com/schemas/address.xsd"/>
7/16/2015
xmlschemafall01
41
Xsd:Include
• Here is the File to be
included
• The parser will find all the
files and construct the total
Schema
7/16/2015
xmlschemafall01
42
Deriving Types by Extension
Derived
Derived
Equivalent to
Standalone
• Here the types USAddress and
UKAddress are gotten by
adding elements and attribute
(for UKAddress) to complex
type Address
• ipo is defined as Namespace
There is an equivalent standalone
where UKPostcode and
way of doing this which is shorter
USAddress are defined
perhaps but as we see on next foil is
less powerful in the long run
7/16/2015
xmlschemafall01
43
Using Extended types
• One can use derived
types in an instance of a
Schema where Schema
specified only the
parent
• To do this you must set
the xsi:type attribute
for the derived element
to be ipo:derived type
• Here xsi is W3C central
XML Schema Instance
namespace.
• ipo is Namespace for
this particular schema
7/16/2015
xmlschemafall01
44
Restriction of Complex Types
• Here we are taking
a given complex or
simple type and
restricting it to a
subset of its
previous
capabilities. In
example, we take
Items and derive a
new type
ConfirmedItems
which only has one
change
– minOccurs=“1”
not “0” for the
item subelement
7/16/2015
Note you must repeat all the unchanged
parts of items and item
xmlschemafall01
45
Examples of Restrictions
• Valid restrictions must decrease allowed range so that if in
original (minOccurs,maxOccurs) = (1,1) one cannot further
restrict it
• The first three examples are in common between attributes (
where one needs to specify use and value) and elements
7/16/2015
xmlschemafall01
46
<xsd:redefine Tag
• Here you do not
create new
elements but
rather change an
existing element
preserving its
name
– Here we change
Address
• This change holds
for all derived
types based on this
changed element
– So UKAddress
also gets a
country element
7/16/2015
xmlschemafall01
47
Substitution Groups
• Consider any global element, such as comment in po.xml. We can
define alternative labels for this tag with the substitutionGroup
attribute in <xsd:element
• One can use the new labels to improve readability of an XML
instance
We define alternatives
customerComment and
shipComment
7/16/2015
xmlschemafall01
48
Abstract Element and Type
• If we declare comment as abstract by
<element name="comment" type="string" abstract="true"/>
Then we cannot use comment but we can use any element defined in a
substitutionGroup such as customerComment and shipComment
Vehicle is described as an abstract
type. One can use conventionally
any types like Car or Plane
derived from it.
Element transport is defined with
a type Vehicle that is abstract.
Using an element with an abstract
type requires setting
xsi:type=“XXX” where XXX is a
non abstract type derived from
original one
7/16/2015
xmlschemafall01
49
Restricting what can be changed I
• One can control how much freedom there is in extending or restricting simple
and complex types by use of the final attribute in the <xsd:element
<xsd:complexType or <xsd:simpleType Schema elements
– This stops derivation of this type later in Schema
– final=“restriction” prevents new types gotten by restricting original type
– final=“extension” prevents new types gotten by extending original type
– final=“#all” prevents restriction and extension
• In xsd:schema the attribute finalDefault can be used with these same three
values to preset values of final in element and type definitions
• In example, the derivation of USAddress and UKAddress would not be allowed
7/16/2015
xmlschemafall01
50
Restricting what can be changed II
• We also described how one can use in instance documents,
derivations such as USAddress and UKAddress as in shipTo and
billTo elements which are defined in the schema to have type
Address
– This capability can be controlled using the block attribute
block can take values restriction, extension or substitution (to forbid use of
substitution groups) or #all to reflect all of these three capabilities
As with final, one can specify blockDefault in the <xsd:schema element to apply a
default to block in every type and element definition
7/16/2015
xmlschemafall01
51
Changing Facets in Simple Types
• One can use the fixed
attribute to control
restriction of simple types as
in this Postcode example
• [A-Z]{2}\d\s\d[A-Z]{2} is an
allowed derived type as
length still 7
• However changing length or
specifying a pattern that is
not 7 in length is illegal
7/16/2015
xmlschemafall01
52
Constrained Data
• This example shows a report where there are some implicit constraints
– Each Zip code occurs only once
– Each part can occur in different zip codes but any part occurring in any zip code
must occur once and only once in <parts> section
These are constraints between different
element and attribute values
With facets, we constrained individual
entries.
7/16/2015
xmlschemafall01
53
Report Schema report.xsd I
• The <xsd:unique tag
with name dummy1
specifies a set of
elements – those in
regions/zip defined on
next page.
• The <xsd:field tag tells
you what has to be
unique in this set of
elements
– Namely the code
attribute
• We will do xpath later
7/16/2015
xmlschemafall01
54
Report Schema report.xsd II
• Here we illustrate a more powerful concept which generalizes ID
and IDREF in basic XML to any attribute or element
– The <xsd:key tag identifies the number part element in parts as a
key (generalized ID) to be referenced by pNumKey ( a generalized
IDREF)
– As it is a key, the number attribute in parts/part must be unique as
is ID in XML specification
The <xsd:keyref tag
specifies the number
attribute in zip/part has
properties of IDREF
It is not necessarily
unique but must
reference a “pNumKey”
with its value
7/16/2015
xmlschemafall01
55
Report Schema report.xsd III
• This is rest of
report.xsd
• We can specify two
fields in dummy1 to
ensure that pair
(zip,part) is unique
• i.e. for each zip, each
part occurs 0 or 1
times
7/16/2015
xmlschemafall01
56
Import directive I
• In report.xsd we started schema specification
• <schema targetNamespace="http://www.example.com/Report"
xmlns="http://www.w3.org/2000/08/XMLSchema"
xmlns:r="http://www.example.com/Report"
xmlns:xipo="http://www.example.com/IPO"
elementFormDefault="qualified“ >
• <import namespace="http://www.example.com/IPO"/>
• include (as used in ipo.xsd to include address.xsd) incorporates
external schema components into same target namespace as base
schema.
• import allows us to set up “libraries” and use predefined
components from different target namespaces
7/16/2015
xmlschemafall01
57
Import directive II
• Here we see
<xsd:include used
earlier in ipo.xsd
• Given we have
imported all of
ipo.xsd, we can
reference any globally
defined component
Thus we can write in report.xsd
<element ref="xipo:comment"/>
Note however, that we cannot reuse the shipTo element from
po.xsd as only global components can be used. Thus one
canNOT write <element ref="xipo:shipTo"/>
7/16/2015
xmlschemafall01
58
Import directive III
• One can import complex
types as long as they are
globally defined
• USAddress can be used
from ipo.xsd and in example
we extend it to Analyst type
which is used to define in
obvious way (not shown) an
analyst element used as
shown
When schema components are imported from multiple namespaces, each namespace
must be identified with a separate import element, which must appear as the first
children of the schema element. One must also associate each namespace with a
prefix, using a standard xmlns:prefix namespace declaration. This prefix is used in
any references to components in the imported namespace
7/16/2015
xmlschemafall01
59
Type Library
• Here we define a type library and access it
to form a new element as shown in this
Schema fragment
• <schema targetNamespace=
"http://www.example.com/Money"
xmlns="http://www.w3.org/2000/08/XMLSchema"
• xmlns:m=“
http://www.example.com/MoneyTypes" >
• <import namespace=“
http://www.example.com/MoneyTypes"/>
• <element name="convertFrom"
type="m:Currency" > …
• </schema>
• In an instance we would use:
• <convertFrom
name="AFA"/>199.37</convertFrom>
7/16/2015
xmlschemafall01
60
Use of any Element in Schema
• Here we introduce a new element <xsd:any with two special
attributes namespace and processContents
• In this example a single element <htmlexample is allowed but it is
allowed to have any tags from the XHTML Namespace
http://www.w3.org/1999/xhtml
processContents is set to skip so
that the XHTML must be well
formed but is not validated
processContents can also be
strict to require validation of
XHTML fragment or
processContents can also be lax
when XML parser does its best
7/16/2015
xmlschemafall01
61
any Schema
Element - Example
• This is an
example with
inserted
XHTML. Note
use of default
Namespace in
<table tag so
we can avoid
any prefixes
7/16/2015
xmlschemafall01
62
any Schema Attribute or Element
• The namespace attribute in <xsd:any can take several values with
##any ##local ##other and ##targetNamespace having special
meaning
One can also define
<xsd:anyAttribute with the
example allowing href to
appear in <htmlExample tag
7/16/2015
xmlschemafall01
63
schemaLocation and xsi:schemaLocation
• One can use in <xsd:schema the attribute xsi:schemaLocation
to specify helpful information to parser. The syntax is
xsi:schemaLocation=“Namespace1 Hint1 Namespace2 Hint2”
• The parser is allowed to ignore the hints.
• For a document without a target namespace use
xsi:noNamespaceSchemaLocation
• In import and include, the schemaLocation attribute (no xsi:
prefix) specifies URI for imported/included Schema
Example of use of
xsi:schemaLocation
7/16/2015
xmlschemafall01
64