64
1 Module 3 Module 3 XML Schema XML Schema

Module 3 XML Schema - Stanford Universityweb.stanford.edu/class/cs345b/slides/XMLDB-M3-Stanford.pdfModule 3 XML Schema. 2 Recapitulation (Module 2) XML as inheriting from the Web history

  • Upload
    lymien

  • View
    237

  • Download
    0

Embed Size (px)

Citation preview

1

Module 3Module 3

XML SchemaXML Schema

2

Recapitulation (Module 2)Recapitulation (Module 2) XML as inheriting from the Web historyXML as inheriting from the Web history

SGML, HTML, XHTML, XMLSGML, HTML, XHTML, XML XML key conceptsXML key concepts

Documents, elements, attributes, textDocuments, elements, attributes, text Order, nested structure, textual informationOrder, nested structure, textual information

NamespacesNamespaces XML usage scenariosXML usage scenarios

Financial, medical, Web Services, blogs, etcFinancial, medical, Web Services, blogs, etc communication data, meta data, documentscommunication data, meta data, documents

DTDs and the need for describing the “structure” of DTDs and the need for describing the “structure” of an XML filean XML file

Next: XML SchemasNext: XML Schemas

3

Limitations of DTDsLimitations of DTDs DTDs describe only the “grammar” of the XML DTDs describe only the “grammar” of the XML

file, not the detailed structure and/or typesfile, not the detailed structure and/or types This grammatical description has some obvious This grammatical description has some obvious

shortcomings:shortcomings: we cannot express that a “length” element must contain we cannot express that a “length” element must contain

a non-negative number a non-negative number (constraints on the type of the (constraints on the type of the value of an element or attribute)value of an element or attribute)

The “unit”The “unit” element should only be allowed when element should only be allowed when ““amount”amount” is present is present (co-occurrence constraints)(co-occurrence constraints)

the “the “comment”comment” element should be allowed to appear element should be allowed to appear anywhere anywhere (schema flexibility)(schema flexibility)

There is no subtyping / inheritance There is no subtyping / inheritance (reuse of definitions)(reuse of definitions) There are no composite keysThere are no composite keys (referential integrity) (referential integrity)

4

Overview XML SchemaOverview XML Schema ComplexTypes and SimpleTypesComplexTypes and SimpleTypes

ComplexType correspond to RecordsComplexType correspond to Records „„string“ is an example of a SimpleTypestring“ is an example of a SimpleType

Built-in and user-defined TypesBuilt-in and user-defined Types ComplexTypes are always user-definedComplexTypes are always user-defined

Elements have complexTypes or simpleTypes; Elements have complexTypes or simpleTypes; Attributes have simpleTypesAttributes have simpleTypes

Type of Root element of a document is globalType 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 SchemasNamespaces etc. are part of XML Schemas

5

Example SchemaExample Schema<?xml version=„1.0“ ?><?xml version=„1.0“ ?><xsd:schema xmlns:xsd=„http://w3.org/2001/XMLSchema“><xsd:schema xmlns:xsd=„http://w3.org/2001/XMLSchema“> <xsd:element name=„book“ type=„<xsd:element name=„book“ type=„BookTypeBookType“/>“/>

<xsd:complexType name=„BookType“><xsd:complexType name=„BookType“> <xsd:sequence><xsd:sequence> <xsd:element name=„title“ type=„<xsd:element name=„title“ type=„xsd:stringxsd:string“/>“/>

<xsd:element name=„author“ type=„<xsd:element name=„author“ type=„PersonTypePersonType““minOccurs=„1“ maxOccurs=„unbounded“minOccurs=„1“ maxOccurs=„unbounded“/>/>

<xsd:complexType name=„PersonType“><xsd:complexType name=„PersonType“><xsd:sequence> ... <xsd:sequence><xsd:sequence> ... <xsd:sequence>

</xsd:complexType></xsd:complexType> <xsd:element name=„publisher“ type=„<xsd:element name=„publisher“ type=„xsd:anyTypexsd:anyType“/>“/> </xsd:sequence></xsd:sequence> </xsd:complexType></xsd:complexType></xsd:schema></xsd:schema>

6

Example SchemaExample Schema

<?xml version=„1.0“ ?><?xml version=„1.0“ ?><xsd:schema <xsd:schema

xmlns:xsd=„http://w3.org/2001/XMLSchema“>xmlns:xsd=„http://w3.org/2001/XMLSchema“> ......</xsd:schema></xsd:schema>

Schema in a separate XML DocumentSchema in a separate XML Document Vocabulary of Schema defined in special Vocabulary of Schema defined in special

Namespace. Prefix „xsd“ is commonly usedNamespace. 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 Rootschema“ Element is always the Root

7

Example SchemaExample Schema <xsd:element name=„book“ type=„<xsd:element name=„book“ type=„BookTypeBookType“/>“/>

„„element“ Element in order to declare elementselement“ 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 Declarations under „schema“ are globalglobal Global element declarations are potential rootsGlobal element declarations are potential roots Example: „book“ is the only global element, Example: „book“ is the only global element,

root element of a valid document must be a „book“.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

Example SchemaExample Schema<xsd:complexType name=„BookType“><xsd:complexType name=„BookType“> <xsd:sequence><xsd:sequence> ...... </xsd:sequence></xsd:sequence></xsd:complexType></xsd:complexType>

