Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML Schema. Why Schema? To define a class of XML documents Serve same purpose as DTD “Instance document" used for XML document conforming to schema.

Similar presentations


Presentation on theme: "XML Schema. Why Schema? To define a class of XML documents Serve same purpose as DTD “Instance document" used for XML document conforming to schema."— Presentation transcript:

1 XML Schema

2 Why Schema? To define a class of XML documents Serve same purpose as DTD “Instance document" used for XML document conforming to schema

3 DTD v/s Schema XML Schema uses XML to define XML, DTD does not XML introduces a datatype library to use, DTD does not

4 XML Schema Instance - PO Alice Smith 123 Maple Street Mill Valley CA 90952 Robert Smith 8 Oak Avenue Old Town PA 95819 Hurry, my lawn is going wild! Lawnmower 1 148.95 Confirm this is electric

5 Analyzing PO Schema Instance Elements containing sub elements or carry attributes (complex types) Elements containing numbers (strings etc.) but no sub elements (simple types) Attributes always have simple types Exercise: Identify Complex, Simple types in sample!

6 XML Schema - PO

7 XML Schema Name Space Each of schema elements has prefix xsd: xsd: associated with XML Schema namespace through xmlns:xsd="http://www.w3.org/2001/XMLSchema" Prefix xsd: is used by convention, though any prefix could be used

8 XML Schema Name Space Purpose –To identify elements, simple types as belonging to XML Schema language vocabulary xsd: also appears on the names of built-in simple types eg. xsd:string

9 Element Declarations Elements are defined using Common attributes used: –name: Name of the element –type: Name of element type (simple/complex) –ref: Reference to an element name

10 Element Declarations Common attributes used: –default: provides the element default attribute –fixed: to ensure that the element is set to particular value –minOccurs: minimum number of times an element can occur –maxOccurs: maximum number of times an element can occur Default values of minOccurs, maxOccurs is 1

11 Element Declarations Content (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*))

12 EXERCISE 1 In all 3 case studies, Identify, Construct the following: –Element Declarations

13 Attribute Declarations Attributes are defined using Common attributes used: –name: Name of the attribute –type: Name of attribute type (simple) –ref: Reference to an attribute name –default: if absent, processor provides default attribute value

14 Attribute Declarations Attributes are defined using Common attributes used: –fixed: to ensure that the attribute is set to particular value –use: to indicate whether the attribute is required, optional, prohibited Content (annotation?, (simpleType?))

15 EXERCISE 2 In all 3 case studies, Identify, Construct the following: –Attribute Declarations

16 Complex Type Definitions Complex Types –Allow elements in their content and may carry attributes Simple Types –Cannot have element content and cannot carry attributes

17 Complex Type Definitions They are defined using Common attributes used: –name: Name of the Complex Type –mixed: content is mixed Content (annotation?, (simpleContent | complexContent | ((group | all | choice |sequence)?, ((attribute | attributeGroup)*, anyAttribute?))))

18 Complex Type Definitions – Eg. Typically contain set of: –element declarations –element references –attribute declarations xsd:sequence the elements must appear in the same sequence (order) in which they are declared

19 Complex Type Definitions – Eg. Defining a PurchaseOrderType

20 EXERCISE 3 In all 3 case studies, Identify, Construct the following: –Complex Types

21 Global Elements, Attributes Created by declarations that appear as the children of the schema element Global elements/attributes can be referenced using ref attribute (See comment in PO example) Global Element declaration enables the element to appear at the top-level of an instance document (See purchaseOrder in PO example)

22 Global Elements, Attributes Cannot contain the ref attribute They must use the type attribute or be followed by an anonymous type definition Global declarations cannot contain the attributes minOccurs, maxOccurs, or use

23 EXERCISE 4 In all 3 case studies, Identify, Construct the following: –Global Elements

24 Naming Conflicts If two things are both types, there is a conflict Say we define a complex type called USStates and a simple type called USStates = Conflict! If the two things are a type and an element or attribute, there is no conflict Say we define a complex type called USAddress and we declare an element called USAddress = No Conflict!

25 Naming Conflicts Two things are elements within different types (i.e. not global elements): No conflict Two things are both types: –one user defined –other XML Schema defined No conflict

26 Simple Type Definitions Cannot have element content and cannot carry attributes Simple Types are either built in to XML Schema Simple Types could also be derived from the built-in's

