Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Module 3 XML Schema. 2 Recapitulation (Module 2) XML as inheriting from the Web history XML as inheriting from the Web history SGML, HTML, XHTML, XML.

Similar presentations


Presentation on theme: "1 Module 3 XML Schema. 2 Recapitulation (Module 2) XML as inheriting from the Web history XML as inheriting from the Web history SGML, HTML, XHTML, XML."— Presentation transcript:

1 1 Module 3 XML Schema

2 2 Recapitulation (Module 2) XML as inheriting from the Web history XML as inheriting from the Web history SGML, HTML, XHTML, XML SGML, HTML, XHTML, XML XML key concepts XML key concepts Documents, elements, attributes, text Documents, elements, attributes, text Order, nested structure, textual information Order, nested structure, textual information Namespaces Namespaces XML usage scenarios XML usage scenarios Financial, medical, Web Services, blogs, etc Financial, medical, Web Services, blogs, etc communication data, meta data, documents communication data, meta data, documents DTDs and the need for describing the “structure” of an XML file DTDs and the need for describing the “structure” of an XML file Next: XML Schemas Next: XML Schemas

3 3 Limitations of DTDs DTDs describe only the “grammar” of the XML file, not the detailed structure and/or types DTDs describe only the “grammar” of the XML file, not the detailed structure and/or types This grammatical description has some obvious shortcomings: This grammatical description has some obvious shortcomings: we cannot express that a “length” element must contain a non-negative number (constraints on the type of the value of an element or attribute) we cannot express that a “length” element must contain a non-negative number (constraints on the type of the value of an element or attribute) The “unit” element should only be allowed when “amount” is present (co-occurrence constraints) The “unit” element should only be allowed when “amount” is present (co-occurrence constraints) the “comment” element should be allowed to appear anywhere (schema flexibility) the “comment” element should be allowed to appear anywhere (schema flexibility) There is no subtyping / inheritance (reuse of definitions) There is no subtyping / inheritance (reuse of definitions) There are no composite keys (referential integrity) There are no composite keys (referential integrity)

4 4 Overview XML Schema ComplexTypes and SimpleTypes ComplexTypes and SimpleTypes ComplexType correspond to Records ComplexType correspond to Records „string“ is an example of a SimpleType „string“ is an example of a SimpleType Built-in and user-defined Types Built-in and user-defined Types ComplexTypes are always user-defined ComplexTypes are always user-defined Elements have complexTypes or simpleTypes; Attributes have simpleTypes Elements have complexTypes or simpleTypes; Attributes have simpleTypes Type of Root element of a document is global Type of Root element of a document is global (almost) downward comptable with DTDs (almost) downward comptable with DTDs Schemas are XML Documents (Syntax) Schemas are XML Documents (Syntax) Namespaces etc. are part of XML Schemas Namespaces etc. are part of XML Schemas

5 5 Example Schema

6 6 Example Schema...... Schema in a separate XML Document Schema in a separate XML Document Vocabulary of Schema defined in special Namespace. Prefix „xsd“ is commonly used Vocabulary of Schema defined in special Namespace. Prefix „xsd“ is commonly used There is a Schema for Schemas (don‘t worry!) There is a Schema for Schemas (don‘t worry!) „schema“ Element is always the Root „schema“ Element is always the Root

7 7 Example Schema „element“ Element in order to declare elements „element“ Element in order to declare elements „name“ defines the name of the element. „name“ defines the name of the element. „type“ defines the type of the element „type“ defines the type of the element Declarations under „schema“ are global Declarations under „schema“ are global Global element declarations are potential roots Global element declarations are potential roots Example: „book“ is the only global element, root element of a valid document must be a „book“. Example: „book“ is the only global element, root element of a valid document must be a „book“. The type of a „book“ is BookType (defined next). The type of a „book“ is BookType (defined next).

8 8 Example Schema User-defined complex type User-defined complex type Defines a sequence of sub-elements Defines a sequence of sub-elements Attribute „name“ specifies name of Type Attribute „name“ specifies name of Type This Typedefinition is global. Type can be used in any other definition. This Typedefinition is global. Type can be used in any other definition.