User-defined complex typeUser-defined complex type Defines a sequence of sub-elementsDefines a sequence of sub-elements Attribute Attribute „name“„name“ specifies name of Type specifies name of Type This Typedefinition is This Typedefinition is global.global.

Type can be used in any other definition.Type can be used in any other definition.

9

Example SchemaExample Schema

<xsd:sequence><xsd:sequence> <xsd:element name=„title“ type=„<xsd:element name=„title“ type=„xsd:stringxsd:string“/>“/></xsd:sequence></xsd:sequence> Local element declaration within a complex typeLocal element declaration within a complex type („title“ cannot be root element of documents)(„title“ cannot be root element of documents) „„name“name“ and and „type“„type“ as before as before „„xsd:string“xsd:string“ is built-in type of XML Schemais built-in type of XML Schema

10

Example SchemaExample Schema<xsd:element name=„author“ type=„<xsd:element name=„author“ type=„PersonTypePersonType““

minOccurs=„1“ maxOccurs=„unbounded“minOccurs=„1“ maxOccurs=„unbounded“/>/> Local element declarationLocal element declaration „„PersonType“PersonType“ is user-defined type is user-defined type „„minOccurs“, „maxOccurs“ minOccurs“, „maxOccurs“ specify cardinality of specify cardinality of

„author“„author“ Elements in Elements in „BookType“.„BookType“. Default: Default: minOccurs=1, maxOccurs=1minOccurs=1, maxOccurs=1

11

Example SchemaExample Schema<xsd:complexType name=„PersonType“><xsd:complexType name=„PersonType“>

<xsd:sequence> <xsd:sequence> <xsd:element name=„first“ type=„xsd:string“/><xsd:element name=„first“ type=„xsd:string“/><xsd:element name=„last“ type=„xsd:string“/><xsd:element name=„last“ type=„xsd:string“/>

<xsd:sequence><xsd:sequence></xsd:complexType></xsd:complexType>

Local type definitionLocal type definition „„PersonType“PersonType“ may only be used inside the scope of may only be used inside the scope of

the definition of BookType.the definition of BookType. The same syntax as for BookType.The same syntax as for BookType.

12

Example SchemaExample Schema

<xsd:element name=„publisher“ type=„<xsd:element name=„publisher“ type=„xsd:anyTypexsd:anyType“/>“/> Local element declaratationLocal element declaratation Every book has exactly one Every book has exactly one „publisher“ „publisher“

minOccurs, maxOccurs minOccurs, maxOccurs by default 1by default 1 „„anyType“ anyType“ is built-in Typeis built-in Type „„anyType“ allows any contentanyType“ allows any content „„anyType“anyType“ is default type. Equivalent definition: is default type. Equivalent definition:

<xsd:element name=„publisher“ /><xsd:element name=„publisher“ />

13

Example SchemaExample Schema<?xml version=„1.0“ ?><?xml version=„1.0“ ?><xsd:schema xmlns:xsd=„http://w3.org/2001/XMLSchema“><xsd:schema xmlns:xsd=„http://w3.org/2001/XMLSchema“> <xsd:element name=„book“ type=„<xsd:element name=„book“ type=„BookTypeBookType“/>“/>

<xsd:complexType name=„BookType“><xsd:complexType name=„BookType“> <xsd:sequence><xsd:sequence> <xsd:element name=„title“ type=„<xsd:element name=„title“ type=„xsd:stringxsd:string“/>“/>

<xsd:element name=„author“ type=„<xsd:element name=„author“ type=„PersonTypePersonType““minOccurs=„1“ maxOccurs=„unbounded“minOccurs=„1“ maxOccurs=„unbounded“/>/>

<xsd:complexType name=„PersonType“><xsd:complexType name=„PersonType“><xsd:sequence> ... <xsd:sequence><xsd:sequence> ... <xsd:sequence>

</xsd:complexType></xsd:complexType> <xsd:element name=„publisher“ type=„<xsd:element name=„publisher“ type=„xsd:anyTypexsd:anyType“/>“/> </xsd:sequence></xsd:sequence> </xsd:complexType></xsd:complexType></xsd:schema></xsd:schema>

14

Valid DocumentValid Document<?xml version=„1.0“><?xml version=„1.0“><book><book> <title>Die Wilde Wutz</title><title>Die Wilde Wutz</title> <author><first>D.</first><author><first>D.</first> <last>K.</last></author><last>K.</last></author> <publisher> Addison Wesley, <publisher> Addison Wesley, <state>CA</state>, USA<state>CA</state>, USA </publisher></publisher></book></book>

15

Valid DocumentValid Document<?xml version=„1.0“><?xml version=„1.0“><book><book> <title>Die Wilde Wutz</title><title>Die Wilde Wutz</title> <author><first>D.</first><author><first>D.</first> <last>K.</last></author><last>K.</last></author> <publisher> Addison Wesley, <publisher> Addison Wesley, <state>CA</state>, USA<state>CA</state>, USA </publisher></publisher></book></book>

Root is book

16

Valid DocumentValid Document<?xml version=„1.0“><?xml version=„1.0“><book><book> <title>Die Wilde Wutz</title><title>Die Wilde Wutz</title> <author><first>D.</first><author><first>D.</first> <last>K.</last></author><last>K.</last></author> <publisher> Addison Wesley, <publisher> Addison Wesley, <state>CA</state>, USA<state>CA</state>, USA </publisher></publisher></book></book>

Exactly one title of Type string

17

