Lecture 03 - XML Schema
Lecture 03 - 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
– XML File
– XSD File
• Schema Declaration
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
</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>
The element Element
15
Empty element
<element name="img">
<complexType>
<attribute name="src" type="string"/>
</complexType>
</element>
The ComplexType element
20
Only element
<element name="Price">
<complexType>
<simpleContent>
<extension base="string">
<attribute name="currency" type="string"/>
</extension>
</simpleContent>
</complexType>
</element>
The ComplexType element
22
Mixed
reference to it.
<xs:element name="Book" type="bookType"
minOccurs="0" maxOccurs="unbounded"/>
appear.
choices to appear.
Simple Type
26
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
</xs:restriction>
</xs:simpleType>
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
• 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"/>