0% found this document useful (0 votes)
72 views

Lecture 03 - XML Schema

The document provides an overview of XML schemas including: - Schemas define valid elements, attributes, structure and data types for XML documents. - Schemas have advantages over DTDs like richer data types, namespaces and extensibility. - Key schema components include elements, complexTypes, simpleTypes, attributes and groups for defining structure. - Schemas use XML syntax and vocabulary like xs:element and xs:complexType to define valid document structure. - Data types, constraints, inheritance and validation are supported to define valid element content.

Uploaded by

Nguyen Minh Nhat
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Lecture 03 - XML Schema

The document provides an overview of XML schemas including: - Schemas define valid elements, attributes, structure and data types for XML documents. - Schemas have advantages over DTDs like richer data types, namespaces and extensibility. - Key schema components include elements, complexTypes, simpleTypes, attributes and groups for defining structure. - Schemas use XML syntax and vocabulary like xs:element and xs:complexType to define valid document structure. - Data types, constraints, inheritance and validation are supported to define valid element content.

Uploaded by

Nguyen Minh Nhat
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

XML Schema

1
Objectives
2

• Define schema
• Distinguish between a schema and DTD
• Create schema using XML schema vocabulary
• Identify the elements in an XML schema
• Describe validation and constraints
• Explain element inheritance in an schema
Schemas
3
Schemas
4

• DTD though they define document structure and validate XML


documents, but they have some limitations. XML schema has
been introduced to overcome the drawbacks of DTDs.

• An XML Schema defines the valid building blocks of an XML


document.

• The XML Schema language is referred as XML Schema


Definition (XSD)
What is an XML Schema ?
5

• An XML Schema defines:

– Elements and attributes that can appear in a document.

– Which elements are child elements.

– The order and number of child elements.

– Whether an element is empty or can include text

– Data types for elements and attributes

– Default and fixed values for elements and attributes


How to write an XML Schema
6

 • You need two files. They are:

– XML File
– XSD File

 For storing schema documents, the file saved with “.xsd”

extension. Schema documents are XML document and


can have DTDs, DOCTYPE declarations.
XML Schema New Features
7

• Schemas support data types.


• Schemas are portable and efficient.
• Schemas secure data communication.
• Schemas are extensible.
• Schemas catch higher-level mistakes.
• Schemas support namespace.
Limitations of DTD
8

• DTDs are written in a non-XML syntax.


• DTDs are not extensible.
• They have no support for namespaces.
• They only offer extremely limited data typing.
Advantages of Schema over DTD
9

• Richer data types


• Archetypes
• Attributes grouping
• Refinable archetypes
Data Types in Schema
10
XML Schema Vocabulary
11
XML Schema Vocabulary
12

 • Schema Declaration
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<!-- Schema content go here -->

</xs:schema>
Using Schema Declaration
13

With namespace
<?xml version="1.0" encoding="UTF-8"?>
<BookStore
xmlns:ins="https://fpt.edu.vn"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="https://fpt.edu.vn BookStore.xsd">

</BookStore>

Without namespace
<?xml version="1.0" encoding="UTF-8"?>
<BookStore
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:noNamespaceSchemaLocation="BookStore.xsd">

</BookStore>
The element Element
14

 Syntax

<element
name="element-name"
type="element-data-type"
minOccurs="minimum-appear"
maxOccurs="maximum-appear">

<!-- Element content -->

</element>
The element Element
15

• The three attributes of the element “element” are:

– type: used to specify the type of the element

– minOccurs: the minimum number of times the


element must occur
– maxOccurs: the maximum number of times the
element must occur
The element Element
16

 The relationship between the minOccurs and maxOcuurs attribute

minOccur maxOccur Number of times an element


can occur
0 1 0 or 1
3 3 3
0 unbounded Infinitive
1 unbounded At least one
>0 unbounded At least min occurs times
> maxOccurs Any value Error
Any value < minOccurs Error
The datatype Element
17

• It specifies the datatype of an element or attribute.


• It includes the attribute “xs:type‟.
• Values allowed in the xs:type attribute include:
– string
– boolean
– int
– float
–…
The ComplexType element
18

• Schemas assign a type to each element and


attribute it declares.
• Element with complex type may contain nested
elements and have attributes.
• Only elements can contain complex types.
• Complex type element have 4 variations.
The ComplexType element
19

 Empty element