9 9 Example Schema Local element declaration within a complex type Local element declaration within a complex type („title“ cannot be root element of documents) („title“ cannot be root element of documents) „name“ and „type“ as before „name“ and „type“ as before „xsd:string“ is built-in type of XML Schema „xsd:string“ is built-in type of XML Schema

10 10 Example Schema Local element declaration Local element declaration „PersonType“ is user-defined type „PersonType“ is user-defined type „minOccurs“, „maxOccurs“ specify cardinality of „author“ Elements in „BookType“. „minOccurs“, „maxOccurs“ specify cardinality of „author“ Elements in „BookType“. Default: minOccurs=1, maxOccurs=1 Default: minOccurs=1, maxOccurs=1

11 11 Example Schema Local type definition Local type definition „PersonType“ may only be used inside the scope of the definition of BookType. „PersonType“ may only be used inside the scope of the definition of BookType. The same syntax as for BookType. The same syntax as for BookType.

12 12 Example Schema Local element declaratation Local element declaratation Every book has exactly one „publisher“ minOccurs, maxOccurs by default 1 Every book has exactly one „publisher“ minOccurs, maxOccurs by default 1 „anyType“ is built-in Type „anyType“ is built-in Type „anyType“ allows any content „anyType“ allows any content „anyType“ is default type. Equivalent definition: „anyType“ is default type. Equivalent definition:

13 13 Example Schema

14 14 Valid Document Die Wilde Wutz Die Wilde Wutz D. D. K. K. Addison Wesley, Addison Wesley, CA, USA CA, USA

15 15 Valid Document Die Wilde Wutz Die Wilde Wutz D. D. K. K. Addison Wesley, Addison Wesley, CA, USA CA, USA Root is book

16 16 Valid Document Die Wilde Wutz Die Wilde Wutz D. D. K. K. Addison Wesley, Addison Wesley, CA, USA CA, USA Exactly one title of Type string

17 17 Valid Document Die Wilde Wutz Die Wilde Wutz D. D. K. K. Addison Wesley, Addison Wesley, CA, USA CA, USA At least one author of Type PersonType One publisher with arbitrary content. Subelements In right order

18 18 Schema Validation Conformance Test Conformance Test Result: „true“ or „false“ Result: „true“ or „false“ Infoset Contribution Infoset Contribution Annotate Types Annotate Types Set Default Values Set Default Values Result: new instance of the data model Result: new instance of the data model Tools: Xerces (Apache) Tools: Xerces (Apache) Theory: Graph Simulation Algorithms Theory: Graph Simulation Algorithms Validation is a-posteri; explicit - not implicit! Validation is a-posteri; explicit - not implicit!

19 19 Global vs. Local Declarations Instances of global element declarations are potential root elements of documents Instances of global element declarations are potential root elements of documents Global declarations can be referenced Global declarations can be referenced Constraints Constraints „ref“ not allowed in global declarations „ref“ not allowed in global declarations No „minOccurs“, „maxOccurs“ in global Decl. No „minOccurs“, „maxOccurs“ in global Decl.

20 20 Attribute Declarations Attributes may only have a SimpleType Attributes may only have a SimpleType SimpleTypes are, e.g., „string“ (more later) SimpleTypes are, e.g., „string“ (more later) Attribute declarations can be global Attribute declarations can be global Reuse declarations with ref Reuse declarations with ref Compatible to Attribute lists in DTDs Compatible to Attribute lists in DTDs Default values possible Default values possible Required and optional attributes Required and optional attributes Fixed attributes Fixed attributes (In addition, there are „prohibited“ attributes) (In addition, there are „prohibited“ attributes)

21 21 Attribute Declarations use=„required“ /> use=„optional“ /> fixed=„EUR“ /> default=„“ />

22 22 Anonymous Types PersonType need not be „named“ PersonType need not be „named“

23 23 Simple Elements + Attributes Valid Instance: Valid Instance: 69.95

24 24 Element + Attributes, no Content Valid Instance: Valid Instance:

25 25 Pre-defined SimpleTypes Numeric Values Numeric Values Integer, Short, Decimal, Float, Double, HexBinary,... Date, Timestamps, Periods Date, Timestamps, Periods Duration, DateTime, Time, Date, gMonth,... Strings Strings String, NMTOKEN, NMTOKENS, NormalizedString Others Others Qname, AnyURI, ID, IDREFS, Language, Entity,... In summary, 44 pre-defined simple types In summary, 44 pre-defined simple types Question: How many does SQL have?

26 26 Derived SimpleTypes Restrict domain Restrict domain minInclusive, maxInclusive are „Facets“ minInclusive, maxInclusive are „Facets“

27 27 Derived SimpleTypes Restriction by Pattern Matching Restriction by Pattern Matching Currencies have three capital letters Currencies have three capital letters

28 28 Derived SimpleTypes Restriction by Enumeration Restriction by Enumeration

29 29 Derived SimpleTypes There are 15 different kinds of Facets There are 15 different kinds of Facets e.g., minExclusive, totalDigits,... e.g., minExclusive, totalDigits,... Most built-in types are derived from other built-in types by restriction Most built-in types are derived from other built-in types by restriction e.g., Integer is derived from Decimal e.g., Integer is derived from Decimal there are only 19 base types (out of 44) there are only 19 base types (out of 44) Ref: Appendix B of XML Schema Primer Ref: Appendix B of XML Schema Primer

30 30 List Types SimpleType for Lists SimpleType for Lists Built-in List Types: IDREFS, NMTOKENS Built-in List Types: IDREFS, NMTOKENS User-defined List Types User-defined List Types Items in instances are separed by whitespace Items in instances are separed by whitespace „ “ Facets for Restrictions: Facets for Restrictions: length, minLength, maxLength, enumeration length, minLength, maxLength, enumeration

31 31 Facets of List Types

32 32 Union Types Corresponds to the „|“ in DTDs Corresponds to the „|“ in DTDs (Variant Records in Pascal or Union in C) (Variant Records in Pascal or Union in C) Valid instances are valid to any of the types Valid instances are valid to any of the types Valid Instanzes Valid Instanzes „fünfzig“ „1 3 17“ „wunderbar“ „15“ Supported Facets Supported Facets pattern, enumeration pattern, enumeration

33 33 Choice: „Union“ in ComplexTypes A book has either an „author“ or an „editor“ A book has either an „author“ or an „editor“ maxOccurs = „unbounded“ />

34 34 Element Groups If the book has an „editor“, then the book also has a „sponsor“: If the book has an „editor“, then the book also has a „sponsor“:

35 35 Optional Element Groups All or nothing; unordered content All or nothing; unordered content PubInfo has „name“, „year“, „city“ or gar nothing PubInfo has „name“, „year“, „city“ or gar nothing No other element declarations allowed!!! No other element declarations allowed!!! maxOccurs must be 1 maxOccurs must be 1

36 36 Attribute Groups

37 37 Definition of Keys Keys are defined as part of elements Keys are defined as part of elements Special sub-element „key“ Special sub-element „key“ selector: describes the context (e.g., emp in a DB) selector: describes the context (e.g., emp in a DB) field: describes the key within context field: describes the key within context several fields ~ composite keys several fields ~ composite keys selector and fields are XPath expressions selector and fields are XPath expressions Validation Validation Eval „selector“ -> Sequence of Nodes Eval „selector“ -> Sequence of Nodes Eval „fields“ on Nodes -> Set of tuples Eval „fields“ on Nodes -> Set of tuples Check that there are no duplicates in Set of tuples Check that there are no duplicates in Set of tuples

38 38 Definition of Keys „isbn“ is key of „books“ in „bib“ „isbn“ is key of „books“ in „bib“......

39 39 References (foreign keys) Part of the definition of an element Part of the definition of an element Concept: „selector“ and „field(s)“ Concept: „selector“ and „field(s)“ selector: determines context of the foreign keys selector: determines context of the foreign keys field(s): specify the foreign key field(s): specify the foreign key refer: gives the scope of the references (key constr.) refer: gives the scope of the references (key constr.) Syntax; e.g., books referencing other books: Syntax; e.g., books referencing other books:

40 40 UNIQUE Constraints Same concept as in SQL Same concept as in SQL uniqueness, but no referentiability uniqueness, but no referentiability Syntax and concept almost the same as for keys Syntax and concept almost the same as for keys Part of the definition of an element Part of the definition of an element

41 41 Null Values „not there“ vs. „unknown“ (i.e., null) „not there“ vs. „unknown“ (i.e., null) „empty“ vs. „unknown“ „empty“ vs. „unknown“ Concept: Attribute „nil“with value „true“ Concept: Attribute „nil“with value „true“ Only works for elements Only works for elements Schema definition: „NULL ALLOWED“ Schema definition: „NULL ALLOWED“ nillable = „true“ /> Valid Instance with content „unknown“ Valid Instance with content „unknown“ xsi: Namespace for predefined Instances xsi: Namespace for predefined Instances Publisher may have other attributes, but content must be empty! Publisher may have other attributes, but content must be empty!

42 42 Derived Complex Types Two concepts of subtyping / inheritance Two concepts of subtyping / inheritance Subtyping via Extension Subtyping via Extension Add Elementen Add Elementen Similar to inheritance in OO Similar to inheritance in OO Subtyping via Restriction Subtyping via Restriction e.g., constrain domains of types used e.g., constrain domains of types used substituitability is preserved substituitability is preserved Further „features“ Further „features“ Constrain Sub-typisierung (~final) Constrain Sub-typisierung (~final) Abstracte Types Abstracte Types

43 43 Subtyping via Extension A „book“ is a „publikation“ A „book“ is a „publikation“

44 44 Subtyping by Extension A „bib“ contains „Publications“ A „bib“ contains „Publications“ „pub“ Elements may be books! „pub“ Elements may be books! Instanzes have „xsi:type“ Attribute Instanzes have „xsi:type“ Attribute Wilde Wutz 1984 Wilde Wutz 1984 D.A.K. D.A.K.

45 45 Subtyping via Restriction The following restrictions are allowed The following restrictions are allowed Instances of subtypes have default values Instances of subtypes have default values Instances of subtypes are fixed (i.e., constant) Instances of subtypes are fixed (i.e., constant) Instances of subtypes have stronger types (e.g., string vs. anyType) Instances of subtypes have stronger types (e.g., string vs. anyType) Instances of subtypes have mandatory fields which optional in supertype Instances of subtypes have mandatory fields which optional in supertype Supertype.minOccurs = Subtype.maxOccurs Supertype.minOccurs = Subtype.maxOccurs

46 46 Subtyping via Restriction

47 47 Substitution Groups Elements, which substitute global elem. Elements, which substitute global elem. E.g., „editor“ is a „person“... E.g., „editor“ is a „person“...

48 48 Abstract Elements and Types No instances exist No instances exist Only instances of subtypes of substitions exist Only instances of subtypes of substitions exist person in Book must be an author or editor person in Book must be an author or editor......

49 49 Constrain Subtyping Corresponds to „final“ in Java Corresponds to „final“ in Java XML Schema is more clever!(?) XML Schema is more clever!(?) Constrain the kind of subtyping (extension, restriction, all) Constrain the kind of subtyping (extension, restriction, all) Constrain the facets used Constrain the facets used You may subtype ZipCode. But all subtypes have length 5. You may subtype ZipCode. But all subtypes have length 5.

50 50 Constrain Substituability It is possible to define subtypes of „Book“ It is possible to define subtypes of „Book“ So, it is possible to reuse structe of „Book“ So, it is possible to reuse structe of „Book“ But instances of subtypes of „Book“ are NOT books themselves. But instances of subtypes of „Book“ are NOT books themselves. (Now, things get really strange!) (Now, things get really strange!)

51 51 Namespaces and XML Schema Declare the Namespace of Elements? Declare the Namespace of Elements? TargetNamespace for Global Elements TargetNamespace for Global Elements qualifies names of root elements qualifies names of root elements elementFormDefault elementFormDefault qualifies names of local (sub-) elements qualifies names of local (sub-) elements attributeFormDefault attributeFormDefault qualifies names of attributes qualifies names of attributes