Valid DocumentValid Document<?xml version=„1.0“><?xml version=„1.0“><book><book> <title>Die Wilde Wutz</title><title>Die Wilde Wutz</title> <author><first>D.</first><author><first>D.</first> <last>K.</last></author><last>K.</last></author> <publisher> Addison Wesley, <publisher> Addison Wesley, <state>CA</state>, USA<state>CA</state>, USA </publisher></publisher></book></book>

At least one authorof Type

PersonTypeOne publisher

with arbitrary content.

Subelements In right order

18

Schema ValidationSchema Validation Conformance TestConformance Test

Result: „true“ or „false“Result: „true“ or „false“ Infoset ContributionInfoset Contribution

Annotate TypesAnnotate Types Set Default ValuesSet Default Values Result: new instance of the data modelResult: new instance of the data model

Tools: Xerces (Apache)Tools: Xerces (Apache) Theory: Graph Simulation AlgorithmsTheory: Graph Simulation Algorithms Validation is a-posteri; explicit - not implicit!Validation is a-posteri; explicit - not implicit!

19

Global vs. Local DeclarationsGlobal vs. Local Declarations Instances of global element declarations are Instances of global element declarations are

potential root elements of documents potential root elements of documents Global declarations can be referencedGlobal declarations can be referenced

<xsd:schema xmlns:xsd=„...“><xsd:schema xmlns:xsd=„...“> <xsd:element name=„book“ type=„BookType“/> <xsd:element name=„book“ type=„BookType“/> <xsd:element name=„comment“ <xsd:element name=„comment“

type=„xsd:string“/> type=„xsd:string“/> <xsd:ComplexType name=„BookType“> <xsd:ComplexType name=„BookType“> ... <xsd:element ... <xsd:element ref=„comment“ref=„comment“ minOccurs=„0“/>... minOccurs=„0“/>...

ConstraintsConstraints „„ref“ref“ not allowed in global declarations not allowed in global declarations No No „minOccurs“, „maxOccurs“„minOccurs“, „maxOccurs“ in global Decl. in global Decl.

20

Attribute DeclarationsAttribute Declarations Attributes may only have a SimpleTypeAttributes 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 Reuse declarations with refref Compatible to Attribute lists in DTDsCompatible to Attribute lists in DTDs

Default values possibleDefault values possible Required and optional attributesRequired and optional attributes Fixed attributesFixed attributes (In addition, there are „prohibited“ attributes)(In addition, there are „prohibited“ attributes)

21

Attribute DeclarationsAttribute Declarations

<xsd:complexType name=„BookType“><xsd:complexType name=„BookType“> <xsd:sequence> ... </xsd:sequence><xsd:sequence> ... </xsd:sequence> <xsd:attribute name=„isbn“ type=„xsd:string“<xsd:attribute name=„isbn“ type=„xsd:string“ use=„required“use=„required“ />/> <xsd:attribute name=„price“ type=„xsd:decimal“<xsd:attribute name=„price“ type=„xsd:decimal“ use=„optional“use=„optional“ />/> <xsd:attribute name=„curr“ type=„xsd:string“<xsd:attribute name=„curr“ type=„xsd:string“ fixed=„EUR“fixed=„EUR“ />/> <xsd:attribute name=„index“ type=„xsd:idrefs“<xsd:attribute name=„index“ type=„xsd:idrefs“ default=„“default=„“ />/></xsd:complexType></xsd:complexType>

22

Anonymous TypesAnonymous Types PersonType PersonType need not be „named“need not be „named“<xsd:complexType name=„BookType“><xsd:complexType name=„BookType“> ...... <xsd:element name=„author“><xsd:element name=„author“> <xsd:complexType><xsd:complexType> <xsd:sequence><xsd:sequence> <xsd:element name=„first“ type=„xsd:string“/><xsd:element name=„first“ type=„xsd:string“/> <xsd:element name=„last“ type=„xsd:string“/><xsd:element name=„last“ type=„xsd:string“/> </xsd:sequence> </xsd:complexType></xsd:sequence> </xsd:complexType>

</xsd:element></xsd:element> ... ...

23

Simple Elements + AttributesSimple Elements + Attributes<xsd:element name=„price“><xsd:element name=„price“> <xsd:complexType><xsd:complexType> <xsd:simpleContent><xsd:simpleContent> <xsd:extension base= <xsd:extension base= „xsd:decimal“„xsd:decimal“ > > <xsd:attribute name=„curr“ type=„xsd:string“/><xsd:attribute name=„curr“ type=„xsd:string“/> </xsd:extension></xsd:extension> </xsd:simpleContent></xsd:simpleContent> </xsd:complexType> </xsd:element> </xsd:complexType> </xsd:element>

Valid Instance:Valid Instance: <price <price curr=„USD“curr=„USD“ >>69.9569.95</price></price>

24

Element + Attributes, no ContentElement + Attributes, no Content<xsd:element name=„price“><xsd:element name=„price“> <xsd:complexType><xsd:complexType> <xsd:attribute name=„curr“ type=„xsd:string“/><xsd:attribute name=„curr“ type=„xsd:string“/> <xsd:attribute name=„val“ type=„xsd:decimal“/><xsd:attribute name=„val“ type=„xsd:decimal“/> </xsd:complexType></xsd:complexType></xsd:element></xsd:element>

Valid Instance:Valid Instance:<price <price curr=„USD“curr=„USD“ val=„69.95“val=„69.95“ />/>