27 Simple Type Definitions 3 types: Atomic, List, Union Type

28 Simple Built In Type Definitions They are built into XML Schema Need not be defined Can be used in all element and attribute declarations Example: string, date, int etc.

29 Simple Built In Type Definitions See “Built-In-Types.doc” for entire list.

30 Simple Derived Type Definitions They are defined using Common attributes used: –name: Name of the Simple Type Content (annotation?, (restriction | list | union))

31 Simple Derived Type Definitions They are derived from built in types Derive a new simple type by restricting an existing simple type Need to be defined

32 Simple Derived Type Definitions Can be used in all element and attribute declarations XML INSTANCE PO: SKU Definition

33 Restriction Element They are defined using Common attributes used: –base: Name of Type Content (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length |minLength | maxLength | enumeration | whiteSpace | pattern)*))

34 Restriction Element Used to indicate the existing (base) type Used to identify the "facets" that constrain the range of values

35 Restriction Element XML INSTANCE PO: SKU Definition (Pattern Facet) MY INTEGER Definition (minInclusive, maxInclusive Facet)

36 Restriction Element XML INSTANCE PO How about using an enumeration of USStates in place of type currently used in the state element declaration?

37 EXERCISE 5 In all 3 case studies, Identify, Construct the following: –Simple Types (with derived, built-in types)

38 Simple List Type Definitions They are defined using – Common attributes used (xsd:list): –ItemType: item type of the list Content (annotation?, (simpleType?))

39 Simple List Type Definitions Comprised of sequences of atomic types Can create new list types by derivation from existing atomic types

40 Simple List Type Definitions MyINTEGER Definition LIST OF MyINTEGER Definition

41 Simple List Type Definitions Can create new list types by derivation from existing atomic types

42 Simple Union Type Definitions They are defined using – Common attributes used (xsd:list): –memberTypes: members of the union Content (annotation?, (simpleType*))

43 Simple Union Type Definitions Enables an element or attribute value to be one or more instances of one type drawn from the union of multiple atomic and list types USState, listOfMyIntType defined previously

44 EXERCISE 6 In all 3 case studies, Identify, Construct the following: –Simple Types List Types Union Types Derived Types using List, Union Types

45 Anonymous Type Definitions Type can also be defined as Anonymous type Saves overhead of having to be named and explicitly referenced XML INSTANCE PO: Definition of the type Items contains two element declarations that use anonymous types (item, quantity)

46 Anonymous Type Definition

47 EXERCISE 7 In all 3 case studies, Identify, Construct the following: –Anonymous Type Definitions Simple Complex

48 Types Of Element Content Declaring an element with attribute, containing a simple value

49 Simple Content Type Definitions They are defined using: – Content (annotation?, (restriction | extension))

50 Simple Content Type Definitions To indicate that the content model of the new type contains only character data and no elements

51 Extension Element They are defined using Common Attributes Used –base: Name of Simple Type Content (annotation?, ((attribute | attributeGroup)*,anyAttribute?))

52 Extension Element Used to derive the new type by extending

53 EXERCISE 8 In all 3 case studies, Identify, Construct the following: –Elements with simple content, attributes

54 Types Of Element Content Declaring an element with sub elements, character data appear alongside (MIXED)

55 Types Of Element Content Declaring an element which has no content at all (EMPTY) Define a type that allows only elements in its content, but do not actually declare any elements

56 Complex Content Type Definitions They are defined using – Commonly used attributes –Mixed: used to indicate existence of character data, elements alongside Content (annotation?, (restriction | extension))

57 Complex Content Type Definitions To indicate that the content model of the new type contains only elements

58 EXERCISE 9 In all 3 case studies, Identify, Construct the following: –Elements with Empty Content Models, only attributes –Elements with MIXED content

59 Annotations 3 elements can be used to annotate Schemas: –Annotation: Contains documentation, appInfo as sub elements Can appear at beginning of XML Schema elements –Documentation Recommended location for human readable material –appInfo provide information for tools, stylesheets and other applications

60 Annotations 3 elements can be used to annotate Schemas: –appInfo provide information for tools, stylesheets and other applications

61 Annotations element declared with anonymous type Empty anonymous type with 2 attributes

62 Building Content Models XML Schema provides constraints that apply to groups of elements appearing in a content model These constraints do not apply to attributes

