62
1 Week5 – Schema Why Schema? Schemas vs. DTDs Introduction – W3C vs. Microsoft XDR • Schema, How To? Element Types – Simple vs. Complex • Attributes • Restrictions/Facets Data Types An Example – How to design a schema from scratch?

Week5 – Schema

Embed Size (px)

DESCRIPTION

Week5 – Schema. Why Schema? Schemas vs. DTDs Introduction – W3C vs. Microsoft XDR Schema, How To? Element Types – Simple vs. Complex Attributes Restrictions/Facets Data Types An Example – How to design a schema from scratch?. Why Schema? Schema vs. DTD. DTDs - PowerPoint PPT Presentation

Citation preview

Page 1: Week5 – Schema

1

Week5 – Schema

• Why Schema? Schemas vs. DTDs

• Introduction – W3C vs. Microsoft XDR

• Schema, How To?

• Element Types – Simple vs. Complex

• Attributes

• Restrictions/Facets

• Data Types

• An Example – How to design a schema from scratch?

Page 2: Week5 – Schema

2

Why Schema? Schema vs. DTD

• DTDs– Not flexible enough to meet certain programming needs

• Cannot be manipulated (searched, transformed, etc.)

• Not XML documents (EBNF)

• Schemas– Alternative to DTDs

– XML documents (using XML syntax)

– Two major models• W3C XML Schema

– Early development stage (at time of this writing)

• Microsoft XML Data-Reduced (XDR)

Page 3: Week5 – Schema

3

Why Schema? Schema vs. DTD (cont.)

• DTDs– Cannot ensure proper element content

<quantity>hello</quantity> is valid

– Use EBNF grammar

<ElementType name=“quantity” content=“textOnly” model=“closed” dt:type=“int”>