25

Pre-defined SimpleTypesPre-defined SimpleTypes Numeric ValuesNumeric Values

Integer, Short, Decimal, Float, Double, HexBinary, ...Integer, Short, Decimal, Float, Double, HexBinary, ... Date, Timestamps, PeriodsDate, Timestamps, Periods

Duration, DateTime, Time, Date, gMonth, ...Duration, DateTime, Time, Date, gMonth, ... StringsStrings

String, NMTOKEN, NMTOKENS, NormalizedStringString, NMTOKEN, NMTOKENS, NormalizedString OthersOthers

Qname, AnyURI, ID, IDREFS, Language, Entity, ...Qname, AnyURI, ID, IDREFS, Language, Entity, ... In summary, 44 pre-defined simple typesIn summary, 44 pre-defined simple types

Question: How many does SQL have?Question: How many does SQL have?

26

Derived SimpleTypesDerived SimpleTypes Restrict domainRestrict domain

<xsd:simpleType name=„MyInteger“><xsd:simpleType name=„MyInteger“> <xsd:restriction<xsd:restriction base=„xsd:integer“>base=„xsd:integer“> <xsd:minInclusive value=„10000“/><xsd:minInclusive value=„10000“/>

<xsd:maxInclusive value=„99999“/><xsd:maxInclusive value=„99999“/> </xsd:restriction></xsd:restriction></xsd:simpleType></xsd:simpleType>

minInclusive, maxInclusiveminInclusive, maxInclusive are are „Facets“„Facets“

27

Derived SimpleTypesDerived SimpleTypes

Restriction by Pattern MatchingRestriction by Pattern Matching Currencies have three capital lettersCurrencies have three capital letters

<xsd:simpleType name=„Currency“><xsd:simpleType name=„Currency“> <xsd:restriction <xsd:restriction base=„xsd:string“base=„xsd:string“ > > <xsd:pattern value=„[A-Z]{3}“/><xsd:pattern value=„[A-Z]{3}“/> </xsd:restriction></xsd:restriction></xsd:simpleType></xsd:simpleType>

28

Derived SimpleTypesDerived SimpleTypes Restriction by EnumerationRestriction by Enumeration

<xsd:simpleType name=„Currency“><xsd:simpleType name=„Currency“> <xsd:restriction <xsd:restriction base=„xsd:string“base=„xsd:string“ > > <xsd:enumeration value=„ATS“/><xsd:enumeration value=„ATS“/> <xsd:enumeration value=„EUR“/><xsd:enumeration value=„EUR“/> <xsd:enumeration value=„GBP“/><xsd:enumeration value=„GBP“/> <xsd:enumeration value=„USD“/> <xsd:enumeration value=„USD“/> </xsd:restriction></xsd:restriction></xsd:simpleType></xsd:simpleType>

29

Derived SimpleTypesDerived SimpleTypes

There are 15 different kinds of FacetsThere are 15 different kinds of Facets e.g., minExclusive, totalDigits, ...e.g., minExclusive, totalDigits, ...

Most built-in types are derived from other Most built-in types are derived from other built-in types by restrictionbuilt-in types by restriction e.g., Integer is derived from Decimale.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 PrimerRef: Appendix B of XML Schema Primer

30

List TypesList Types SimpleType for ListsSimpleType for Lists Built-in List Types: IDREFS, NMTOKENSBuilt-in List Types: IDREFS, NMTOKENS User-defined List TypesUser-defined List Types

<xsd:simpleType name = „intList“ ><xsd:simpleType name = „intList“ > <xsd:list itemType = „xsd:integer“ /><xsd:list itemType = „xsd:integer“ /></xsd:simpleType></xsd:simpleType>

Items in instances are separed by whitespace Items in instances are separed by whitespace „„5 -10 7 -20“5 -10 7 -20“

Facets for Restrictions:Facets for Restrictions: length, minLength, maxLength, enumerationlength, minLength, maxLength, enumeration

31

Facets of List TypesFacets of List Types

<xsd:simpleType name = „Participants“ ><xsd:simpleType name = „Participants“ > <xsd:list itemType = „xsd:string“ /><xsd:list itemType = „xsd:string“ /><xsd:simpleType><xsd:simpleType>

<xsd:simpleType name = „Medalists“ ><xsd:simpleType name = „Medalists“ > <xsd:restriction base = „Participants“ ><xsd:restriction base = „Participants“ > <xsd:length value = „3“ /><xsd:length value = „3“ /> </xsd:restriction></xsd:restriction></xsd:simpleType></xsd:simpleType>

32

Union TypesUnion Types Corresponds to the „|“ in DTDsCorresponds 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 typesValid instances are valid to any of the types

<xsd:simpleType name = „Potpurri“ ><xsd:simpleType name = „Potpurri“ > <xsd:union memberTypes = „xsd:string intList“/><xsd:union memberTypes = „xsd:string intList“/></xsd:simpleType></xsd:simpleType>

Valid InstanzesValid Instanzes„„fünfzig“ „1 3 17“ „wunderbar“ „15“fünfzig“ „1 3 17“ „wunderbar“ „15“

Supported FacetsSupported Facets pattern, enumerationpattern, enumeration

33

Choice: „Union“ in ComplexTypesChoice: „Union“ in ComplexTypes A book has either an A book has either an „author“„author“ or an „ or an „editor“editor“

