Presentation is loading. Please wait.

Presentation is loading. Please wait.

XSD: XML Schema Language Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.

Similar presentations


Presentation on theme: "XSD: XML Schema Language Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University."— Presentation transcript:

1 XSD: XML Schema Language Kanda Runapongsa (krunapon@kku.ac.th)krunapon@kku.ac.th Dept. of Computer Engineering Khon Kaen University

2 168493: XML and Web Services (II/2546) 2 XML Schema & DTD Like a DTD, its purpose is to define the legal building blocks of an XML document defines elements that can appear in a document defines attributes that can appear in a document defines which elements are child elements

3 168493: XML and Web Services (II/2546) 3 XML Schema & DTD (Cont.) 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

4 168493: XML and Web Services (II/2546) 4 Advantages of XSD over DTD XML Schemas use XML syntax Do not need to learn a new syntax Use any XML parser to parse XML schemas XML Schemas support many data types and allow you to create new data types No ‘Integer’ or ‘Double’ types in DTD

5 168493: XML and Web Services (II/2546) 5 Adv. of XSD over DTD (Cont.) XML Schemas allow you to group elements to control the recurrence of elements and attributes In DTD, we cannot specify that the number of occurrences of elements must be at least 2 and at most 5 XML Schemas support namespaces

6 168493: XML and Web Services (II/2546) 6 The Element The element is the root element of every XML Schema:...

7 168493: XML and Web Services (II/2546) 7 The Element (Cont.)...

8 168493: XML and Web Services (II/2546) 8 Attributes of xmlns:xs=“http://www.w3.org/20 01/XMLSchema” Indicates that the elements and data types used in the schema (schema, element,…) come from the http://www.w3.org/2001/XMLSchema namespace http://www.w3.org/2001/XMLSchema These elements and data types should be prefixed with xs:

9 168493: XML and Web Services (II/2546) 9 Attributes of (Cont.) targetNamespace=“http://abc.com” Indicates that the elements defined by this schema (Note, To, From,..) is in the "http://www.w3schools.com" namespace

10 168493: XML and Web Services (II/2546) 10 Attributes of (Cont.) xmlns=“http://abc.com” Indicates that the default namespace is http://abc.comhttp://abc.com elementFormDefault="qualified“ indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.

11 168493: XML and Web Services (II/2546) 11 XSD Simple Elements A simple element Can contain only text Cannot contain any other elements Cannot contain any attributes Can text be any other types besides string? It can be date, etc., or even a custom type that you can define yourself

12 168493: XML and Web Services (II/2546) 12 Defining a Simple Element The syntax for defining a simple element is: where xxx is the name of the element and yyy is the data type of the element.

13 168493: XML and Web Services (II/2546) 13 Simple Elements: Example Some XML elements Wasee 34 Corresponding element definitions

14 168493: XML and Web Services (II/2546) 14 Built-in Data Types xs:string xs:decimal xs:integer xs:boolean xs:date xs:time

15 168493: XML and Web Services (II/2546) 15 Simple Elements: Default Values Simple elements can have a default value A default value is automatically assigned to the element when no value is specified

16 168493: XML and Web Services (II/2546) 16 Simple Elements: Fixed Values A fixed value is automatically assigned to elements We cannot specify another value

17 168493: XML and Web Services (II/2546) 17 XSD Attributes All attributes are declared as simple types Only complex elements can have attributes An element with attributes always has a complex type definition

18 168493: XML and Web Services (II/2546) 18 Defining an Attribute Syntax: Where xxx is the name of the attribute and yyy is the data type of the attribute Examples: XML: Dee XSD:

19 168493: XML and Web Services (II/2546) 19 Default & Fixed Values for Attributes Attributes can have a default value or a fixed value specified Examples:

20 168493: XML and Web Services (II/2546) 20 Optional & Required Attributes All attributes are optional by default To explicitly specify that the attribute is optional or required, use the “use” attribute Examples:

21 168493: XML and Web Services (II/2546) 21 Restrictions on Content When an XML element or attribute has a type defined, it puts a restriction for the element’s or attribute’s content We can also add our own restrictions to our XML elements and attributes These restrictions are called facets

