Вы находитесь на странице: 1из 16

CSE2045Y

Web Application Development

Lecture 4
XSD (XML Schema Definition)
PART 1

Agenda
XML Validation
XML Namespaces
XML Schema Definition
XML Schema Data Types
XML Schema Elements
SimpleType
ComplexType
Schema Element Declaration

2
Recap: Well-formed XML
Syntax Rules:
XML Documents Must Have a Root Element.
All XML Elements Must Have a Closing Tag.
XML Tags are Case Sensitive.
XML Elements Must be Properly Nested.
XML Attribute Values Must be Quoted.

Naming Rules:
Element names are case-sensitive.
Element names must start with a letter or underscore.
Element names cannot start with the letters xml (or XML, or
Xml, etc).
Element names can contain letters, digits, hyphens,
underscores, and periods.
Element names cannot contain spaces.
3

XML Validation (1)


Even if an XML document is well-formed, it
may not be valid.

To check the validity of an XML document, it


should be validate against an XML schema.

The XML Schema is known as XSD (XML


Schema Definition)
4
XML Validation (2)
XSD can check the following:
Structure of an XML file.
Type of data (datatype) of the XML elements.
Legal values (restriction) for the XML elements.

Validating an XML against an XSD requires a


special parser; the schema validator.

XML Namespaces
Before considering XML Schemas and
Validation, it is important to know the use of
XML Namespaces.

Short Definition: Group of elements that


belong together

XML Namespaces provide a method to avoid


element name conflicts.
6
Why we need Namespaces?
In XML, element names are defined by the
developer.

This often results in a conflict when trying to mix


XML documents from different XML applications.

XML Namespaces are therefore used to provide


uniquely named elements and attributes in
an XML document.

XML Namespaces Example


This XML carries HTML
table information.

This XML carries


information about
a table.

If these XML fragments were added together, there


would be a name conflict:
Both contain a <table> element, but the elements
have different content and meaning.
An XML parser will not know how to handle these
differences !
Solving the XML Name conflict
using a Prefix
Name conflicts in XML can easily be avoided using
a name prefix.

This XML carries information about an HTML


table, and a piece of furniture:

There will be no
conflict because the
two <table> elements
have different
names.

XML Namespaces
The xmlns Attribute
When using prefixes in XML, a namespace for the
prefix must be defined.
The namespace can be defined by an xmlns attribute
in the start tag of an element.
Namespaces can also be declared in the XML root
element.

The namespace declaration has the following


syntax:
xmlns:prefix="URI"

10
Namespace declaration
An XML Namespace is declared using the
reserved XML attribute xmlns:prefix or xmlns, the
value of which must be a valid namespace name.

Examples:
xmlns:xhtml=http://www.w3.org/1999/xhtml
Any element or attribute whose name starts with the prefix
"xhtml:" is considered to be in the XHTML namespace
xmlns=http://www.w3.org/1999/xhtml
It is also possible to declare a default namespace. Any
element without a namespace prefix is considered to be in
the XHTML namespace.
11

Namespace Example
(Declaration in the XML root element)

12
XML Schema
XML schema describes the structure of an XML
instance document by defining what each
element must or may contain.

An element containing child elements:

An element containing text only:

13

XML Schema Data Types


XML Schema has a lot of built-in data types. The
most common types are:
string
boolean
decimal
float
double
duration
dateTime
time
date.

14
XML Schema Example (1)
Email.xsd

In this example, Email.xsd defines the element Email


which contains a sequence of four elements: Subject ,
From, To and Message. 15

XML Schema Example (2)


Although it is not required, it is a common practice to
use the xs qualifier to identify schema elements and
types.

The document element of XML schemas is xs:schema.


It takes the attribute xmlns:xs with the value of:

http://www.w3.org/2001/XMLSchema

indicating that the document should follow the rules of


this XML Schema.
16
XML Schema Example (3)
Email.xml

17

How to validate an XML file


against a Schema?
To validate an XML file, we can use:
An online validator:
http://www.utilities-online.info/xsdvalidation

A PHP validator: This validator is available on LCMS


and will run on XAMPP.
$xmlfile and $xsdfile variables should be modified in
xmlvalidator.php.

Both validators will require an XML file and an


XSD file.
18
Schema Elements

https://dhhmzgirqh63s.cloudfront.net/27930.gif 19

Schema Element Declaration


An allowable content is determined
by its type. Element types are:
1. Simple types
2. Complex types

20
Simple Type Element
Simple type element contains only text.
Example:
<Subject>Web Application Development</Subject>

The corresponding schema definition is:

<xs:element name="Subject" type="xs:string" />

21

Activity 1
Write the schema definitions for the following
XML elements:

<lastName>Smith</lastName>
<age>35</age>
<dateofBirth>1985-05-17</dateofBirth>

Assume appropriate data types.


22
Complex Type Element
A complex element is an XML element that contains other
elements and/or attributes.
The content model characterizes the types of children
elements and text nodes that can be included in a Complex
element:
"empty": no children elements nor text nodes are
expected
"simple": only text nodes are accepted.
"complex": only sub-elements are expected.
: both text nodes and sub-elements can be
present.

23

Complex Type Element


(Empty)
A complex XML element <product>, which is
empty, but contains an attribute:
<product pid

The corresponding XSD schema fragment:


<xs:element name="product">
<xs:complexType>
<xs:attribute name="prodid xs:integer"/>
</xs:complexType>
</xs:element>
24
Complex Type Element
(SimpleContent)
Consider the following XML fragment:
<title lang="en">
Introduction to XML
</title>
The corresponding Schema fragment:

25

Complex Type Element


(Complex Content)
Consider the following XML fragment:
<book bookid="125">
<title lang="en"> XSD injection</title>
<reviewer>Reviewer 1</reviewer>
<reviewer>Reviewer 2</reviewer>
</book>

A book can have 0(optional) or up to 4 reviewers:


minOccurs 0
maxOccurs 4

Write the schema?


26
Complex Type Element
(Complex Content)

27

Activity 2
Write the schema definition for the following
XML file:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<lastName>Smith</lastName>
<age>35</age>
<dateofBirth>1985-05-17</dateofBirth>
</person>

Refer to Activity 1
28
Complex Type Element
(Mixed Content)
A mixed complex type element can contain
attributes, elements, and text.

An XML element, "letter", that contains both


text and other elements:
<letter>
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>
29

Complex Type Element


(Mixed Content)
The following schema declares the "letter" element:
<xs:element name="letter">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:Integer"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>

Note: To enable character data to appear between


the child-elements of "letter", the mixed attribute
must be set to "true" 30
Activity 3
Modify Activity 2 such that the <person>
element can now accept mixed content.

31

References
https://www.w3schools.com/xml/
https://www.w3schools.com/xml/xml_namespac
es.asp
https://www.w3schools.com/xml/schema_intro.a
sp
https://www.webucator.com/tutorial/learn-xml-
schema/xml-schema-basics/a-first-look-
reading.cfm
https://www.w3schools.com/xml/schema_compl
ex_mixed.asp

32

Вам также может понравиться