<xsd:complexType name = „Book“ > <xsd:sequence><xsd:complexType name = „Book“ > <xsd:sequence> <xsd:choice><xsd:choice> <xsd:element name = „author“ type = „Person“<xsd:element name = „author“ type = „Person“ maxOccurs = „unbounded“ />maxOccurs = „unbounded“ /> <xsd:element name = „editor“ type = „Person“ /><xsd:element name = „editor“ type = „Person“ /> </xsd:choice></xsd:choice></xsd:sequence> </xsd:complexType></xsd:sequence> </xsd:complexType>

34

Element GroupsElement Groups If the book has an „editor“, then the book also has a If the book has an „editor“, then the book also has a

„sponsor“:„sponsor“:

<xsd:complexType name = „Book“ > <xsd:sequence><xsd:complexType name = „Book“ > <xsd:sequence> <xsd:choice><xsd:choice>

<xsd:element name = „Author“ type = „Person“ .../><xsd:element name = „Author“ type = „Person“ .../> <xsd:group<xsd:group ref = „EditorSponsor“ref = „EditorSponsor“ />/>

</xsd:choice></xsd:choice> </xsd:sequence> </xsd:complexType></xsd:sequence> </xsd:complexType>

<xsd:group name = „EditorSponsor“ > <xsd:sequence> <xsd:group name = „EditorSponsor“ > <xsd:sequence> <xsd:element name =„Editor“ type=„Person“ /><xsd:element name =„Editor“ type=„Person“ />

<xsd:element name = „Sponsor“ type = „Org“ /><xsd:element name = „Sponsor“ type = „Org“ /></xsd:sequence> </xsd:group></xsd:sequence> </xsd:group>

35

Optional Element GroupsOptional Element Groups All or nothing; unordered contentAll or nothing; unordered content PubInfo has „name“, „year“, „city“ PubInfo has „name“, „year“, „city“

or gar nothingor gar nothing <xsd:complexType name = „PubInfo“ > <xsd:sequence><xsd:complexType name = „PubInfo“ > <xsd:sequence>

<xsd:all><xsd:all> <xsd:element name = „name“ type = „xsd:string“/><xsd:element name = „name“ type = „xsd:string“/> <xsd:element name = „year“ type = „xsd:string“ /><xsd:element name = „year“ type = „xsd:string“ /> <xsd:element name = „ort“ type = „xsd:string“ /><xsd:element name = „ort“ type = „xsd:string“ /> </xsd:all></xsd:all> <!-- Attributdeklarationen --><!-- Attributdeklarationen --></xsd:sequence> </xsd:complexType></xsd:sequence> </xsd:complexType>

No other element declarations allowed!!!No other element declarations allowed!!! maxOccurs must be 1maxOccurs must be 1

36

Attribute GroupsAttribute Groups<xsd:attributeGroup name = „PriceInfo“ ><xsd:attributeGroup name = „PriceInfo“ > <xsd:attribute name = „curr“ type = „xsd:string“ /><xsd:attribute name = „curr“ type = „xsd:string“ /> <xsd:attribute name = „val“ type = „xsd:decimal“ /><xsd:attribute name = „val“ type = „xsd:decimal“ /></xsd:attributeGroup></xsd:attributeGroup>

<xsd:complexType name = „Book“ ><xsd:complexType name = „Book“ > ...... <xsd:attributeGroup <xsd:attributeGroup ref = „PriceInfo“ref = „PriceInfo“ /> /></xsd:complexType></xsd:complexType>

37

Definition of KeysDefinition of Keys Keys are defined as part of elementsKeys 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 contextfield: describes the key within context several fields several fields ~ composite keys~ composite keys

selector and fields are XPath expressionsselector and fields are XPath expressions ValidationValidation

Eval „Eval „selectorselector“ -> “ -> Sequence of NodesSequence of Nodes Eval „Eval „fieldsfields“ on “ on NodesNodes -> -> Set of tuplesSet of tuples Check that there are no duplicates in Check that there are no duplicates in Set of tuplesSet of tuples

38

Definition of KeysDefinition of Keys „„isbn“isbn“ is key of is key of „books“„books“ in „bib“ in „bib“

<element name = „bib“> <complexType> <sequence><element name = „bib“> <complexType> <sequence> <element book maxOccurs = „unbounded> <element book maxOccurs = „unbounded>

<complexType> <sequence> ... </sequence> <complexType> <sequence> ... </sequence> <attribute <attribute name = „isbn“name = „isbn“ type = „string“ /> type = „string“ /> </complexType> </element></complexType> </element> </sequence></sequence> <key name = „constraintX“ ><key name = „constraintX“ > <selector xpath = „book“ /> <selector xpath = „book“ />

<field xpath = „@isbn“ /> <field xpath = „@isbn“ /> </key></key></complexType> </element></complexType> </element>

39

References (foreign keys)References (foreign keys) Part of the definition of an elementPart of the definition of an element Concept: Concept: „selector“„selector“ and and „field(s)“ „field(s)“

selector: determines context of the foreign keysselector: 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:<keyref name = „constraintY“ refer = „constraintX“ ><keyref name = „constraintY“ refer = „constraintX“ > <selector xpath = „book/references“ /> <selector xpath = „book/references“ /> <field xpath = „@isbn“ /><field xpath = „@isbn“ /></keyref></keyref>

40

UNIQUE ConstraintsUNIQUE Constraints