63 Building Content Models

64 Choice Element Defined using Commonly used attributes –minOccurs: used to indicate minimum number of occurrences –maxOccurs: used to indicate maximum number of occurrences Content (annotation?, (element | group | choice | sequenceannotationelementgroupchoicesequence | any)*)any Allows only one of its children to appear in an instance

65 Choice Element Content (annotation?, (element | group | choice | sequence | any)*) Allows only one of its children to appear in an instance

66 Sequence Element They are defined using Commonly used attributes –minOccurs: used to indicate minimum number of occurrences –maxOccurs: used to indicate maximum number of occurrences Content (annotation?, (element | group | choice | sequence | any)*)annotationelementgroupchoicesequenceany Allows its children to appear in sequence identified

67 Sequence Element Content (annotation?, (element | group | choice | sequence | any)*) Allows its children to appear in sequence identified

68 Group Element They are defined using Commonly used attributes –name: identifies the name of the group Content (annotation?, (all | choice | sequence))

69 Group Element Allows groups of elements to be defined and named Allows defined groups to be used to build up the content models of complex types

70 All Element They are defined using Commonly used attributes –minOccurs: used to indicate minimum number of occurrences –maxOccurs: used to indicate maximum number of occurrences Content (annotation?, element*)annotationelement All the elements in the group may appear once or not at all, and they may appear in any order Is limited to the top-level of any content model Group's children must all be individual elements (no groups) No element in the content model may appear more than once

71 All Element Content (annotation?, element*) All the elements in the group may appear once or not at all, and they may appear in any order Is limited to the top-level of any content model Group's children must all be individual elements (no groups)

72 All Element Content (annotation?, element*) No element in the content model may appear more than once

73 All Element XML Schema stipulates that an all group must appear as the sole child at the top of a content model

74 EXERCISE 10 In all 3 case studies, Identify, Construct the following content models: –Sequence –Choice –Group –All

75 Attribute Groups They are defined using – Commonly used attributes –name: to identify name of the group –ref: used to reference an attribute group Content (annotation?, ((attribute | attributeGroup)*, anyAttribute?))annotationattributeattributeGroupanyAttribute Can create a named attribute group containing all the desired attributes Reference this group by name in the item element declaration

76 Attribute Groups Content (annotation?, ((attribute | attributeGroup)*, anyAttribute?)) Can create a named attribute group containing all the desired attributes Reference this group by name in the item element declaration

77

78 EXERCISE 11 In all 3 case studies, identify: –Attribute Groups

79 Nil Values To represent an unknown, inapplicable information explicitly with an element <xsd:element name="shipDate" type="xsd:date“ nillable="true"/>

80 Target Name Space Schema is a collection of type definitions, element declarations whose names belong to a particular namespace (target namespace) <schema xmlns=http://www.w3.org/2001/XMLSchemahttp://www.w3.org/2001/XMLSchema xmlns:po=http://www.example.com/PO1http://www.example.com/PO1 targetNamespace=http://www.example.com/PO1http://www.example.com/PO1 elementFormDefault="unqualified" attributeFormDefault="unqualified">

81 EXERCISE 12 In all 3 case studies, Identify, Construct the following: –Target Name Space –Element Form Default –Attribute Form Default

82 Including Schemas Desirable to divide schema content among several schema documents for: –ease of maintenance, access control, and readability <schema targetNamespace=http://www.example.com/IPOhttp://www.example.com/IPO xmlns=http://www.w3.org/2001/XMLSchemahttp://www.w3.org/2001/XMLSchema xmlns:ipo="http://www.example.com/IPO"> ……… <schema targetNamespace=http://www.example.com/IPOhttp://www.example.com/IPO xmlns=http://www.w3.org/2001/XMLSchemahttp://www.w3.org/2001/XMLSchema xmlns:ipo="http://www.example.com/IPO">

83 Including Schemas Caveat to using include is that target namespace of the included components must be the same as the target namespace of the including schemainclude

84 Using Derived Types in Instances Instance whose content conforms to the derivedType is valid if that content appears at a location where a baseType is expected derivedType is identified using the xsi:type attributexsi:type