Empty element optionally specify attributes type, but


do not permit content. <img src="cover.jpg"/>

<element name="img">
<complexType>
<attribute name="src" type="string"/>
</complexType>
</element>
The ComplexType element
20

 Only element

These elements can only contain elements and do


not contain attributes.
<element name="Book" minOccurs="0" maxOccurs="unbounded">
<complexType>
<sequence>
<element name="Name" type="string"/>
<element name="Author" type="string"/>
<element name="Price" type="float"/>
</sequence>
</complexType>
</element>
The ComplexType element
21

 Only text and attribute

These elements can only contain text and have


attributes. <Price currency="usd">12</Price>

<element name="Price">
<complexType>
<simpleContent>
<extension base="string">
<attribute name="currency" type="string"/>
</extension>
</simpleContent>
</complexType>
</element>
The ComplexType element
22

 Mixed

These are elements that can contain text content as


well as sub elements with-in the element.
<element name="Book" minOccurs="0" maxOccurs="unbounded">
<complexType mixed="true">
<sequence>
<element name="Name" type="string"/>
<element name="Author" type="string"/>
<element name="Price" type="float"/>
</sequence>
</complexType>
</element>
The ComplexType element
23

 We can define complex type out side and

reference to it.
<xs:element name="Book" type="bookType"
minOccurs="0" maxOccurs="unbounded"/>

<xs:complexType mixed="true" name="bookType">


<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Author" type="xs:string"/>
<xs:element name="Price" type="xs:string"/>
</xs:sequence>
</xs:complexType>
The group constructs
24

 XML Schema Language provides three grouping

constructs that how ordering of individual


element.
□ all
□ choice
□ sequence
The group constructs
25

 xs:all - Each element in the group must occur at

most once, but that order is not important.

 xs:choice - It will allow only one of the choices to

appear.

 xs:sequence - It will allow only one of the

choices to appear.
Simple Type
26

 • XML Schema supports a library of build-in

datatypes. However, developers can declare


new user-defined datatypes.
• A custom user defined datatype can be
created using the <simpleType> definition.
Simple Type
27

 Syntax

<xs:simpleType name="simple-type-name">
<xs:restriction base="base-data-type">
<xs:constraint value="constraint-value"/>
</xs:restriction>
</xs:simpleType>
Simple Type - Restriction
28

 Restrictions can be specified for the simpleType

elements andrestriction types are declared using the


<restriction> declaration.
<xs:simpleType name="priceType">
<xs:restriction base="xs:float">

</xs:restriction>
</xs:simpleType>

Base data type for


simple type “price”
Restrictions - constraint
29

• Constraint are used to restrict the set or range of


values a datatype can contain.
• The value range defined by the facet must be equal
to or narrower than the value range of the base type.
• There are 12 constraint, declared using a common
syntax.
Restrictions - constraint
30

Constraint Description
enumeration Defines a list of acceptable values
fractionDigits Specifies the maximum number of decimal places allowed. Must
be equal to or greater than zero
length Specifies the exact number of characters or list items allowed.
Must be equal to or greater than zero
maxExclusive Specifies the upper bounds for numeric values (the value must be
less than this value)
maxInclusive Specifies the upper bounds for numeric values (the value must be
less than or equal to this value)
maxLength Specifies the maximum number of characters or list items
allowed. Must be equal to or greater than zero
Restrictions - constraint
31

minExclusive Specifies the lower bounds for numeric values (the value must be
greater than this value)
minInclusive Specifies the lower bounds for numeric values (the value must be
greater than or equal to this value)
minLength Specifies the minimum number of characters or list items
allowed. Must be equal to or greater than zero
pattern Defines the exact sequence of characters that are acceptable

totalDigits Specifies the exact number of digits allowed. Must be greater


than zero
whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage
returns) is handled
The Attribute
32

• It is similar to “Element”.
• It defines the attribute type.
• The fields of the attribute element are:
– name
– type
– use
– default/fixed
33

 Attribute syntax
<xs:attribute
name="attribute-name"
type="simple-type"
use="how-to-use"
default/fixed="value"/>

 type: xs:string, xs:float …

 use: required, optional, prohibited


Q&A
34

You might also like