Top Banner
1 Dr Alexiei Dingli XML Technologies X-Schema
41

1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

Jan 12, 2016

Download

Documents

Louise Wilson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

1

Dr Alexiei Dingli

XML Technologies

X-Schema

Page 2: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

2

• XML-based alternative to DTD

• Describes the structure of an XML document

• Also referred to as XML Schema Definition (XSD)

What is X-Schema? (1)

Page 3: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

3

• 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

What is X-Schema? (2)

Page 4: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

4

• 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

X-Schema vrs DTD

Page 5: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

5

• describe allowable document content

• validate the correctness of data

• work with data from a database

• define restrictions on data

• define data formats

• convert data between different data types

X-Schema makes it easy to

Page 6: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

6

• We know the language

• Can be edited with XML editors

• Can be parsed with XML parsers

• Can be manipulated with XML DOM

• Can be transformed with XSLT

Why is it better to use an XML based schema?

Page 7: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

7

• Ensure secure data communication– 3/11/2004 is the 3rd of November or the 11th of

March?

• Can reuse schemas

• Use one or many in the same document

• Create own data types

Benefits of X-Schemas

Page 8: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

8

<?xml version="1.0"?>

<note xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="note.xsd">

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

How to use X-Schema

Page 9: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

9

<?xml version="1.0"?>

<xs:schema>

<xs:element name="note">

<xs:complexType>

<xs:sequence>

<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"/>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

The actual schema

Page 10: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

10

• Root element of any schema

• May contain attributes

– xmlns:xs="http://www.w3.org/2001/XMLSchema"• Indicate the XSchema namespace

– targetNamespace="http://www.um.edu.mt"• Indicate that the elements defined (to, from, etc) come from the

above namespace

– xmlns="http://www.um.edu.mt"• Sets the default namespace

– elementFormDefault="qualified“• Qualified = all elements can be validated

• Unqualified = only root element can be validated with the namespace

<schema> element

Page 11: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

11

• A simple element contains only text

• But it can have different types or restrictions (facets)

• <xs:element name="xxx" type="yyy"/>

• <xs:element name=“age" type=“xs:integer"/>

Elements

Page 12: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

12

• xs:string

• xs:decimal

• xs:integer

• xs:boolean

• xs:date

• xs:time

Element Built-In Types

Page 13: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

13

<xs:element name="color" type="xs:string" default="red"/>

<xs:element name="color" type="xs:string" fixed="red"/>

Default and Fixed types

Page 14: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

14

• Simple elements cannot have attributes

• Only complex elements have attributes

• An attribute is declared as a simple type

<xs:attribute name="xxx" type="yyy"/>

Attributes

Page 15: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

15

<xs:attribute name="lang" type="xs:string" default="EN"/>

<xs:attribute name="lang" type="xs:string" fixed="EN"/>

<xs:attribute name="lang" type="xs:string" use="required"/>

* Optional by default (unless specified)

Default, Fixed and Optional attributes

Page 16: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

16

<xs:element name="age">

<xs:simpleType>

<xs:restriction base="xs:integer">

<xs:minInclusive value="0"/>

<xs:maxInclusive value="120"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

Restrictions (Facets) - Range

Page 17: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

17

<xs:element name="car">

<xs:simpleType>

<xs:restriction base="xs:string">

<xs:enumeration value="Audi"/>

<xs:enumeration value="Golf"/>

<xs:enumeration value="BMW"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

Restrictions (Facets) – Set 1

Page 18: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

18

<xs:element name="car" type="cType"/>

<xs:simpleType name="cType">

<xs:restriction base="xs:string">

<xs:enumeration value="Audi"/>

<xs:enumeration value="Golf"/>

<xs:enumeration value="BMW"/>

</xs:restriction>

</xs:simpleType>

Restrictions (Facets) – Set 2

Page 19: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

19

<xs:element name="letter"> <xs:simpleType>

<xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

Restrictions (Facets) - Pattern

Page 20: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

20

Regular expressions example

[A-Z] any letter uppercase

[A-Z][A-Z] two letters uppercase

[a-zA-Z] any letter any case

[xyz] x or y or z

[0-9] any number one digit

([A-Z])+ one or more

([A-Z])* zero or more

male|female either or

[a-zA-Z]{8} exactly 8 characters

Restrictions (Facets) - Patterns

Page 21: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

21

<xs:element name="address">

<xs:simpleType>

<xs:restriction base="xs:string">

<xs:whiteSpace value="preserve"/> </xs:restriction>

</xs:simpleType>

</xs:element>

Restricting white space (1)

Page 22: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

22

• Value can be

– Preserve• Do not remove white space

– Replace• Replace line feeds, tabs, spaces, and carriage returns with spaces

– Collapse• Same as replace but collapse multiple spaces into one

Restricting white space (2)

Page 23: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

23

<xs:element name="password">

<xs:simpleType>

<xs:restriction base="xs:string">

<xs:minLength value="5"/>

<xs:maxLength value="8"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

Restrictions (Facets) - Length

Page 24: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