<!DOCTYPE quantity [ !ELEMENT quantity (#PCDATA)]>

• Schema (in XDR format)– Can ensure proper element content – different data types

supported <quantity>hello</quantity> is invalid

– Use XML syntax

Page 4: Week5 – Schema

4

Why Schema?

• There are a number of reasons why XML Schema is better than DTD.

• One of the greatest strengths of XML Schemas is the support for data types.

• With the support for data types:

– It is easier to describe permissible document content

– It is easier to validate the correctness of data

– It is easier to work with data from a database

– It is easier to define data facets (restrictions on data)

– It is easier to define data patterns (data formats)

– It is easier to convert data between different data types

Page 5: Week5 – Schema

5

Why Schema? (cont.)

• XML Schemas use XML Syntax– Another great strength about XML Schemas is that they are written in XML.– Because XML Schemas are written in XML:

• You don't have to learn another language • You can use your XML editor to edit your Schema files • You can use your XML parser to parse your Schema files • You can manipulate your Schema with the XML DOM (API) • You can transform your Schema with XSLT

• XML Schemas Secure Data Communication– Data format agreement from both senders and receivers: the same "expectations"

about the content.

– With XML Schemas, the sender can describe the data in a way that the receiver will understand.

– Data Confusion: A date like this: "03-11-2004" will, in some countries, be interpreted as 3. November and in other countries as 11. March, but 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.

Page 6: Week5 – Schema

6

Why Schema? (cont.)• XML Schemas are Extensible

– XML Schemas are extensible, just like XML, because they are written in XML.

– With an extensible Schema definition you can: (manipulation)

• Reuse your Schema in other Schemas

• Create your own data types derived from standard types

• Reference multiple schemas from the same document

• Well-Formed is not Enough– A well-formed XML document is a document that conforms to the XML syntax rules:

• must begin with the XML declaration

• must have one unique root element

• all start tags must match end-tags

• XML tags are case sensitive

• all elements must be closed

• all elements must be properly nested

• all attribute values must be quoted

• XML entities must be used for special characters

– Even if documents are Well-Formed they can still contain errors, and those errors can have serious consequences.

Page 7: Week5 – Schema

7

Introduction

• XML Schema is an XML-based doc., an alternation to DTD.

• An XML schema describes and defines the structure of an XML document.

• Schema is used to validate XML document

• W3C: The XML Schema language is also referred to as XML Schema Definition (XSD).

• What You Should Already Know

– Before you study the XML Schema Language, you should have a basic understanding of XML and XML Namespaces. It will also help to have some basic understanding of DTD.

Page 8: Week5 – Schema

8

Introduction (cont.)

• What 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

Page 9: Week5 – Schema

9

Introduction (cont.)

• XML Schemas are the Successors of DTDs– 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 useful than DTDs

• XML Schemas are written in XML

• XML Schemas support data types

• XML Schemas support namespaces

• XML Schema is a W3C Recommendation

– XML Schema was originally proposed by Microsoft, but became an official W3C recommendation in May 2001, M keeps an XML-Data Reduced (XDR) Schema (as we saw in the textbook)

– The specification is now stable and has been reviewed by the W3C Membership.

– For a full overview of W3C Activities and Status:

http://www.w3schools.com/w3c/default.asp

Page 10: Week5 – Schema

10

Schema – How to?

• XML documents can have a reference to a DTD or an XML Schema

• An Example presented in XML, DTD, and Schema

<?xml version="1.0"?><note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body></note>

<!ELEMENT note (to, from, heading, body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>

Page 11: Week5 – Schema

11

Schema – How to? (cont.)

An XML Schema – W3c XML Schema

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.w3schools.com“ xmlns="http://www.w3schools.com"elementFormDefault="qualified"><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>

Page 12: Week5 – Schema

12

Schema – How to? (cont.)

XML Schema – Microsoft XDR Schema

<?xml version="1.0"?>

<Schema xmlns="urn:schemas-microsoft-com:xml-data">

<ElementType name="to" model="closed" content="textOnly" />

<ElementType name="from" model="closed" content="textOnly" />

<ElementType name="heading" model="closed" content="textOnly" />

<ElementType name="body" model="closed" content="textOnly" />

<ElementType name="note" model="closed" content="eltOnly" order="seq" >

<element type="to" minOccurs="1" />

<element type="from" minOccurs="1" />

<element type="heading" minOccurs="1" />

<element type="body" minOccurs="1" />

</ElementType>

</Schema>XML +

DTD XML + Schema (W3c) XML + Schema (Mic)

Page 13: Week5 – Schema

13

Schema – How to? (cont.)

• Referencing a Schema in an XML Document – W3C

<?xml version="1.0"?><note xmlns=http://www.w3schools.com xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation="http://www.w3schools.com note.xsd">……………………</note>

• xmlns: specifies the default namespace declaration.

• This declaration tells the schema-validator that all the elements used in this XML document are declared in the "http://www.w3schools.com" namespace.

• xmlns:xsi: specifies the XML Schema Instance namespace

• xsi:schemaLocation: specifies where the XML schema file is

Page 14: Week5 – Schema

14

Schema – How to? (cont.)

• Referencing a Schema in an XML Document – Microsoft XDR

<?xml version="1.0"?><note xmlns="x-schema:yourschema.xml">……………………</note>

• xmlns: specifies where the XML schema is

Page 15: Week5 – Schema

15

Elements

• root element: <schema> – W (as in W3c XML Schema):

– M (as in Microsoft XDR Schema):

– Attribute: xmlns (XML namespace)

<?xml version="1.0"?><xs:schema xmlns="http://www.w3schools.com" > ......</xs:schema>

<?xml version="1.0"?><Schema xmlns=“urn:schemas-microsoft-com:xml-data”> ......</Schema>

Page 16: Week5 – Schema

16

W3C or Microsoft XDR?

• W3c is a standard version of XML schema recommendation, We’ll use it through the class.http://www.w3.org

• Microsoft XDR spec. can be found through the web site:http://msdn.microsoft.com/library/

en-us/xmlsdk/html/xmconxdr_whatis.asp

Page 17: Week5 – Schema

17

Elements (cont.)

• XML Schemas define the elements of your XML files• What are Global Elements?

– Global elements are elements that are immediate children of the "schema" element!

– Local elements are elements nested within other elements

• Element Types:

– Simple Types • Contain only text with data types

– Complex Types• An XML element that contains other elements and/or attributes.

• There are four kinds of complex elements:

– Empty elements

– Elements that contain only other elements

– Elements that contain only text

– Elements that contain both other elements and text

Page 18: Week5 – Schema

18

Elements – Simple Elements

• A simple element is an XML element that can contain only text. It cannot contain any other elements or attribute

• The text can be of many different types. It can be one of the types that are included in the XML Schema definition (boolean, string, date, etc.),

• or it can be a custom type that you can define yourself.

• Common XML Schema Data Types: XML Schema has a lot of built-in data types. Here is a list of the most common types:

– xs:string

– xs:decimal

– xs:integer

– xs:boolean

– xs:date

– xs:time

Page 19: Week5 – Schema

19

Elements – Simple Elements (cont.)

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

Simple Element Definition:<xs:element name="lastname" type="xs:string"/><xs:element name="age" type="xs:integer"/><xs:element name="dateborn" type="xs:date"/>

In XML doc.:<lastname>Refsnes</lastname><age>34</age><dateborn>1968-03-27</dateborn>

Page 20: Week5 – Schema

20

Elements – Simple Elements (cont.)

• Declare Default and Fixed Values for Simple Elements– Simple elements can have a default value OR a fixed value set.

– A default value is automatically assigned to the element when no other value is specified. In the following example the default value is "red":

• A fixed value is also automatically assigned to the element. You cannot specify another value. In the following example the fixed value is "red":

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

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

Page 21: Week5 – Schema

21

Attributes

• All attributes are declared as simple types.

• Only complex elements can have attributes

• What is an Attribute?– Simple elements cannot have attributes.

– If an element has attributes, it is considered to be of complex type.

– The attribute itself is always declared as a simple type.

– This means that an element with attributes always has a complex type definition.

– Define an Attribute

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

Page 22: Week5 – Schema

22

Attributes (cont.)

• XML Schema has a lot of built-in data types (same as in Common XML Schema Data Types, slides #19).

• A simple attribute definition

The Syntax:<xs:attribute name="lang" type="xs:string"/>

XML element with attribute:<lastname lang="EN">Smith</lastname>

Page 23: Week5 – Schema

23

Attributes (cont.)

• Declare Default and Fixed Values for Attributes– Attributes can have a default value OR a fixed value specified.

– A default value is automatically assigned to the attribute when no other value is specified. In the following example the default value is "EN":

• A fixed value is also automatically assigned to the attribute. You cannot specify another value. In the following example the fixed value is "EN"

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

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

Page 24: Week5 – Schema

24

Attributes (cont.)

• Creating Optional and Required Attributes– All attributes are optional by default. To explicitly specify that the

attribute is optional, use the "use" attribute

• To make an attribute required

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

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

Page 25: Week5 – Schema

25

Restrictions on Content

• When an XML element or attribute has a type defined, it puts a restriction on the element's or attribute's content.

• If an XML element is of type "xs:date" and contains a string like "Hello Mother", the element will not validate.

• With XML Schemas, you can also add your own restrictions to your XML elements and attributes. These restrictions are called

facets. 

• Restrictions/Facets

– Restrictions are used to control acceptable values for XML elements or attributes.

– Restrictions on XML elements are called facets

Page 26: Week5 – Schema

26

Restrictions on Content (cont.)

• Restrictions on Values– This example defines an element called "age" with a restriction.

The value of age cannot be lower than 0 or greater than 100

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

<xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="100"/>

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

</xs:element>

Page 27: Week5 – Schema

27

Restrictions on Content (cont.)

• Restrictions on a Set of Values– To limit the content of an XML element to a set of acceptable

values, we would use the enumeration constraint.

– This example defines an element called "car“

– The "car" element is a simple type with a restriction. The acceptable values are: Audi, Golf, BMW.

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

Page 28: Week5 – Schema

28

Restrictions on Content (cont.)• Restrictions on a Series of Values

– To limit the content of an XML element to define a series of numbers or letters that can be used, we would use the pattern constraint.

– The examples defines two elements called “letter”, “word”:

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

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

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

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

– The "letter" element is a simple type with a restriction. The only acceptable value is ONE of the LOWERCASE letters from a to z

– The “word” element is a simple type with a restriction. The only acceptable value is Two of the UPPERCASE letters from A to Z

<xs:element name=“word"><xs:simpleType>

<xs:restriction base="xs:string"> <xs:pattern value="[A-Z] [A-Z]"/> </xs:restriction>

</xs:simpleType></xs:element>

Page 29: Week5 – Schema

29

Restrictions on Content (cont.)

• Several other examples:

– [a-zA-Z] :The only acceptable value is ONE of the LOWERCASE OR UPPERCASE letters

– [xyz]: The only acceptable value is ONE of the following letters: x, y, OR z

– [0-9][0-9]: The only acceptable value is TWO digits in a sequence, and each digit must be in a range from 0 to 9

– ([a-z])*: The acceptable value is zero or more occurrences of lowercase letters from a to z (applied +, *, and pipe character: |)

– [a-zA-Z0-9]{8} :There must be exactly eight characters in a row and those characters must be lowercase or uppercase letters, or a number

Page 30: Week5 – Schema

30

Restrictions on Content (cont.)• Restrictions on White Space Characters

– To specify how white space characters should be handled, use the whiteSpace constraint.

– This example defines an element called "address"

– The whiteSpace constraint is set to "preserve", the XML processor WILL NOT remove any white space characters

– Alternatively, the whiteSpace constraint can set to “replace", the XML processor WILL REPLACE all white space characters (line feeds, tabs, spaces, and carriage returns) with spaces

– The whiteSpace constraint can set to "collapse", which means that the XML processor WILL REMOVE all white space characters

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

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

</xs:simpleType></xs:element>

Page 31: Week5 – Schema

31

Restrictions on Content (cont.)

• Restrictions on Length

– To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints.

<xs:element name="password">

<xs:simpleType> <xs:restriction

base="xs:string"> <xs:length value="8"/> </xs:restriction>

</xs:simpleType></xs:element>

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

<xs:restriction base="xs:string"> <xs:minLength value="8"/> <xs:maxLength value="8"/></xs:restriction>

</xs:simpleType></xs:element>

Page 32: Week5 – Schema

32

Restrictions on Data TypesConstraint Description

enumeration Defines a list of acceptable values

fractionDigitsSpecifies the maximum number of decimal places allowed. Must be equal to or greater than zero

lengthSpecifies the exact number of characters or list items allowed. Must be equal to or greater than zero

maxExclusiveSpecifies the upper bounds for numeric values (the value must be less than this value)

maxInclusiveSpecifies the upper bounds for numeric values (the value must be less than or equal to this value)

maxLengthSpecifies the maximum number of characters or list items allowed. Must be equal to or greater than zero

minExclusiveSpecifies the lower bounds for numeric values (the value must be greater than this value)

minInclusiveSpecifies the lower bounds for numeric values (the value must be greater than or equal to this value)

minLengthSpecifies 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

whiteSpaceSpecifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

Page 33: Week5 – Schema

33

XML Data Types (cont.)

Page 34: Week5 – Schema

34

Elements – Complex (cont.)

• What is a Complex Element?

– A complex element is an XML element that contains other elements and/or attributes.

– There are four kinds of complex elements:

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

– Note: Each of these elements may contain attributes as well!

Page 35: Week5 – Schema

35

Elements – Complex (cont.)

• 4 types of complex XML elements,

<product pid="1345"/>

<employee><firstname>John</firstname><lastname>Smith</lastname>

</employee>

<food type="dessert">Ice cream</food>

<description>It happened on <date lang="norwegian">03.03.99</date> ....

</description>

Page 36: Week5 – Schema

36

Elements – Complex (cont.)

• Define a complex element

<xs:element name=“elementName"> <xs:complexType>

………………………………<!-- 4 types of complex elements may have different format here! -->………………………………

</xs:complexType></xs:element>

Page 37: Week5 – Schema

37

Elements – Complex (cont.)

• Type I: Define Complex Types for Empty Elements

• An empty complex element can contain attributes; but it cannot have any content between the opening and closing tags.

<xs:element name=“elementName"> <xs:complexType>

<xs:attribute name=“attr1" type="xs:positiveInteger"/> </xs:complexType></xs:element>

Reusable element definition:<xs:element name="product" type="prodtype"/><xs:element name=“goods" type="prodtype"/><xs:complexType name="prodtype"> <xs:attribute name="prodid" type="xs:positiveInteger"/></xs:complexType>

Page 38: Week5 – Schema

38

Elements – Complex (cont.)

• Type II: Define Complex Types with Elements Only – An "elements only" complex type contains an element that

contains only other elements

Reusable element definition: <xs:element name="person" type="persontype"/><xs:element name=“employee" type="persontype"/><xs:complexType name="persontype">

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

</xs:complexType>

Page 39: Week5 – Schema

39

Elements – Complex (cont.)

• Type III: Define Complex Text-Only Elements – A complex text element can contain both attributes and text

– This type contains only simple content (text and attributes),

– Add a simpleContent element around the content.

– Must define an extension OR a restriction within the simpleContent element,

<xs:element name="somename">

<xs:complexType> <xs:simpleContent> <xs:extension

base="basetype"> .... .... </xs:extension> </xs:simpleContent>

</xs:complexType></xs:element>

<xs:element name="somename"> <xs:complexType>

<xs:simpleContent> <xs:restriction base="basetype"> .... .... </xs:restriction> </xs:simpleContent>

</xs:complexType></xs:element>

Page 40: Week5 – Schema

40

Elements – Complex (cont.)• Examples<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>

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

Reusable element definition:<xs:element name="shoesize" type="shoetype"/><xs:complexType name="shoetype">

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

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

</xs:complexType>

Page 41: Week5 – Schema

41

Elements – Complex (cont.)

• Type IV: Define Complex Types with Mixed Content– A mixed complex type element can contain attributes, elements, and

text

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

Page 42: Week5 – Schema

42

Elements – Complex (cont.)• Example: Mixed content + reusable element definition

<xs:element name="letter“ type=“letterform”> <xs:complexType mixed="true“ name=“letterform”>

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

<letter>Dear Mr.<name>John Smith</name>.Your order <orderid>1032</orderid>will be shipped on <shipdate>2001-07-13</shipdate>.</letter>

Page 43: Week5 – Schema

43

Elements – Complex (cont.)

• Complex Types Indicators – Control HOW elements are to be used in documents with indicators

– 7 types:• Order indicators: Define how elements should occur

– All

– Choice

– Sequence

• Occurrence indicators: Define how often an element can occur

– maxOccurs

– minOccurs

• Group indicators: Define related sets of elements, attributes

– Element Group name

– Attribute Group name

Page 44: Week5 – Schema

44

Elements – Complex (cont.)

Attribute Name

Syntax Description

all <xs:all>

The <all> indicator specifies by default that the child elements can appear in any order and that each child element must occur once and only once

Note: When using the <all> indicator you can set the <minOccurs> indicator to 0 or 1 and the <maxOccurs> indicator can only be set to 1

choice <xs:choice>The <choice> indicator specifies that either one child element or another can occur

sequence <xs:sequence>The <sequence> indicator specifies that the child elements must appear in a specific order

Order Indicators

Order indicators are used to define how elements should occur.

Page 45: Week5 – Schema

45

Elements – Complex (cont.)

Attribute Name

Syntax Description

maxOccurs <maxOccurs>

The <maxOccurs> indicator specifies the maximum number of times an element can occur

Example:

<xs:element name="child_name" … maxOccurs="10"/>

minOccurs <minOccurs>

The <minOccurs> indicator specifies the minimum number of times an element can occur

Example:

<xs:element name="child_name" …maxOccurs="10" minOccurs="1"/>

Occurrence IndicatorsOccurrence indicators are used to define how often an element can occurNote: For all "Order" and "Group" indicators (any, all, choice, sequence, group name,

and group reference) the default value for maxOccurs and minOccurs is 1

Page 46: Week5 – Schema

46

Elements – Complex (cont.)

• A working example (which type of complex element?) <?xml version="1.0" encoding="ISO-8859-1"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"elementFormDefault="qualified"><xs:element name="persons">

<xs:complexType> <xs:sequence>

<xs:element name="person" maxOccurs="unbounded"> <xs:complexType> <xs:sequence>

<xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" minOccurs="0" maxOccurs="5"/> </xs:sequence>

</xs:complexType> </xs:element>

</xs:sequence> </xs:complexType>

</xs:element></xs:schema>

XML

Page 47: Week5 – Schema

47

Elements – Complex (cont.)

Attribute Name

Syntax Description

Element Group

<xs:group

name="groupname"> Element groups are defined with the group declaration

Attribute Group

<xs:attributeGroup

name="groupname"> Attribute groups are defined with the

attributeGroup declaration

Group IndicatorsGroup indicators are used to define related sets of elements or attributes

Page 48: Week5 – Schema

48

Elements – Complex (cont.)• The example of defining an element group and using it in an element definition

<xs:group name="persongroup"> <xs:sequence>

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

</xs:sequence></xs:group>

<xs:group name="persongroup"> <xs:sequence>

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

</xs:sequence></xs:group><xs:element name="person" type="personinfo"/><xs:complexType name="personinfo">

<xs:sequence> <xs:group ref="persongroup"/> <xs:element name="country" type="xs:string"/>

</xs:sequence></xs:complexType>

Page 49: Week5 – Schema

49

Elements – Complex (cont.)• The example of defining an attribute group and using it in an element definition

<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/>

</xs:attributeGroup>

<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/>

</xs:attributeGroup><xs:element name="person">

<xs:complexType> <xs:attributeGroup ref="personattrgroup"/>

</xs:complexType></xs:element>

Page 50: Week5 – Schema

50

Elements – The <any> Element (cont.)

• The <any> element enables us to extend the XML document with elements not specified by the schema

• A declaration for an element is presented. – By using the <any> element we can extend (after general elements)

the content of "person" with any element:

<xs:element name="person"> <xs:complexType>

<xs:sequence> <xs:element name=“elementname" type="xs:string"/> ………………. <xs:any minOccurs="0"/>

</xs:sequence> </xs:complexType></xs:element>

Page 51: Week5 – Schema

51

The <anyAttribute> Element

• Enables us to extend the XML document with attributes not specified by the schema!

• A declaration for an element is presented– By using the <anyAttribute> element we can we can add any number of

attributes to the "person" element:

<xs:element name="person"> <xs:complexType>

<xs:sequence> <xs:element name=“elementname" type="xs:string"/> ………………………………

</xs:sequence> <xs:anyAttribute/>

</xs:complexType></xs:element>

Page 52: Week5 – Schema

52

Schema Data Types

• For all data types supported by W3c, please refers to:http://www.w3schools.com/schema/

– XSD String Data Types :

• Used for values that contains character strings

– XSD Date and Time Data Types

• Used for values that contain date and time

• The date is specified in the following form "YYYY-MM-DD"

• The time is specified in the following form "hh:mm:ss"

– XSD Numeric Data Types

• Used for numeric values

– XSD Miscellaneous Data Types

• Other miscellaneous data types are boolean, base64Binary(Base64-encoded binary data) , hexBinary (hexadecimal-encoded binary data) , float, double, anyURI, QName, and NOTATION

Page 53: Week5 – Schema

53An Example (Topic: Shipping Order)– Step by Step

• How to write an XML Schema in different ways

Step I: Modeling data set

Step II: Create an XML Schema

Step III: Element Definition

Step IV: Elaborate Your Schema Design

Step V: Divide the Schema

Step VI: Using Named Types

Page 54: Week5 – Schema

54

An Example – Step I

• How to write an XML Schema in different ways

• Modeling data set

• Topic: shipping order– Order person

• Name

– Shipping to whom and where• Name

• Address

• City

• country

– Shipping items• Title

• Note

• Quantity

• price

According to the data structure listed left, what type of XML document structure can you design?

Page 55: Week5 – Schema

55

An Example – Step I (cont.)

• Topic: shipping order (root element)– Order ID (attribute – required)

– Order person (child element – required, single)• Name (element – required, single)

– Shipping to whom and where (child element – required, single)• Name (element – required, single)

• Address (element – required, single)

• City (element – required, single)

• Country(element – required, single)

– Shipping items (child element – required, multiple)• Title (element – required, single)

• Note (element – optional, single)

• Quantity (element – required, single)

• Price (element – required, single)

Page 56: Week5 – Schema

56

Step II – Creating an XML Schema

1. Now we are going to create a schema for the data set above!

2. Start by opening a new file that we will call "shiporder.xsd".

3. To create the schema we could simply follow the data structure and define each element as we find it.

4. We will start with the standard XML declaration followed by the xs:schema element that defines a schema

<?xml version="1.0" encoding="ISO-8859-1" ?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

</xs:schema>

Demo

Page 57: Week5 – Schema

57

Step III – Element Definition• Use the template below

<xs:element name=“elementName"> <xs:complexType>

<xs:sequence> ... ...

</xs:sequence> </xs:complexType></xs:element>

<xs:element name="shipto"> <xs:complexType>

<xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/>

</xs:sequence> </xs:complexType></xs:element>

Page 58: Week5 – Schema

58

Step IV – Elaborate Your Design

• To further elaborate your schema design you can:– Define the number of possible occurrences for an element with the

maxOccurs and minOccurs attributes. – Define data types– Define attribute type

<xs:element name="item" maxOccurs="unbounded"> <xs:complexType>

<xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minOccurs="0"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/>

</xs:sequence> </xs:complexType></xs:element>

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

Page 59: Week5 – Schema

59

Step V – Divide the Schema

• The previous design method is very simple

• But can be very difficult to read and maintain when documents are complex

• The next design method is based on defining all elements and attributes first, and then referring to them using the ref attribute.

Page 60: Week5 – Schema

60

Step V – Divide the Schema (cont.)

<?xml version="1.0" encoding="ISO-8859-1" ?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><!-- definition of simple elements --><xs:element name="orderperson" type="xs:string"/><xs:element name="name" type="xs:string>…………………………<!-- definition of attributes --><xs:attribute name="orderid" type="xs:string"/><!-- definition of complex elements --><xs:element name="shipto"> <xs:complexType>

<xs:sequence> <xs:element ref="name"/> ……………………..

</xs:sequence> </xs:complexType></xs:element>………………………..</xs:schema>

Page 61: Week5 – Schema

61

Step VI – Using Named Types

• The third design method defines classes or types, – Enables us to reuse element definitions.

– This is done by naming the simpleTypes and complexTypes elements

– Then point to them through the type attribute of the element.

<xs:simpleType name="stringtype"> <xs:restriction base="xs:string"/>

</xs:simpleType>…………………………………………………………..<xs:simpleType name="inttype">

<xs:restriction base="xs:positiveInteger"/></xs:simpleType><!-- type usage --><xs:complexType name="shiptotype"> <xs:sequence>

<xs:element name="name" type="stringtype"/> …………………………………………………………

</xs:sequence> </xs:schema>

Page 62: Week5 – Schema

62

Review Ch. 8 Document Object Model (DOM)

That’s it for today!