XML Documents
XML Documents
Miao Qiao
SEAT
Massey University
1
XML: Schema Definition Language
• Well-formed XML: adheres to basic structural requirements.
• Valid XML: adheres to content-specific specification.
Two Schema Definition Languages for XML:
• Document Type Definition (DTD)
• XML Schema Definition (XSD)
2
XML: Schema Definition, Schema, and Instance Document
• XML Schema: the most widely used schema standard. XML schema
is developed and maintained by W3C, and is designed to define a
broad range of document structures.
URL: http://www.w3.org/2001/XMLSchema
• XML Schema Definition: an XML document that defines an XML
vocabulary.
• Instance Document: the document to be validated when apply XML
schema to a specific XML document.
3
http://www.w3.org/2001/XMLSchema
XML Schema and DTD
XML schema is more powerful than DTD.
XML Schema DTD
Document language XML Extended Backus Naur Form (EBNF)
Standard multiple standard single standard
Supported data type 44(19 primitives + 25 derived) 10
Mixed content easy to develop difficult to develop
Namespace completely supported only namespace prefix are supported
Entities no yes
Supported Keys global/typed keys only global keys
4
XML: Namespace
• Purpose: to avoid duplicated tag names in merging XML files.
• Method: prepend each tag with a Unique Resource Identifier (URI).
• Namespace: define an abbreviation for URIs
An instance document with namespace:
5
XSD: Start a Schema File
• By convention, the namespace prefix, typically xsd or xs, is assigned
to the XML Schema namespace to identify elements and attributes
that belong to the XML Schema vocabulary.
• The usual form of an Schema document is:
content
6
XSD: an Example
See documents “Bookstore-XSD.xml” and “Bookstore.xsd”.
7
XSD: Element Type
• Simple type: an XML element that can contain only text — no
attributes / subelements.
• Complex type: an XML element that contains subelements or
attributes.
8
XSD: Define a Simple Type Element
Define a simple type element:
An instance of “Last_Name”:
Common built-in types:
• xsd:string
• xsd:decimal
• xsd:integer
• xsd:boolean
• xsd:date
• xsd:time
Their instances are all texts.
9
XSD: Built-in Data Type https://www.w3.org/TR/xmlschema-2/
10
https://www.w3.org/TR/xmlschema-2/
XSD: Define a Complex Type
Define a complex type called BookType:
Declaration…
Define an element of the complex type:
Or define the element directly:
Declaration…
11
XSD: Contents of a Complex Type
Contents of a complex type:
• Attribute
• Basic text
• Nested child element
12
XSD: Contents of a Complex Type
Contents of a complex type:
• Attribute
• Basic text
• Nested child element
12
XSD: Define an Attribute
Define an attribute:
Define an instance with the attribute:
…
13
XSD: Attribute Use Indicators
Use indicator: the requirement on the appearance of an attribute.
Use is one of the following three values:
• required: the attribute must always appear with the element.
• optional (default): the use of the attribute is optional with the
element.
• prohibited: the attribute cannot be used with the element.
Example:
See documents “Bookstore.xsd” and “Bookstore-XSD.xml”.
14
XSD: Contents of a Complex Type
Contents of a complex type:
• Attribute
• Basic text
• Nested child element
A complex-typed element can have any combination of the three above:
1 Empty element: contain only attributes.
2 Basic text + attributes
3 Nested child elements
4 Nested child elements + attributes
5 Mixed: Nested child elements + attributes + basic text
15
XSD: Contents of a Complex Type
Contents of a complex type:
• Attribute
• Basic text
• Nested child element
A complex-typed element can have any combination of the three above:
1 Empty element: contain only attributes.
2 Basic text + attributes
3 Nested child elements
4 Nested child elements + attributes
5 Mixed: Nested child elements + attributes + basic text
15
XSD: Define a Empty Element
Define an empty element of Book:
Define an instance of Book:
16
XSD: Contents of a Complex Type
Contents of a complex type:
• Attribute
• Basic text
• Nested child element
A complex-typed element can have any combination of the three above:
1 Empty element: contain only attributes.
2 Basic text + attributes
3 Nested child elements
4 Nested child elements + attributes
5 Mixed: Nested child elements + attributes + basic text
17
XSD: Define an Basic Text with Attributes
Define an element of Book:
Define an instance of Book:
A First Course in Database Systems
18
XSD: Contents of a Complex Type
Contents of a complex type:
• Attribute
• Basic text
• Nested child element
A complex-typed element can have any combination of the three above:
1 Empty element: contain only attributes.
2 Basic text + attributes
3 Nested child elements
4 Nested child elements + attributes
5 Mixed: Nested child elements + attributes + basic text
19
XSD:Define an Element with Nested Child Elements
Define an element of Author:
Define an instance of Author:
20
XSD: Order Indicators for Nested Child Elements
Order indicators: to specify the order of the appearances of the child
elements.
• All: the child elements can appear in any order, and that each child
element must occur only once.
• Choice: either one child element or another can occur.
• Sequence: the child elements must appear in a specific order.
See documents “Bookstore.xsd” and “Bookstore-XSD.xml”.
21
XSD: Occurrence Indicators for Nested Child Elements
Occurrence indicator: to define how often an element can occur.
The default value for both maxOccurs and minOccurs is 1.
See documents “Bookstore.xsd” and “Bookstore-XSD.xml”.
22
XSD: Contents of a Complex Type
Contents of a complex type:
• Attribute
• Basic text
• Nested child element
A complex-typed element can have any combination of the three above:
1 Empty element: contain only attributes.
2 Basic text + attributes
3 Nested child elements
4 Nested child elements + attributes
5 Mixed: Nested child elements + attributes + basic text
23
XSD: Define an Element with Child Elements + Attributes
Define an element “Book” with both child elements and attributes:
See documents “Bookstore.xsd” and “Bookstore-XSD.xml” for an instance.
24
XSD: Contents of a Complex Type
Contents of a complex type:
• Attribute
• Basic text
• Nested child element
A complex-typed element can have any combination of the three above:
1 Empty element: contain only attributes.
2 Basic text + attributes
3 Nested child elements
4 Nested child elements + attributes
5 Mixed: Nested child elements + attributes + basic text
25
XSD: Define a Mixed Element
Define a mixed element by setting its complextype attribute mixed=”true”.
An instance of letter:
Dear Mr.
Your order
26
XSD: Keys and Foreign Keys
Specify an attribute (ISBN) of an element (Book) is a key:
Specify an attribute (book) of an element (Book/Remark/BookRef) is a
foreign key of key “BookKey” defined above:
See documents “Bookstore-key.xsd” and “Bookstore-XSD-key.xml”.
27
XSD: Keys and Foreign Keys
Specify an attribute (ISBN) of an element (Book) is a key:
Specify an attribute (book) of an element (Book/Remark/BookRef) is a
foreign key of key “BookKey” defined above:
See documents “Bookstore-key.xsd” and “Bookstore-XSD-key.xml”.
27
XSD: Keys and Foreign Keys
Specify an attribute (ISBN) of an element (Book) is a key:
Specify an attribute (book) of an element (Book/Remark/BookRef) is a
foreign key of key “BookKey” defined above:
See documents “Bookstore-key.xsd” and “Bookstore-XSD-key.xml”.
27
References
• W3Cschool:
https://www.w3schools.com/xml/schema_intro.asp
• W3Crecommendation:
https://www.w3.org/TR/xmlschema-1/
https://www.w3.org/TR/xmlschema-2/
• Chapter 10, “Database System Concepts (Fifth Edition)”, A.
Silberschatz, H. F. Korth, S. Sudarshan.
• Introduction to Databases, Stanford University.
28
https://www.w3schools.com/xml/schema_intro.asp
https://www.w3.org/TR/xmlschema-1/
https://www.w3.org/TR/xmlschema-2/