24

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

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

Summarising Restrictions

Page 25: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

25

1. empty elements

2. elements that contain only other elements

3. elements that contain only text

4. elements that contain both other elements and text

Complex Elements

Page 26: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

26

<xs:element name="product">

<xs:complexType>

<xs:attribute name="prodid" type="xs:positiveInteger"/>

</xs:complexType>

</xs:element>

Ex

<product prodid="1345" />

Complex Empty Elements

Page 27: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

27

• Contains only other elements

<xs:element name="person">

<xs:complexType>

<xs:sequence>

<xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence>

</xs:complexType>

</xs:element>

Complex Elements Only

Page 28: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

28

<person>

<firstname>John</firstname>

<lastname>Smith</lastname>

</person>

Complex Elements Only Ex

Page 29: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

29

<xs:element name="shoesize">

<xs:complexType>

<xs:simpleContent>

<xs:extension base="xs:integer">

<xs:attribute name="country" type="xs:string" />

</xs:extension>

</xs:simpleContent>

</xs:complexType>

</xs:element>

Eg

<shoesize country="france">35</shoesize>

Complex TextOnly Element

Page 30: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

30

<xs:element name="letter">

<xs:complexType mixed="true">

<xs:sequence>

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

<xs:element name="orderid" type="xs:positiveInteger"/>

<xs:element name="shipdate" type="xs:date"/>

</xs:sequence>

</xs:complexType>

</xs:element>

Complex Mixed Content

Page 31: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

31

• Order indicators:– All

• All must occur but in any order

– Choice• Either or can occur

– Sequence• Must appear in a specified sequence

• Occurrence indicators:– maxOccurs– minOccurs

• Group indicators:Group elements or attributes together and allow for reuse

– Group name – attributeGroup name

7 Indicators

Page 32: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

32

<xs:element name="person">

<xs:complexType>

<xs:all>

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

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

</xs:all>

</xs:complexType>

</xs:element>

All, Choice, Sequence indicator

Page 33: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

33

<xs:element name="person">

<xs:complexType>

<xs:sequence>

<xs:element name="child_name" type="xs:string"

maxOccurs="10" minOccurs="0"/>

</xs:sequence>

</xs:complexType>

</xs:element>

Occurrence indicator

Page 34: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

34

<xs:element name="person">

<xs:complexType>

<xs:sequence>

<xs:element name="child_name" type="xs:string"

maxOccurs="10" minOccurs="0"/>

<xs:any minOccurs="0"/>

</xs:sequence>

<xs:anyAttribute/> </xs:complexType>

</xs:element>

*allows for any element or attribute but it has to be defined just the same in another XSchema

<any> and <anyAttribute>

Page 35: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

35

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

<xs:element name=“isem" substitutionGroup="name"/>

Allows for …

<customer>

<isem>John</isem>

</customer>

Substitutions

Page 36: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

36

Name Description

ENTITIES  

ENTITY  

ID A string that represents the ID attribute in XML (only used with schema attributes)

IDREF A string that represents the IDREF attribute in XML (only used with schema attributes)

IDREFS  

language A string that contains a valid language id

Name A string that contains a valid XML name

NCName  

NMTOKEN A string that represents the NMTOKEN attribute in XML (only used with schema attributes)

NMTOKENS  

normalizedString A string that does not contain line feeds, carriage returns, or tabs

string A string

token A string that does not contain line feeds, carriage returns, tabs, leading or trailing spaces, or multiple spaces

String Datatype

Page 37: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

37

Name Description

date Defines a date value

dateTime Defines a date and time value

duration Defines a time interval

gDay Defines a part of a date - the day (DD)

gMonth Defines a part of a date - the month (MM)

gMonthDay Defines a part of a date - the month and day (MM-DD)

gYear Defines a part of a date - the year (YYYY)

gYearMonth Defines a part of a date - the year and month (YYYY-MM)

time Defines a time value

Date Datatype

Page 38: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

38

Name Description

byte A signed 8-bit integer

decimal A decimal value

int A signed 32-bit integer

integer An integer value

long A signed 64-bit integer

negativeInteger An integer containing only negative values ( .., -2, -1.)

nonNegativeInteger An integer containing only non-negative values (0, 1, 2, ..)

nonPositiveInteger An integer containing only non-positive values (.., -2, -1, 0)

positiveInteger An integer containing only positive values (1, 2, ..)

short A signed 16-bit integer

unsignedLong An unsigned 64-bit integer

unsignedInt An unsigned 32-bit integer

unsignedShort An unsigned 16-bit integer

unsignedByte An unsigned 8-bit integer

Numeric Datatype

Page 39: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

39

Example

Page 40: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

40

• Define an XML Vocabulary for a Toaster Markup Language (TML) using XSchema

• It must support features such as:– Print message on toast– Display on LCD– Time stamp

Exercise

Page 41: 1 Dr Alexiei Dingli XML Technologies X-Schema. 2 XML-based alternative to DTD Describes the structure of an XML document Also referred to as XML Schema.

41

Questions?