Same concept as in SQLSame concept as in SQL uniqueness, but no referentiabilityuniqueness, but no referentiability

Syntax and concept almost the same as for keysSyntax and concept almost the same as for keys<unique name = „constraintZ“><unique name = „constraintZ“> <selector xpath = „book“ /><selector xpath = „book“ /> <field xpath = „title“ /><field xpath = „title“ /></unique></unique>

Part of the definition of an elementPart of the definition of an element

41

Null ValuesNull 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 elementsOnly works for elements Schema definition: „NULL ALLOWED“Schema definition: „NULL ALLOWED“

<xsd:element name = „publisher“ type = „PubInfo“<xsd:element name = „publisher“ type = „PubInfo“ nillable = „true“nillable = „true“ /> />

Valid Instance with content „unknown“Valid Instance with content „unknown“<publisher <publisher xsi:nil = „true“xsi:nil = „true“ /> />

xsi: Namespace for predefined Instancesxsi: Namespace for predefined Instances Publisher may have other attributes, but content Publisher may have other attributes, but content

must be empty!must be empty!

42

Derived Complex TypesDerived Complex Types Two concepts of subtyping / inheritanceTwo concepts of subtyping / inheritance Subtyping via ExtensionSubtyping via Extension

Add ElementenAdd Elementen Similar to inheritance in OOSimilar to inheritance in OO

Subtyping via RestrictionSubtyping via Restriction e.g., constrain domains of types usede.g., constrain domains of types used substituitability is preservedsubstituitability is preserved

Further „features“Further „features“ Constrain Sub-typisierung (Constrain Sub-typisierung (~final~final)) Abstracte TypesAbstracte Types

43

Subtyping via ExtensionSubtyping via Extension A „book“ is a „publikation“A „book“ is a „publikation“<xsd:complexType name = „Publication“> <xsd:complexType name = „Publication“>

<xsd:sequence><xsd:sequence> <xsd:element name = „title“ type = „xsd:string“ /><xsd:element name = „title“ type = „xsd:string“ /> <xsd:element name = „year“ type = „xsd:integer“ /> <xsd:element name = „year“ type = „xsd:integer“ /> </xsd:sequence> </xsd:complexType> </xsd:sequence> </xsd:complexType> <xsd:complexType name = „Book“> <xsd:complexType name = „Book“>

<xsd:complexContent><xsd:complexContent> <xsd:extension base =<xsd:extension base = „Publication“„Publication“ > >

<xsd:sequence><xsd:sequence> <xsd:element name = „author“ type = „Person“ /><xsd:element name = „author“ type = „Person“ /> </xsd:sequence> </xsd:sequence> </xsd:extension></xsd:extension></xsd:complexContent> </xsd:complexType></xsd:complexContent> </xsd:complexType>

44

Subtyping by ExtensionSubtyping by Extension A „bib“ contains „Publications“ A „bib“ contains „Publications“ <xsd:element name = „bib“ > <xsd:sequence><xsd:element name = „bib“ > <xsd:sequence> <xsd:element name = „pub“ type = „Publication“ <xsd:element name = „pub“ type = „Publication“

maxOccurs = „unbounded“/>maxOccurs = „unbounded“/></xsd:sequence> </xsd:element></xsd:sequence> </xsd:element> „„pub“pub“ Elements may be books!Elements may be books! Instanzes have „xsi:type“ AttributeInstanzes have „xsi:type“ Attribute<bib> <pub <bib> <pub xsi:type = „Book“xsi:type = „Book“>> <title>Wilde Wutz</title><year>1984</year><title>Wilde Wutz</title><year>1984</year> <author>D.A.K.</author> </pub><author>D.A.K.</author> </pub></bib></bib>

45

Subtyping via RestrictionSubtyping via Restriction The following restrictions are allowedThe following restrictions are allowed

Instances of subtypes have default valuesInstances 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., Instances of subtypes have stronger types (e.g.,

string vs. anyType)string vs. anyType) Instances of subtypes have mandatory fields which Instances of subtypes have mandatory fields which

optional in supertypeoptional in supertype Supertype.minOccurs <= Subtype.minOccursSupertype.minOccurs <= Subtype.minOccurs

Supertype.maxOccurs >= Subtype.maxOccursSupertype.maxOccurs >= Subtype.maxOccurs

46

Subtyping via RestrictionSubtyping via Restriction<complexType name = „superType“> <sequence><complexType name = „superType“> <sequence> <element name = „a“ type = „string“ minOccurs = „0“ /><element name = „a“ type = „string“ minOccurs = „0“ /> <element name = „b“ type = „anyType“ /> <element name = „b“ type = „anyType“ /> <element name = „c“ type = „decimal“ /><element name = „c“ type = „decimal“ /></sequence> <complexType></sequence> <complexType>

<complexType name = „subType“> <complexContent><complexType name = „subType“> <complexContent> <restriction base = „superType“><restriction base = „superType“> <sequence> <sequence> <element name = „a“ type = „string“ minOccurs = „0“ <element name = „a“ type = „string“ minOccurs = „0“

maxOccurs = „0“maxOccurs = „0“ /> /> <element name = „b“ type = „<element name = „b“ type = „stringstring“ /> “ /> <element name = „c“ type = „decimal“ /><element name = „c“ type = „decimal“ /> </sequence> </sequence> </restriction></restriction></complexContent> </complexType></complexContent> </complexType>

47

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

