In this chapter students will be able to: review the basics of creating an XML document, imposing structure on XML documents using document type definition DTD, strengthening the data-modeling capabilities of XML using XML Schemas.
Trang 1CSC 330 E-Commerce
Teacher
Ahmed Mumtaz Mustehsan
GM-IT CIIT Islamabad
Virtual Campus, CIIT
COMSATS Institute of Information Technology
T2-Lecture-5
Trang 2eXtensable Markup Language
(XML) Part - III
For Lecture Material/Slides Thanks to: www.w3schools.com
Trang 3Part 1:
Review The basics of creating an XML document
Part 2:
Imposing Structure on XML Documents using
Document Type Definition DTD
Trang 4Part 1:
Review The basics of creating an XML
document
Trang 5Part 1: A review of XML
An Extensible Markup Language (XML) document
describes the structure of data.
XML and HTML have a similar syntax … both
derived from SGML
XML has no mechanism to specify the format for
presenting data to the user
An XML document resides in its own file with an
‘.xml’ extension
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-5
Trang 6Main Components of an XML Document
Elements: <hello>
Attributes: <item id=“33905”>
Entities: < (<)
Trang 7The Basic Rules
XML is case sensitive
All start tags must have end tags
Elements must be properly nested
XML declaration is the first statement
Every document must contain a root element
Attribute values must have quotation marks
Certain characters are reserved for parsing
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-7
Trang 8XML is different from HTML
HTML is a Hyper Text Markup language
◦Designed for a specific application, namely,
displaying, viewing, presenting and linking
hypertext documents
XML describes structure (organization of data)
XML is a subset of SGML (Standard Generalized SGML (Standard Generalized
Markup Language)
Trang 9An Address Book as an XML document
Trang 10Important Features of XML
No fixed set of tags
User is allowed to introduce New tags
Already defined set of tags can also be used
Trang 11Features of XML (cont’d)
XML supports internationalization through Unicode
Web services (e.g., e-commerce) require
exchanging data between various applications that run on different platforms
XML (with the support of namespaces) is the best
option for data exchange on the Web
XML is a data model
◦Similar to the semi-structured-data-model
XML has follow the concept of DTD and the more impressive XML Schema
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-11
Trang 12XML family
Limited styling of XML can be done with CSS
Document Type Definitions (DTDs) impose structure
on XML documents
XML Schemas strengthen the data-modeling
capabilities of XML
XPath is a language for accessing XML documents
XLink and XPointer support cross-references
XSLT is a language for transforming XML documents into other XML documents such as XHTML, for
viewing XML files
XQuery is a language for querying XML documents
Trang 13DTD : (Document Type Definition)
Imposing Structure on
XML Documents
using
Trang 14XML defines structure of the document
Some XML files only contain text documents with
tags that contain metadata and describe the
Trang 15Document Type Definitions
Document Type Definitions (DTDs) impose structure
on XML documents
There is some relationship between a DTD and a
schema, but it is not close hence the need for
additional “typing” systems exists
The DTD is a syntactic specification
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-15
Trang 16Document Type Definitions
A description of legal, valid data further contributes
to the interoperability and efficiency of using XML
A single DTD ensures a common format for each
XML document that references it
An application can use a standard DTD to verify
that data that it receives from the outside world is
valid
An XML document that conforms to the rules within
a DTD is said to be valid document
If the XML document does not follow the rules
contained within the DTD, a parser generates an
error
Trang 17A DTD adds syntactical requirements in addition to
the well-formed requirement
It helps in eliminating errors when creating or
editing XML documents
It clarifies the intended semantics
It simplifies the processing of XML documents
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-17
Trang 18An Example
In an address book, where can a phone number
appear?
Under <person>, under <name> or under both?
If we have to check for all possibilities, processing
takes longer and it may not be clear to whom a phone belongs to?
Trang 19Example: An Address Book
< person >
< name > Homer Simpson </ name >
< greet > Dr H Simpson </ greet >
< addr > 1234 Springwater Road </ addr >
< addr > Springfield USA, 98765 </ addr >
As many
as needed
As many address lines as needed (in order)
At most one greeting Exactly one name
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-19
Trang 20Specifying the Structure
name to specify a name element
greet? to specify an optional
Trang 21Specifying the Structure (cont’d)
So the whole structure of a person entry is
specified by
name, greet?, addr*, (tel | fax)*, email*
This is known as a regular expression
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-21
Trang 22Element Type Definition
for each element type E, a declaration of the form:
<!ELEMENT E P>
where P is a regular expression, i.e.,
P ::= EMPTY | ANY | #PCDATA | E’ |
Trang 23Summary of Regular Expressions
A The tag (i.e., element) A occurs
e1,e2 The expression e1 followed by e2
Trang 24The Definition of an Element Consists of Exactly One of the Following
A regular expression (as defined earlier)
EMPTY means that the element has no content
ANY means that content can be any mixture of
PCDATA and elements defined in the DTD
Mixed content which is defined as described on the
next slide :
(#PCDATA)
Trang 25The Definition of Mixed Content
Mixed content is described by a repeatable OR group
* The group can be repeated 0 or more times
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-25
Trang 26Some Example DTD Declarations
Example 1: Elements with Data
<! ELEMENT Month ( #PCDATA) > <! DTD declaration of an
element->
<! —Valid usage within XML file >
< Month > April </ Month >
< Month > This is a month </ Month >
<! —Invalid usage within XML file, Month can’t have children! >
<Month>
<January>Jan</January>
Trang 27Some Example DTD Declarations
have a single child element, include the element name within the
parenthesis.
<! ELEMENT House ( Address )>
<! —A house element has a single child address >
< House > <! —Valid usage within XML file >
< Address > 5 Park Road Chak Shahzad, Islamabad </ Address >
</ House >
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-27
Trang 28Some Example DTD Declarations
describes multiple children using a sequence, or a list of elements separated by commas The XML file must contain one of each
element in the specified order.
<! DTD declaration of an element >
<! ELEMENT address ( person , street , city , zip )>
<! ELEMENT person ( #PCDATA) >
<! ELEMENT street ( #PCDATA) >
<! ELEMENT city ( #PCDATA) >
<! ELEMENT zip ( #PCDATA) >
<! —Valid usage within XML file >
< address >
< person > Tariq Rasheed </ person >
< street > 9 th Eveneue </ street >
Trang 29Cautions concerning DTDs
All element declarations begin with <! and end
with >
<! ELEMENT Address >
The ELEMENT declaration is case sensitive
The programmer must declare all elements within
an XML file
Elements declared with the #PCDATA content
model can not have children
When describing sequences, (e1,e2) the XML
document must contain exactly those elements in exactly that order
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-29
Trang 30An Address-Book XML Document with an Internal DTD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE addressbook [
<!ELEMENT addressbook (person*)>
<!ELEMENT person
<!ELEMENT name (#PCDATA)>
<!ELEMENT greet (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT tel (#PCDATA)>
<!ELEMENT fax (#PCDATA)>
<!ELEMENT email (#PCDATA)>
The name of the DTD is addressbook
The syntax
of a DTD is
not XML
syntax
Trang 31The Address-Book XML Document
<addressbook>
<person>
<name> Ahmed Mumtaz</name>
<greet> Prof. Mumtaz</greet>
<email> am@yahoo.com </email>
</person>
</addressbook>
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-31
Trang 32XML documents are similar to database files
Trang 33XML Representation of school database
Trang 35Well-Formed XML Documents
An XML document (with or without a DTD) is
well-formed if
◦Tags are syntactically correct
◦Every tag has an end tag
◦Tags are properly nested
◦There is a root tag
◦A start tag does not have two occurrences of the
same attribute
An XML document must be well formed
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-35
Trang 36Strengthen the data-modeling
capabilities of XML
Using XML Schemas
Trang 37What is an XML Schema?
The purpose of an XML Schema is to define the legal
building blocks of an XML document, just like a DTD
An XML Schema:
defines elements that can appear in a document
defines attributes that can appear in a document
defines which elements are child elements
defines the order of child elements
defines the number of child elements
defines whether an element is empty or can include
text
defines data types for elements and attributes
defines default and fixed values for elements and
attributes
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-37
Trang 38XML Schemas are the Successors of DTDs
It is expected that XML Schemas will be used in most
Web applications as a replacement for DTDs
Here are some reasons:
XML Schemas are extensible to future additions
XML Schemas are richer and more powerful than DTDs
XML Schemas are written in XML
XML Schemas support data types
XML Schemas support namespaces
Trang 39<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"/>
Trang 40XML Schema
The Schema in previous example is interpreted like this:
Explanation of the Example:
<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
Trang 41Why Use an XML Schema?
With XML Schema, the XML files can carry a
description of its own format
With XML Schema, independent groups of people
can agree on a standard for interchanging data
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-41
Trang 42XML Schemas use XML Syntax
Another great strength about XML Schemas is that they are written in XML:
You don't have to learn a new language
You can use your XML editor to edit your Schema
Trang 43XML Schemas Support Data Types
One of the greatest strength of XML Schemas is the support for data types:
It is easier to describe document content
It is easier to define restrictions on data
It is easier to validate the correctness of data
It is easier to convert data between different data
types
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-43
Trang 44XML Schemas Support Data Types
One of the greatest strength of XML Schemas is the support for data types:
It is easier to describe document content
It is easier to define restrictions on data
It is easier to validate the correctness of data
It is easier to convert data between different data
types
Trang 45XML Schemas Secure Data Communication
When sending data from a sender to a receiver, it is
essential that both parts have the same "expectations" about the content
With XML Schemas, the sender can describe the data
in a way that the receiver will understand
A date like: "03-11-2004" will, in some countries, be
interpreted as 3.November and in other countries as 11.March
However, an XML element with a data type like this:
<date type="date">2004-03-11</date>
ensures a mutual understanding of the content,
because the XML data type "date" requires the format
"YYYY-MM-DD"
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-45
Trang 46XML Schemas are Extensible
XML Schemas are extensible, because they are written
in XML
With an extensible Schema definition you can:
Reuse your Schema in other Schemas
Create your own data types derived from the standard types
Reference multiple schemas in the same document
Trang 47Well-Formed is not Enough
A well-formed XML document is a document that
conforms to the XML syntax rules, like:
it must begin with the XML declaration
it must have one unique root element
start-tags must have matching end-tags
elements are case sensitive
all elements must be closed
all elements must be properly nested
all attribute values must be quoted
entities must be used for special characters
T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com
1-47
Trang 48The End eXtensable Markup Language
(XML) Part – III
Thank You