52 52 Namespaces in the Schemadef. targetNamespace=„http://www.Book.com“> „book“ und „BookType“ are part of TargetNameSp. „book“ und „BookType“ are part of TargetNameSp.

53 53 Namespaces in Schemadef. targetNamespace=„http://www.Book.com“ >

54 54 Namespaces in Schemadef. targetNamespace=„http://www.Book.com“ > Target „www.Book.com“ as Default Namespace Target „www.Book.com“ as Default Namespace

55 55 Instances of Valid according to all three schemas! Valid according to all three schemas!

56 56 Schema Location in Instance Declare within an XML document, where to find the schema that should valid that document Declare within an XML document, where to find the schema that should valid that document Declare „target Namespace“ Declare „target Namespace“ Declare URI of Schema Declare URI of Schema

57 57 Unqualified „Locals“ Local Declarations are not qualifiziert Local Declarations are not qualifiziert price = „69.95“ curr = „EUR“ > Die wilde Wutz... Die wilde Wutz... Valide Instance: globally qualifed, locally not Valide Instance: globally qualifed, locally not Even works within Schema Even works within Schema Full flexibility to control use of namespaces Full flexibility to control use of namespaces

58 58 Qualifizied Sub-elements targetNamespace=„http://www.Book.com“ > elementFormDefault=„qualified“ elementFormDefault=„qualified“

59 59 Valid Instances Die wilde Wutz Die wilde Wutz D. K. D. K. Die wilde Wutz Die wilde Wutz D. K. D. K.

60 60 Qualified Attributes Enforce Qualified Attributes Enforce Qualified Attributes attributeFormDefault = „qualified“ in Element definition Enforce that certain attributes must be qualified Enforce that certain attributes must be qualified (Analogous, enforce that Sub-elements must be qualified)

61 61 Composition of Schemas Construct libraries of schemas Construct libraries of schemas Include a Schema Include a Schema Parent and child have the same Target Namespace Parent and child have the same Target Namespace Only Parent used for Validation Only Parent used for Validation Redefine: Include + Modify Redefine: Include + Modify Again, parent and child have the same Target Namespace Again, parent and child have the same Target Namespace Include individual types from a schema Include individual types from a schema

62 62Summary XML Schema is very powerful XML Schema is very powerful simple Types and complex Types simple Types and complex Types many pre-defined types many pre-defined types many ways to derive and create new types many ways to derive and create new types adopts database concepts (key, foreign keys) adopts database concepts (key, foreign keys) full control and flexibility full control and flexibility fully inline with namespaces and other XML standards fully inline with namespaces and other XML standards XML Schema is too powerful? XML Schema is too powerful? too complicated, confusing? too complicated, confusing? difficult to implement difficult to implement people use only a fraction anyway people use only a fraction anyway XML Schema is very different to what you know! XML Schema is very different to what you know! the devil is in the detail the devil is in the detail

63 63 XML vs. OO Encapsulation Encapsulation OO hides data OO hides data XML makes data explicit XML makes data explicit Type Hierarchy Type Hierarchy OO defines superset / subset relationship OO defines superset / subset relationship XML shares structure; set rel. make no sense XML shares structure; set rel. make no sense Data + Behavior Data + Behavior OO packages them together OO packages them together XML separates data from its interpretation XML separates data from its interpretation

64 64 XML vs. Relational Structural Differences Structural Differences Tree vs. Table Tree vs. Table Heterogeneous vs. Homegeneous Heterogeneous vs. Homegeneous Optional vs. Strict typing Optional vs. Strict typing Unnormalized vs. Normalized data Unnormalized vs. Normalized data Some commonalities Some commonalities Logical and physical data independance Logical and physical data independance Declarative semantics Declarative semantics Generic data model Generic data model


Download ppt "1 Module 3 XML Schema. 2 Recapitulation (Module 2) XML as inheriting from the Web history XML as inheriting from the Web history SGML, HTML, XHTML, XML."

Similar presentations


Ads by Google