Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
2/24/2016
1
MANAGING INFORMATION (CSCU9T4)
LECTURE 3: XML STRUCTURE 2 - SCHEMAS
Gabriela Ochoa
http://www.cs.stir.ac.uk/~goc/
OUTLINE
Well-formed and Valid XML documents
Document Type Definitions (DTDs)
XML Schemas
Summary
2
2/24/2016
2
XML SCHEMA
Describe the structure of an XML document, just
like a DTD
They became a W3C Recommendation in May
2001
More powerful than DTD because they:
are written in XML
are extensible to additions
support data types
support namespaces
Drawback of Schemas: they are more complex and
verbose than DTD
3
ADVANTAGES OF XML SCHEMAS
4
Easier to:
Describe document
content
Define restrictions on
data
Validate the
correctness of data
Convert data between
different data types
No need to learn a
new language
You can use the same
tools for documents
and Schema
Editor
Parser
Validation
Transformation
Support for Data Types Written in XML
2/24/2016
3
XML SCHEMA- SIMPLE
EXAMPLE <xs:element name="note"> <xs:complexType> <xsd:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
<xs:element name="note"> defines the element called "note"
<xs:complexType> the "note" element is a complex type
<xs:sequence> the complex type is a sequence of elements
<xs:element name="to" type="xs:string"> the element "to" is of type string (text)
<xs:element name="from" type="xs:string"> the element "from" is of type string
<xs:element name="heading" type="xs:string"> the element "heading" is of type string
<xs:element name="body" type="xs:string"> the element "body" is of type string
<?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
Note: I’m omitting the
heading to make it simple
5
Book.xml
1 <?xml version = "1.0"?>
2 <!-- Fig. 20.7 : book.xml -->
3 <!-- Book list marked up as XML -->
4
5 <deitel:books xmlns:deitel = "http://www.deitel.com/booklist">
6 <book>
7 <title>XML How to Program</title>
8 </book>
9 <book>
10 <title>C How to Program</title>
11 </book>
12 <book>
13 <title>Java How to Program</title>
14 </book>
15 <book>
16 <title>C++ How to Program</title>
17 </book>
18 <book>
19 <title>Perl How to Program</title>
20 </book>
21 </deitel:books>
2/24/2016
4
Book.xsd
1 <?xml version = "1.0"?>
2
3 <!-- Fig. 20.8 : book.xsd -->
4 <!-- Simple W3C XML Schema document -->
5
6 <xs:schema xmlns:xsd = "http://www.w3.org/2000/10/XMLSchema"
7 xmlns:deitel = "http://www.deitel.com/booklist"
8 targetNamespace = "http://www.deitel.com/booklist">
9
10 <xs:element name = "books" type = "deitel:BooksType"/>
11
12 <xs:complexType name = "BooksType">
13 <xs:element name = "book" type = "deitel:BookType"
14 minOccurs = "1" maxOccurs = "unbounded"/>
15 </xs:complexType>
16
17 <xs:complexType name = "BookType">
18 <xs:element name = "title" type = "xsd:string"/>
19 </xs:complexType>
20
21 </xs:schema>
Attributes name and type specify the element’s
name and data type, respectively.
Element complexType defines an element type
that has a child element named book.
Attribute maxOccurs, with value unbounded
specifies that books may have any number of book
child elements.
Attribute minOccurs specifies that books must
contain a minimum of one book element.
Element element defines an element to be
included in the XML document structure.
XML SCHEMA DETAILS • The <schema> Element
• ELEMENTS
• ATTRIBUTES
2/24/2016
5
THE <SCHEMA> ELEMENT
The root element of every XML Schema
It may contain some attributes
<?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema>
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
...
...
</xs:schema>
<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema
• Elements and data types come from the "http://www.w3.org/2001/XMLSchema" namespace.
• Elements and data types in the scheme should be prefixed with xs:
targetNamespace=http://www.w3schools.com
• Elements defined by this schema come from the "http://www.w3schools.com" namespace.
xmlns=http://www.w3schools.com
• The default namespace is "http://www.w3schools.com".
9
ELEMENTS
Elements are the main building block of all XML documents,
containing the data and determine the structure of the instance
document.
Syntax: <xs:element name="x" type="y"/>
Name property: tag name that will appear in the XML document
Type property: provides the description of what type of data can be
contained within the element when it appears in the XML document.
Element content type can be:
Simple: simple predefined data types (such as: xs:string, xs:decimal,
xs:integer, xs:boolean, xs:date, xs:time)
Complex: other elements
Mixed: both (simple data types an other elements)
User defined types: using the <xs:simpleType> and
<xs:complexType> constructs, which we will cover later.
10
2/24/2016
6
EXAMPLES OF SIMPLE ELEMENTS AND
THEIR XML DATA
Sample XSD Sample XML
<xs:element name="Customer_dob“ type="xs:date"/>
<Customer_dob> 2000-01-12T12:13:14Z </Customer_dob>
<xs:element name="Customer_address"type="xs:string"/>
<Customer_address> 99 London Road </Customer_address>
<xs:element name="OrderID“ type="xs:int"/>
<OrderID> 5756 </OrderID>
<xs:element name="Body“ type="xs:string"/>
<Body> (a type can be defined as a string but not have any content, this is not true of all data types however).</Body>
Note: the corresponding value in the XML document must be in the correct
format for its given type otherwise this will cause a validation error.
11
FIXED AND DEFAULT PROPERTIES
The valid data values for the element in the XML
document can be further constrained using the fixed and
default properties.
Default: if no value is specified in the XML document then the
application reading the document, should use the default specified
in the XSD.
Fixed: the value in the XML document can only have the value
specified in the XSD
Example:
<xs:element name="Customer_name" type="xs:string" default="unknown"/>
<xs:element name="Customer_location" type="xs:string" fixed="UK"/>
12
2/24/2016
7
CARDINALITY CONSTRAINTS
Use to indicate the specific number of elements that can
appear in an XML document
Use minOccurs and maxOccurs attributes
Specify whether an element is mandatory, optional, or can
appear up to a set number of times.
Default values for minOccurs and maxOccurs is 1. So if not
present, the element must appear once.
minOccurs can be assigned any non-negative integer value
(e.g. 0, 1, 2, 3... etc.)
maxOccurs can be assigned any non-negative integer value
or the special string constant "unbounded" meaning there
is no maximum so the element can occur an unlimited
number of times.
13
CARDINALITY CONSTRAINTS: EXAMPLES Sample XSD Description
<xs:element name="Customer_dob" type="xs:date"/>
If we do not specify minOccurs or maxOccurs, then the default values of 1 are used. This means there has to be one and only one occurrence of Customer_dob, i.e. it is mandatory.
<xs:element name="Customer_order" type="xs:integer" minOccurs ="0" maxOccurs="unbounded"/>
If we set minOccurs to 0, then the element is optional. Here, a customer can have from 0 to an unlimited number of Customer_orders.
<xs:element name="Customer_hobbies" type="xs:string" minOccurs="2" maxOccurs="10"/>
Setting both minOccurs and maxOccurs means the element Customer_hobbies must appear at least twice, but no more than 10 times. 14
2/24/2016
8
COMPLEX TYPES
A complex type is a container for other element definitions,
this allows you to specify which child elements an element can
contain.
Let us consider some simple element definitions:
<xs:element name="Customer" type="xs:string"/>
<xs:element name="Customer_dob" type="xs:date"/>
<xs:element name="Customer_address" type="xs:string"/>
<xs:element name="Supplier" type="xs:string"/>
<xs:element name="Supplier_phone" type="xs:integer"/>
<xs:element name="Supplier_address" type="xs:string"/>
Some of these elements should really be represented as child elements:
• "Customer_dob" and "Customer_address" belong to a parent element
"Customer".
• "Supplier_phone" and "Supplier_address" belong to a parent element
"Supplier". 15
<xs:element name="Customer">
<xs:complexType>
<xs:sequence>
<xs:element name="Dob" type="xs:date" />
<xs:element name="Address" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Supplier">
<xs:complexType>
<xs:sequence>
<xs:element name="Phone" type="xs:integer"/>
<xs:element name="Address" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<Customer>
<Dob> 2000-01-12T12:13:14Z </Dob>
<Address> 34 thingy street, someplace, sometown, w1w 8uu </Address>
</Customer>
<Supplier>
<Phone>0123987654</Phone>
<Address>22 whatever place, someplace, sometown, ss1 6gy </Address>
</Supplier>
Example of complex types
• 2 new element type: “Customer” and “Supplier”
• <xs:complexType>: a container for other <xs:element > definitions
• <xs:element > contains <xs:sequence> which in turn has child elements
Example XML
16
2/24/2016
9
TYPE COMPOSITORS
There are three types of compositors <xs:sequence>,
<xs:choice> and <xs:all>
Determine how the child elements contained within them will
appear within the XML document.
Compositor Description
Sequence The child elements in the XML document MUST appear in
the order they are declared in the XSD schema.
Choice Only one of the child elements described in the XSD schema
can appear in the XML document.
All The child elements described in the XSD schema can appear
in the XML document in any order.
17
<xs:element name="Customer">
<xs:complexType>
<xs:sequence>
<xs:element name="Dob" type="xs:date" />
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="Line1" type="xs:string" />
<xs:element name="Line2" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Supplier">
<xs:complexType>
<xs:sequence>
<xs:element name="Phone" type="xs:integer" />
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="Line1" type="xs:string" />
<xs:element name="Line2" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
Example:
Break the ‘Address’
into several fields.
More useful in
practice
18
2/24/2016
10
<xs:complexType name="AddressType">
<xs:sequence>
<xs:element name="Line1" type="xs:string"/>
<xs:element name="Line2" type="xs:string"/>
</xs:sequence>
</xs:complexType>
• It makes more sense to have a
single definition for "Address",
which could then be used by both
customer and supplier.
• This can be done by defining a complexType independently of an
element, and giving it a unique
name.
GLOBAL TYPES
<xs:element name="Customer">
<xs:complexType>
<xs:sequence>
<xs:element name="Dob" type="xs:date"/>
<xs:element name="Address" type="AddressType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="supplier">
<xs:complexType>
<xs:sequence>
<xs:element name="address" type="AddressType"/>
<xs:element name="phone" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
We can now use
the new
AddressType
19
EXAMPLE XML
<Customer>
<Dob> 2000-01-12T12:13:14Z </Dob>
<Address>
<Line1>34 thingy street, someplace</Line1>
<Line2>sometown, w1w8uu </Line2>
</Address>
</Customer>
<Supplier>
<Phone>0123987654</Phone>
<Address>
<Line1>22 whatever place, someplace</Line1>
<Line2>sometown, ss1 6gy </Line2>
</Address>
</Supplier>
20
2/24/2016
11
ATTRIBUTES
An attribute provides extra information within an element
Attributes have name and type properties. Syntax:
<xs:attribute name="x" type="y"/>
Attributes are either optional or mandatory (default is optional)
The "use" property is used to specify if the attribute is optional
or mandatory.
The default and fixed attributes can be specified within the XSD
attribute specification (in the same way as they are for
elements).
21
ATTRIBUTES: EXAMPLES
Sample XSD Sample XML
<xs:element name="Order">
<xs:complexType>
<xs:attribute name="OrderID"
type="xs:int"/>
</xs:complexType>
</xs:element>
<Order OrderID="6"/>
or
<Order/>
<xs:element name="Order">
<xs:complexType>
<xs:attribute name="OrderID"
type="xs:int"
use="optional"/>
</xs:complexType>
</xs:element>
<Order OrderID="6"/>
or
<Order/>
<xs:element name="Order">
<xs:complexType>
<xs:attribute name="OrderID"
type="xs:int"
use="required"/>
</xs:complexType>
</xs:element>
<Order OrderID="6"/>
22
2/24/2016
12
SUMMARY
More Examples: http://www.w3schools.com/schema/schema_example.asp
Two ways of describing the structure of an XML
document
Document Type Definitions (DTDs)
<!DOCTYPE root_element >
<!ELEMENT name allowable_contents>
<ATTLIST el_name att_name att_type default_value >
XML Schemas
<schema>
<xs:element name="x" type="y"/>
<xs:attribute name="x" type="y"/>
23