<element name = „<element name = „personperson“ type = „string“ />“ type = „string“ /><complexType name = „Book“ > <sequence><complexType name = „Book“ > <sequence> <element <element ref = „ref = „personperson““ /> ... /> ...</sequence> </complexType></sequence> </complexType>

<element name = „<element name = „authorauthor“ type = „string““ type = „string“ substitutionGroup = „person“substitutionGroup = „person“ /> /><element name = „<element name = „editoreditor“ type = „string““ type = „string“ substitutionGroup = „person“substitutionGroup = „person“ /> />

48

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

<element name = „person“ type = „string“ <element name = „person“ type = „string“ abstract = „true“abstract = „true“ /> /><complexType name = „Book“ > <sequence><complexType name = „Book“ > <sequence> <element ref = „ <element ref = „personperson“ /> ...“ /> ...</sequence> </complexType></sequence> </complexType>

......

49

Constrain SubtypingConstrain Subtyping Corresponds to „Corresponds to „final“ in Javafinal“ 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 usedConstrain the facets used<simpleType name = „ZipCode“ ><simpleType name = „ZipCode“ > <restriction base = „string“><restriction base = „string“> <length value = „5“ <length value = „5“ fixed = „true“fixed = „true“ /> /> </restriction> <simpleType></restriction> <simpleType><complexType name = „Book“ <complexType name = „Book“ final = „restriction“final = „restriction“ > >... </complexType>... </complexType>

You may subtype ZipCode. But all subtypes have You may subtype ZipCode. But all subtypes have length 5.length 5.

50

Constrain SubstituabilityConstrain Substituability<complexType name = „Book“ <complexType name = „Book“ block = „all“block = „all“ > >... </complexType>... </complexType>

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 But instances of subtypes of „Book“ are NOT

books themselves.books themselves. (Now, things get really strange!)(Now, things get really strange!)

51

Namespaces and XML SchemaNamespaces and XML Schema Declare the Namespace of Elements?Declare the Namespace of Elements? TargetNamespace for Global ElementsTargetNamespace for Global Elements

qualifies names of root elementsqualifies names of root elements elementFormDefaultelementFormDefault

qualifies names of local (sub-) elementsqualifies names of local (sub-) elements attributeFormDefaultattributeFormDefault

qualifies names of attributesqualifies names of attributes

52

Namespaces in the Schemadef.Namespaces in the Schemadef.<xsd:schema <xsd:schema

xmlns:xsd=„http://w3.org/2001/XMLSchema“xmlns:xsd=„http://w3.org/2001/XMLSchema“ xmlns:bo=„http://www.Book.com“xmlns:bo=„http://www.Book.com“ targetNamespace=„http://www.Book.com“targetNamespace=„http://www.Book.com“>> <xsd:element <xsd:element name=„book“name=„book“ type=„bo:BookType“type=„bo:BookType“/>/> <xsd:complexType <xsd:complexType name=„bo:BookTypename=„bo:BookType““ > > ... </xsd:complexType>... </xsd:complexType> </xsd:schema></xsd:schema>

„„book“ und „BookType“ are part of TargetNameSp.book“ und „BookType“ are part of TargetNameSp.

53

Namespaces in Schemadef.Namespaces in Schemadef.<schema xmlns = „http://w3.org/2001/XMLSchema“<schema xmlns = „http://w3.org/2001/XMLSchema“ xmlns:bo=„http://www.Book.com“xmlns:bo=„http://www.Book.com“ targetNamespace=„http://www.Book.com“ >targetNamespace=„http://www.Book.com“ > <element<element name=„book“ type name=„book“ type = „bo:BookType“ />= „bo:BookType“ /> <complexType name<complexType name=„BookType“ >=„BookType“ > ... ... </complexType> </complexType> </schema></schema>

54

Namespaces in Schemadef.Namespaces in Schemadef.<xsd:schema <xsd:schema

xmlns:xsd=„http://w3.org/2001/XMLSchema“xmlns:xsd=„http://w3.org/2001/XMLSchema“ xmlns =„http://www.Book.com“xmlns =„http://www.Book.com“

targetNamespace=„http://www.Book.com“ >targetNamespace=„http://www.Book.com“ > <xsd:element name=„book“ <xsd:element name=„book“ type = „BookType“type = „BookType“ /> /> <xsd:complexType xsd:name=„BookType“ ><xsd:complexType xsd:name=„BookType“ > ... </xsd:complexType>... </xsd:complexType> </xsd:schema></xsd:schema>

Target „www.Book.com“ as Default NamespaceTarget „www.Book.com“ as Default Namespace

55

Instances of www.Book.comInstances of www.Book.com

<bo:book xmlns:bo = „http://www.Book.com“ ><bo:book xmlns:bo = „http://www.Book.com“ > ......</bo:book></bo:book>

Valid according to all three schemas!Valid according to all three schemas!

56

Schema Location in InstanceSchema Location in Instance Declare within an XML document, where to find the Declare within an XML document, where to find the

schema that should valid that documentschema that should valid that document Declare „Declare „target Namespacetarget Namespace“ “ Declare Declare URIURI of Schemaof Schema

<book xmlns = „http://www.Book.com“<book xmlns = „http://www.Book.com“ xmlns:xsi = „http://w3.org/XMLSchema-instance“xmlns:xsi = „http://w3.org/XMLSchema-instance“ xsi:schemaLocation = „xsi:schemaLocation = „http://www.Book.comhttp://www.Book.com http://www.book.com/Book.xsdhttp://www.book.com/Book.xsd““ ......</book></book>