85 Redefining Types, Groups Enables you to redefine simple and complex types, groups, and attribute groups that are obtained from external schema files <schema targetNamespace=http://www.example.com/IPOhttp://www.example.com/IPO xmlns=http://www.w3.org/2001/XMLSchemahttp://www.w3.org/2001/XMLSchema xmlns:ipo="http://www.example.com/IPO">

86 Substitution Groups Allows elements to be substituted for other elements Elements can be assigned to a special group of elements which are substitutable for a particular named element (Head Element) <element name="shipComment" type="string“ substitutionGroup="ipo:comment"/> <element name="customerComment" type="string“ substitutionGroup="ipo:comment"/> Elements in a substitution group must have the same type as the head element Elements in a substitution group can have a type that has been derived from the head element's type

87 Substitution Groups Elements in a substitution group must have the same type as the head element Elements in a substitution group can have a type that has been derived from the head element's type

88 EXERCISE 13 In all 3 case studies: –Identify any element from an externally included.xsd, redefine it –Identify derived types,their simple types –Use derived types in instances (xsi:type) –Identify Substitution Groups

89 Abstract Elements & Types Is a mechanism to force substitution for a particular element or type When an element is declared to be abstract, a member of that element's substitution group must appear in the instance document When an element's corresponding type definition is declared as abstract, all instances of that element must use xsi:type to indicate a derived type that is not abstract

90 Abstract Elements & Types Declaring an element as abstract requires the use of a substitution group

91 Abstract Elements & Types Declaring a type as abstract simply requires the use of a type derived from it (xsi:type) <schema xmlns=http://www.w3.org/2001/XMLSchemahttp://www.w3.org/2001/XMLSchema targetNamespace=http://cars.example.com/schemahttp://cars.example.com/schema xmlns:target="http://cars.example.com/schema"> ………. <transport xmlns=http://cars.example.com/schemahttp://cars.example.com/schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Car"/>

92 EXERCISE 14 In all 3 case studies: –Identify derived types for simple types –Define derived types, simple type abstract –Use derived type in instance (using xsi:type)

93 Controlling Derived Types One mechanism allows that new types may not be derived from complex type: –by restriction –by extension –at all

94 Controlling Derived Types Preventing derivations at all, or by extension, are indicated by the values #all and extension respectively There exists an optional finalDefault attribute on the schema element Effect of specifying the finalDefault attribute is equivalent to specifying a final attribute on every type definition and element declaration in the schema

95 Controlling Derived Types Another type-derivation mechanism controls which facets can be applied in the derivation of a new simple type A fixed attribute may be applied to any of a simple type definition facet to prevent a derivation of that type from modifying the value of the fixed facets

96 Controlling Derived Types Another mechanism is provided that controls which derivations and substitution groups may be used in instance documents Replacement by derived types can be controlled using the block attribute in a type definition Restriction value on the block attribute prevents derivations-by- restriction from replacing Address in an instance

97 Controlling Derived Types Preventing replacement by derivations at all, or derivations-by-extension, are indicated by the values #all and extension respectively There exists an optional blockDefault attribute on the schema element Effect of specifying the blockDefault attribute is equivalent to specifying a block attribute on every type definition and element declaration in the schema

98 Specifying Uniqueness Can indicate uniqueness of attribute/element value within a certain scope Use unique element to specify uniqueness

99 Unique Element They are defined using Commonly used attributes –name: identifies the name of the unique element Content ( annotation ?, ( selector, field +)) Helps in specifying uniqueness

100 Defining Keys Used to indicate relationships between elements Use key, keyref elements to specify relations …. Lawnmower …

101 Defining Keys …… ……

102 key Element They are defined using Commonly used attributes –name: identifies the name of the key Content ( annotation ?, ( selector, field +))

103 keyRef Element They are defined using Commonly used attributes –name: identifies the name of the keyref element Content ( annotation ?, ( selector, field +))

104 EXERCISE 15 In all 3 case studies: –Identify, document a type which should not be derived at all using xsd:final –Identify facets for derived types which need to be fixed, document them –Identify, document a type which could be derived any way, but derivation-by-restriction should not be used in its instance using xsd:block

105 EXERCISE 15 In all 3 case studies: –Identify attributes of elements which could have a relation –Establish the relation using xsd:key, xsd:keyref


Download ppt "XML Schema. Why Schema? To define a class of XML documents Serve same purpose as DTD “Instance document" used for XML document conforming to schema."

Similar presentations


Ads by Google