22 168493: XML and Web Services (II/2546) 22 XSD Restrictions/Facets Restrictions are used to control acceptable values for XML elements or attributes Many kinds of restrictions/facets Restrictions on values Restrictions on white spaces Restrictions on length

23 168493: XML and Web Services (II/2546) 23 Restrictions on Values This example defines an element called “age” with a restriction. … What range the value of age is in? Cannot be lower than 0 or greater than 60

24 168493: XML and Web Services (II/2546) 24 Restriction 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 …

25 168493: XML and Web Services (II/2546) 25 Restrictions on a Series of Values To limit the content to a series of numbers or letters, use the pattern constraint … What are the acceptable values of the “letter” element?

26 168493: XML and Web Services (II/2546) 26 Restrictions on a Series of Values … What are the acceptable values of the “letter” element?

27 168493: XML and Web Services (II/2546) 27 Restrictions on a Series of Values … What are the acceptable values of the “password” element?

28 168493: XML and Web Services (II/2546) 28 Restrictions on White Space Characters To specify how white space characters should be handled, use the whiteSpace constraint … </xs:element The XML processor will not remove any white space characters

29 168493: XML and Web Services (II/2546) 29 Restrictions on White Space Characters Three values of whiteSpace preserve: the XML processor will not remove any white space characters replace: the XML processor will replace all white space characters with spaces collapse: the XML processor will remove all white space characters (leading and trailing spaces are removed)

30 168493: XML and Web Services (II/2546) 30 Restrictions on Length … Can we specify the minimum and maximum length of the value? Yes, use “minLength” and “maxLength”

31 168493: XML and Web Services (II/2546) 31 What is a Complex Element? A complex element is an XML element that contains other elements and/or attributes 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

32 168493: XML and Web Services (II/2546) 32 Complex Elements Ex1: Ex2: 1 2 Ex3: Ice cream Ex4: Today is 11.13.2003

33 168493: XML and Web Services (II/2546) 33 Defining a Complex Element …

34 168493: XML and Web Services (II/2546) 34 Sharing the Element Type <xs:element name=“firstname” …

35 168493: XML and Web Services (II/2546) 35 Defining Based on Other Types … …

36 168493: XML and Web Services (II/2546) 36 Types for Empty Elements XML: XSD:

37 168493: XML and Web Services (II/2546) 37 Elements w/ Only Sub-Elements XML: A B XSD:

38 168493: XML and Web Services (II/2546) 38 Elements w/ Only Text XML: 8 XSD: …

39 168493: XML and Web Services (II/2546) 39 Types w/ Mixed Content XML: Today is 11.13.2003 XSD: …

40 168493: XML and Web Services (II/2546) 40 Indicators Order indicators: All, choice, sequence Occurrence indicators: maxOccurs, minOccurs Group indicators: Group name attributeGroup name

41 168493: XML and Web Services (II/2546) 41 All Indicator Specify by default that the child elements can appear in any order and each child element must occur once and only once <xs:element name=“fname” type=“xs:string”/> …

42 168493: XML and Web Services (II/2546) 42 Choice Indicator Specify that either one child element or another can occur...

43 168493: XML and Web Services (II/2546) 43 Sequence Indicator Specify that the child elements must appear in a specific order …

44 168493: XML and Web Services (II/2546) 44 Occurrence Indicator Define how often an element can occur maxOccurs Indicator: maximum number of times an element can occur …

45 168493: XML and Web Services (II/2546) 45 Occurrence Indicator (Cont.) minOccurs Indicator: minimum number of times an element can occur …

46 168493: XML and Web Services (II/2546) 46 Group Indicators Define related sets of elements Element Groups are defined with the group declaration, like this: … After defining a group, we can reference it in another complex type definition

47 168493: XML and Web Services (II/2546) 47 Element Groups … …

48 168493: XML and Web Services (II/2546) 48 Attribute Groups … …


Download ppt "XSD: XML Schema Language Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University."

Similar presentations


Ads by Google