This is not enforced! This is not enforced! Validation using other Schemas is legal.Validation using other Schemas is legal.

57

Unqualified „Locals“Unqualified „Locals“ Local Declarations are not qualifiziert Local Declarations are not qualifiziert

<<bo:bookbo:book xmlns:bo = „http://www.Book.com“ xmlns:bo = „http://www.Book.com“ priceprice = „69.95“ = „69.95“ currcurr = „EUR“ >= „EUR“ > <<titletitle>Die wilde Wutz</>Die wilde Wutz</titletitle> ...> ...<</bo:book/bo:book>>

Valide Instance: globally qualifed, locally notValide Instance: globally qualifed, locally not Even works within SchemaEven works within Schema

<<xsd:elementxsd:element namename = „...“ = „...“ typetype = „...“ /> = „...“ /> Full flexibility to control use of namespacesFull flexibility to control use of namespaces

58

Qualifizied Sub-elementsQualifizied Sub-elements<schema xmlns = „http://w3.org/2001/XMLSchema“<schema xmlns = „http://w3.org/2001/XMLSchema“ xmlns:bo=„http://www.Book.com“xmlns:bo=„http://www.Book.com“ targetNamespace=„http://www.Book.com“ >targetNamespace=„http://www.Book.com“ > elementFormDefault=„qualified“elementFormDefault=„qualified“ <element name=„book“ type = „bo:BookType“ /><element name=„book“ type = „bo:BookType“ /> <complexType name=„BookType“ > <sequence><complexType name=„BookType“ > <sequence> <element <element name = „title“name = „title“ type = „string“ /> type = „string“ /> <element <element name = „author“name = „author“ /> <sequence> /> <sequence> <element <element name = „vname“name = „vname“ type = „string“ /> type = „string“ /> <element <element name = „nname“name = „nname“ type = „string“ /> type = „string“ /> </sequence> </sequence> </complexType> </sequence> </sequence> </complexType> </schema></schema>

59

Valid InstancesValid Instances<bo:book<bo:book xmlns:bo = „http://www.Book.com“ xmlns:bo = „http://www.Book.com“

<<bo:titlebo:title>Die wilde Wutz</>Die wilde Wutz</bo:titlebo:title>> <<bo:authorbo:author><><bo:vnamebo:vname>D.</>D.</bo:vnamebo:vname>>

<<bo:nnamebo:nname>K.</>K.</bo:nnamebo:nname></></bo:authorbo:author>>

<</bo:book/bo:book>>

<<bookbook xmlns = „http://www.Book.com“ xmlns = „http://www.Book.com“ < <titletitle>Die wilde Wutz</>Die wilde Wutz</titletitle>>

<<authorauthor><><vnamevname>D.</>D.</vnamevname>> < <nnamenname>K.</>K.</nnamenname></></authorauthor>>

<</book/book>>

60

Qualified AttributesQualified Attributes

Enforce Qualified AttributesEnforce Qualified AttributesattributeFormDefault = „qualified“attributeFormDefault = „qualified“ in Element in Element

definitiondefinition Enforce that certain attributes must be Enforce that certain attributes must be

qualifiedqualified<attribute name = „...“ type = „...“ form = <attribute name = „...“ type = „...“ form =

„qualified“ />„qualified“ />(Analogous, enforce that Sub-elements must be (Analogous, enforce that Sub-elements must be

qualified)qualified)

61

Composition of Schemas Composition of Schemas Construct libraries of schemasConstruct libraries of schemas Include a SchemaInclude a Schema

Parent and child have the same Target Parent and child have the same Target NamespaceNamespace

Only Parent used for ValidationOnly Parent used for Validation Redefine: Include + ModifyRedefine: Include + Modify

Again, parent and child have the same Target Again, parent and child have the same Target NamespaceNamespace

Include individual types from a schemaInclude individual types from a schema<element ref = „lib:impType“ /><element ref = „lib:impType“ />

62

SummarySummary XML Schema is very powerfulXML Schema is very powerful

simple Types and complex Typessimple Types and complex Types many pre-defined typesmany pre-defined types many ways to derive and create new typesmany ways to derive and create new types adopts database concepts (key, foreign keys)adopts database concepts (key, foreign keys) full control and flexibilityfull control and flexibility fully inline with namespaces and other XML standardsfully 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 implementdifficult to implement people use only a fraction anywaypeople 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 detailthe devil is in the detail

63

XML vs. OOXML vs. OO EncapsulationEncapsulation

OO hides dataOO hides data XML makes data explicitXML makes data explicit

Type HierarchyType Hierarchy OO defines superset / subset relationshipOO defines superset / subset relationship XML shares structure; XML shares structure; setset rel. make no sense rel. make no sense

Data + BehaviorData + Behavior OO packages them togetherOO packages them together XML separates data from its interpretationXML separates data from its interpretation

64

XML vs. RelationalXML vs. Relational Structural DifferencesStructural Differences

Tree vs. TableTree vs. Table Heterogeneous vs. HomegeneousHeterogeneous vs. Homegeneous Optional vs. Strict typingOptional vs. Strict typing Unnormalized vs. Normalized dataUnnormalized vs. Normalized data

Some commonalitiesSome commonalities Logical and physical data independanceLogical and physical data independance Declarative semanticsDeclarative semantics Generic data model Generic data model