XML and Content Management Lecture 3: Modelling XML Documents: XML - - PowerPoint PPT Presentation

xml and content management
SMART_READER_LITE
LIVE PREVIEW

XML and Content Management Lecture 3: Modelling XML Documents: XML - - PowerPoint PPT Presentation

XML and Content Management Lecture 3: Modelling XML Documents: XML Schema Maciej Ogrodniczuk, Patryk Czarnik MIMUW, Oct 18, 2010 Lecture 3: XML Schema XML and Content Management 1 DTD example (recall) <!ELEMENT glossary (entry)+>


slide-1
SLIDE 1

XML and Content Management

Lecture 3: Modelling XML Documents: XML Schema

Maciej Ogrodniczuk, Patryk Czarnik

MIMUW, Oct 18, 2010

Lecture 3: XML Schema XML and Content Management 1

slide-2
SLIDE 2

DTD example (recall)

<!ELEMENT glossary (entry)+>

Lecture 3: XML Schema XML and Content Management 2

slide-3
SLIDE 3

DTD example (recall)

<!ELEMENT glossary (entry)+> <!ELEMENT entry (term, description)>

Lecture 3: XML Schema XML and Content Management 2

slide-4
SLIDE 4

DTD example (recall)

<!ELEMENT glossary (entry)+> <!ELEMENT entry (term, description)> <!ATTLIST entry id ID #REQUIRED lastMod NMTOKEN #IMPLIED>

Lecture 3: XML Schema XML and Content Management 2

slide-5
SLIDE 5

DTD example (recall)

<!ELEMENT glossary (entry)+> <!ELEMENT entry (term, description)> <!ATTLIST entry id ID #REQUIRED lastMod NMTOKEN #IMPLIED> <!ELEMENT term (#PCDATA)>

Lecture 3: XML Schema XML and Content Management 2

slide-6
SLIDE 6

DTD example (recall)

<!ELEMENT glossary (entry)+> <!ELEMENT entry (term, description)> <!ATTLIST entry id ID #REQUIRED lastMod NMTOKEN #IMPLIED> <!ELEMENT term (#PCDATA)> <!ELEMENT description (#PCDATA | link)*>

Lecture 3: XML Schema XML and Content Management 2

slide-7
SLIDE 7

DTD example (recall)

<!ELEMENT glossary (entry)+> <!ELEMENT entry (term, description)> <!ATTLIST entry id ID #REQUIRED lastMod NMTOKEN #IMPLIED> <!ELEMENT term (#PCDATA)> <!ELEMENT description (#PCDATA | link)*> <!ELEMENT link (#PCDATA)>

Lecture 3: XML Schema XML and Content Management 2

slide-8
SLIDE 8

DTD example (recall)

<!ELEMENT glossary (entry)+> <!ELEMENT entry (term, description)> <!ATTLIST entry id ID #REQUIRED lastMod NMTOKEN #IMPLIED> <!ELEMENT term (#PCDATA)> <!ELEMENT description (#PCDATA | link)*> <!ELEMENT link (#PCDATA)> <!ATTLIST link term IDREF #REQUIRED>

Lecture 3: XML Schema XML and Content Management 2

slide-9
SLIDE 9

DTD drawbacks

Limited control over document structure

Lecture 3: XML Schema XML and Content Management 3

slide-10
SLIDE 10

DTD drawbacks

Limited control over document structure Too general data types: not possible to limit text content to numbers, dates etc.

Lecture 3: XML Schema XML and Content Management 3

slide-11
SLIDE 11

DTD drawbacks

Limited control over document structure Too general data types: not possible to limit text content to numbers, dates etc. Poor methods of controlling number of elements occurrence: <!ELEMENT stanza (line, line, line, line, line?, line?, line?, line?)>

Lecture 3: XML Schema XML and Content Management 3

slide-12
SLIDE 12

DTD drawbacks

Limited control over document structure Too general data types: not possible to limit text content to numbers, dates etc. Poor methods of controlling number of elements occurrence: <!ELEMENT stanza (line, line, line, line, line?, line?, line?, line?)> Little abilities to modularly relate similar definitions. The existing

  • nes (mainly parametric entities) operates on text level, not

logical level, hence they have many limitations.

Lecture 3: XML Schema XML and Content Management 3

slide-13
SLIDE 13

DTD drawbacks

Limited control over document structure Too general data types: not possible to limit text content to numbers, dates etc. Poor methods of controlling number of elements occurrence: <!ELEMENT stanza (line, line, line, line, line?, line?, line?, line?)> Little abilities to modularly relate similar definitions. The existing

  • nes (mainly parametric entities) operates on text level, not

logical level, hence they have many limitations. Non-XML syntax (SGML roots)

Lecture 3: XML Schema XML and Content Management 3

slide-14
SLIDE 14

The need of strict text content control

Modern applications of XML Electronic data interchange, WebServices, . . . Databases rather than text documents Format (and even values) of text fields as a part of document structure

Lecture 3: XML Schema XML and Content Management 4

slide-15
SLIDE 15

The need of strict text content control

Modern applications of XML Electronic data interchange, WebServices, . . . Databases rather than text documents Format (and even values) of text fields as a part of document structure Example <order> <products-number>Not too few and not too many</products-number> </order>

Lecture 3: XML Schema XML and Content Management 4

slide-16
SLIDE 16

The need of strict text content control

Modern applications of XML Electronic data interchange, WebServices, . . . Databases rather than text documents Format (and even values) of text fields as a part of document structure Example <order> <products-number>Not too few and not too many</products-number> </order> Example <order> <products-number>-999</products-number> </order>

Lecture 3: XML Schema XML and Content Management 4

slide-17
SLIDE 17

XML Schema specification

1999: requirements for new standard published by W3C 2001: XML Schema becomes a W3C Recommendation, three parts:

XML Schema Part 0: Primer, XML Schema Part 1: Structures, XML Schema Part 2: Datatypes.

2004: second edition of the recommendation

Lecture 3: XML Schema XML and Content Management 5

slide-18
SLIDE 18

XML Schema — main structure

XML Schema extends DTD capabilities (automatic conversion from DTD to XML Schema available)

without entities.

Lecture 3: XML Schema XML and Content Management 6

slide-19
SLIDE 19

XML Schema — main structure

XML Schema extends DTD capabilities (automatic conversion from DTD to XML Schema available)

without entities.

A schema is an XML document.

Lecture 3: XML Schema XML and Content Management 6

slide-20
SLIDE 20

XML Schema — main structure

XML Schema extends DTD capabilities (automatic conversion from DTD to XML Schema available)

without entities.

A schema is an XML document. Elements (and other components) relevant to the standard in XML Schema namespace http://www.w3.org/2001/XMLSchema (prefix xsd used in the rest of lecture).

Lecture 3: XML Schema XML and Content Management 6

slide-21
SLIDE 21

XML Schema — main structure

XML Schema extends DTD capabilities (automatic conversion from DTD to XML Schema available)

without entities.

A schema is an XML document. Elements (and other components) relevant to the standard in XML Schema namespace http://www.w3.org/2001/XMLSchema (prefix xsd used in the rest of lecture). Root element is <xsd:schema>.

Lecture 3: XML Schema XML and Content Management 6

slide-22
SLIDE 22

XML Schema — main structure

XML Schema extends DTD capabilities (automatic conversion from DTD to XML Schema available)

without entities.

A schema is an XML document. Elements (and other components) relevant to the standard in XML Schema namespace http://www.w3.org/2001/XMLSchema (prefix xsd used in the rest of lecture). Root element is <xsd:schema>. Root element contains “global definitions”, such as <xsd:element> or <xsd:attribute>.

Lecture 3: XML Schema XML and Content Management 6

slide-23
SLIDE 23

XML Schema — trivial example

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="glossary"> <xs:complexType> <xs:sequence> <xs:element ref="entry" minOccurs="1" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="entry"> <xs:complexType> <xs:sequence> <xs:element ref="term"/> <xs:element ref="description"/> </xs:sequence> <xs:attribute name="id" type="xs:ID"/> <xs:attribute name="lastMod" type="xs:dateTime"/> </xs:complexType> </xs:element> <xs:element name="term" type="xs:string"/> <xs:element name="description" type="xs:string"/> </xs:schema>

Lecture 3: XML Schema XML and Content Management 7

slide-24
SLIDE 24

Types in XML Schema

Types assigned to elements and attributes Set of predefined types Means for defining author’s own types

Lecture 3: XML Schema XML and Content Management 8

slide-25
SLIDE 25

Types in XML Schema

Types assigned to elements and attributes Set of predefined types Means for defining author’s own types Assigning types to elements <xsd:element name="entry" type="TEntry"/> <xsd:complexType name="TEntry">...</xsd:complexType> <xsd:element name="term" type="xsd:string"/>

Lecture 3: XML Schema XML and Content Management 8

slide-26
SLIDE 26

Types: simple vs complex

Simple types Text strings, numbers, date and time, etc. Values written as text Applied to elements and attributes

Lecture 3: XML Schema XML and Content Management 9

slide-27
SLIDE 27

Types: simple vs complex

Simple types Text strings, numbers, date and time, etc. Values written as text Applied to elements and attributes Complex types Structures (subelements, attributes) Document tree fragments Applied to elements only

Lecture 3: XML Schema XML and Content Management 9

slide-28
SLIDE 28

XML Schema predefined types

boolean — logical value (true, false, 0, or 1), string — characters string, normalizedString — string to be normalized during processing; every white space character is replaced with space character (#x20,) token — like above; additionally, leading and trailing white spaces are removed, hexBinary, base64Binary — binary data in hexadecimal or base64 notation,

Lecture 3: XML Schema XML and Content Management 10

slide-29
SLIDE 29

XML Schema predefined types

decimal — rational number (positive or negative) in decimal notation, float — 32-bit floating point number (including special values:

  • INF, INF, and NaN),

double — 64-bit floating point number (including -INF, INF, and NaN), integer — most general type for integer numbers, long, int, short, byte — integer values from appropriate intervals (like in Java), date, time, dateTime, duration, gYearMonth, ... ID, IDREF, IDREFS, ENTITY, ENTITIES, NOTATION, NMTOKEN, NMTOKENS, CDATA, language, Name, normalizedString, token, uriReference...

Lecture 3: XML Schema XML and Content Management 10

slide-30
SLIDE 30

Predefined types

Lecture 3: XML Schema XML and Content Management 11

slide-31
SLIDE 31

Predefined types

basic types Lecture 3: XML Schema XML and Content Management 11

slide-32
SLIDE 32

Predefined types

basic types primitive types primitive types Lecture 3: XML Schema XML and Content Management 11

slide-33
SLIDE 33

Predefined types

basic types primitive types derived types primitive types Lecture 3: XML Schema XML and Content Management 11

slide-34
SLIDE 34

Basic types xsd:anyType and xsd:anySimpleType

xsd:anyType Can be used as element type: <xsd:element name="codeFragment" type="xsd:anyType"/> Any character and element content (including mixed content) allowed

Lecture 3: XML Schema XML and Content Management 12

slide-35
SLIDE 35

Basic types xsd:anyType and xsd:anySimpleType

xsd:anyType Can be used as element type: <xsd:element name="codeFragment" type="xsd:anyType"/> Any character and element content (including mixed content) allowed Default type for elements: xsd:anyType is used if an element declaration does not give any type.

Lecture 3: XML Schema XML and Content Management 12

slide-36
SLIDE 36

Basic types xsd:anyType and xsd:anySimpleType

xsd:anyType Can be used as element type: <xsd:element name="codeFragment" type="xsd:anyType"/> Any character and element content (including mixed content) allowed Default type for elements: xsd:anyType is used if an element declaration does not give any type. xsd:anySimpleType Any character content allowed Can be used as element or attribute type Default type for attributes

Lecture 3: XML Schema XML and Content Management 12

slide-37
SLIDE 37

Defining new simple types

One can define new simple types basing on predefined types and using facets (pol. aspekty).

Lecture 3: XML Schema XML and Content Management 13

slide-38
SLIDE 38

Defining new simple types

One can define new simple types basing on predefined types and using facets (pol. aspekty). Important facets: list — lists of space-separated values (like NMTOKENS), union — set-theoretic union of several simple types,

Lecture 3: XML Schema XML and Content Management 13

slide-39
SLIDE 39

Defining new simple types

One can define new simple types basing on predefined types and using facets (pol. aspekty). Important facets: list — lists of space-separated values (like NMTOKENS), union — set-theoretic union of several simple types, minInclusive, maxInclusive, minExclusive, maxExclusive — narrowing the interval of allowed numeric values, pattern — restricting set of allowed character strings with regular expressions (., a?, a+, a*, (a|b), [a-c]), (ab){2,}...) enumeration — enumerating the set of allowed values, length, minLength, maxLength — length of a string; for types defined as list applies to number of elements.

Lecture 3: XML Schema XML and Content Management 13

slide-40
SLIDE 40

Example: Defining simple type using pattern

<xsd:simpleType> element introduces new simple type. <xsd:restriction> element defines the type by specifying (or narrowing) “constraining facets” of type given in base attribute. <xsd:pattern> element specifies pattern facet. Simple type restriction example <xsd:element name="postalCode"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{2}-\d{3}"/> </xsd:restriction> </xsd:simpleType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 14

slide-41
SLIDE 41

Example: Defining simple type using pattern

<xsd:simpleType> element introduces new simple type. <xsd:restriction> element defines the type by specifying (or narrowing) “constraining facets” of type given in base attribute. <xsd:pattern> element specifies pattern facet. Simple type restriction example <xsd:element name="postalCode"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{2}-\d{3}"/> </xsd:restriction> </xsd:simpleType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 14

slide-42
SLIDE 42

Example: Defining simple type using pattern

<xsd:simpleType> element introduces new simple type. <xsd:restriction> element defines the type by specifying (or narrowing) “constraining facets” of type given in base attribute. <xsd:pattern> element specifies pattern facet. Simple type restriction example <xsd:element name="postalCode"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{2}-\d{3}"/> </xsd:restriction> </xsd:simpleType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 14

slide-43
SLIDE 43

Types: anonymous vs named

Anonymous type Defined in place of use (“inside” element or attribute) Used for one element or attribute Use it to make the schema more compact (only if a type is to be used in one place)

Lecture 3: XML Schema XML and Content Management 15

slide-44
SLIDE 44

Types: anonymous vs named

Anonymous type Defined in place of use (“inside” element or attribute) Used for one element or attribute Use it to make the schema more compact (only if a type is to be used in one place) Named type Defined globally (in <xsd:schema>) May be used for many elements or attributes Use it to make the schema more modular (whenever a type may be used is several places)

Lecture 3: XML Schema XML and Content Management 15

slide-45
SLIDE 45

Types: anonymous vs named

Anonymous type Defined in place of use (“inside” element or attribute) Used for one element or attribute Use it to make the schema more compact (only if a type is to be used in one place) Named type Defined globally (in <xsd:schema>) May be used for many elements or attributes Use it to make the schema more modular (whenever a type may be used is several places) Named type example <xsd:simpleType name="TPostalCodePL"> ... </xsd:simpleType> <xsd:element name="postalCode" type="TPostalCodePL"/>

Lecture 3: XML Schema XML and Content Management 15

slide-46
SLIDE 46

Constraining facets: intervals

DTD Not possible to restrict text content of elements Not understand numeric values of attributes

Lecture 3: XML Schema XML and Content Management 16

slide-47
SLIDE 47

Constraining facets: intervals

DTD Not possible to restrict text content of elements Not understand numeric values of attributes XML Schema <xsd:simpleType name="LottoNumber type"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="1"/> <xsd:maxInclusive value="49"/> </xsd:restriction> </xsd:simpleType>

Lecture 3: XML Schema XML and Content Management 16

slide-48
SLIDE 48

Constraining facets: intervals

DTD Not possible to restrict text content of elements Not understand numeric values of attributes XML Schema <xsd:simpleType name="LottoNumber type"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="1"/> <xsd:maxInclusive value="49"/> </xsd:restriction> </xsd:simpleType>

Lecture 3: XML Schema XML and Content Management 16

slide-49
SLIDE 49

Constraining facets: enumeration

DTD Not possible to restrict text content of elements

Lecture 3: XML Schema XML and Content Management 17

slide-50
SLIDE 50

Constraining facets: enumeration

DTD Not possible to restrict text content of elements XML Schema <xsd:simpleType name="TSex"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="female"/> <xsd:enumeration value="male"/> </xsd:restriction> </xsd:simpleType>

Lecture 3: XML Schema XML and Content Management 17

slide-51
SLIDE 51

Constraining facets: enumeration

DTD Not possible to restrict text content of elements XML Schema <xsd:simpleType name="TSex"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="female"/> <xsd:enumeration value="male"/> </xsd:restriction> </xsd:simpleType>

Lecture 3: XML Schema XML and Content Management 17

slide-52
SLIDE 52

Restrictions to restrictions

Different facets — conjunction Multiple entries for the same facet (pattern and enumeration only) — disjunction

Lecture 3: XML Schema XML and Content Management 18

slide-53
SLIDE 53

Restrictions to restrictions

Different facets — conjunction Multiple entries for the same facet (pattern and enumeration only) — disjunction A facet in a restriction can not be more general than in base type. Incorrect restriction <xsd:simpleType name="TByte9"> <xsd:restriction base="xsd:byte"> <xsd:minInclusive value="-256"/> <xsd:maxInclusive value="255"/> </xsd:restriction> </xsd:simpleType>

Lecture 3: XML Schema XML and Content Management 18

slide-54
SLIDE 54

Lists

List of simple values separated with white space Lists are simple types, sequences are complex types

Lecture 3: XML Schema XML and Content Management 19

slide-55
SLIDE 55

Lists

List of simple values separated with white space Lists are simple types, sequences are complex types List further restricted by length facet <xsd:simpleType name="TLottoNumberList"> <xsd:list itemType="LottoNumber type"/> </xsd:simpleType> <xsd:simpleType name="TLotto"> <xsd:restriction base="TLottoNumberList"> <xsd:length value="6"/> </xsd:restriction> </xsd:simpleType>

Lecture 3: XML Schema XML and Content Management 19

slide-56
SLIDE 56

Lists

List of simple values separated with white space Lists are simple types, sequences are complex types List further restricted by length facet <xsd:simpleType name="TLottoNumberList"> <xsd:list itemType="LottoNumber type"/> </xsd:simpleType> <xsd:simpleType name="TLotto"> <xsd:restriction base="TLottoNumberList"> <xsd:length value="6"/> </xsd:restriction> </xsd:simpleType> List in document <lotto>3 13 5 15 48 3</lotto>

Lecture 3: XML Schema XML and Content Management 19

slide-57
SLIDE 57

Anonymous base of derivation — lists

One can use anomous simple type as item type of a list. List defined basing on anonymous simple type <xsd:simpleType name="TLottoNumberList"> <xsd:list> <xsd:simpleType> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="1"/> <xsd:maxInclusive value="49"/> </xsd:restriction> <xsd:simpleType> </xsd:list> </xsd:simpleType>

Lecture 3: XML Schema XML and Content Management 20

slide-58
SLIDE 58

Anonymous base of derivation — restriction

Anonymous simple type can also be base to extension. Restricting an anonymous simple type (here a list) <xsd:simpleType name="TLotto"> <xsd:restriction> <xsd:simpleType> <xsd:list itemType="LottoNumber type"/> </xsd:simpleType> <xsd:length value="6"/> </xsd:restriction> </xsd:simpleType>

Lecture 3: XML Schema XML and Content Management 21

slide-59
SLIDE 59

Anonymous base of derivation — combined

Lotto defined at once, using anonymous simple types <xsd:simpleType name="TLotto"> <xsd:restriction> <xsd:simpleType> <xsd:list> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="1"/> <xsd:maxInclusive value="49"/> </xsd:restriction> </xsd:list> </xsd:simpleType> <xsd:length value="6"/> </xsd:restriction> </xsd:simpleType>

Lecture 3: XML Schema XML and Content Management 22

slide-60
SLIDE 60

Unions

Union of values sets. A value is a member of union type if and

  • nly if it is a member of any of component types.

Union of named simple types

<xsd:simpleType name="TNumericSize"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="32"/> <xsd:maxInclusive value="62"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="TLetterSize"> <xsd:restriction base="xsd:token"> <xsd:enumeration value="S"/> <xsd:enumeration value="M"/> <xsd:enumeration value="L"/> <xsd:enumeration value="XL"/> <xsd:enumeration value="XXL"/> </xsd:restriction> </xsd:simpleType>

Lecture 3: XML Schema XML and Content Management 23

slide-61
SLIDE 61

Unions

Union of values sets. A value is a member of union type if and

  • nly if it is a member of any of component types.

Union of named simple types

<xsd:simpleType name="TNumericSize"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="32"/> <xsd:maxInclusive value="62"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="TLetterSize"> <xsd:restriction base="xsd:token"> <xsd:enumeration value="S"/> <xsd:enumeration value="M"/> <xsd:enumeration value="L"/> <xsd:enumeration value="XL"/> <xsd:enumeration value="XXL"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="TSize"> <xsd:union memberTypes="TNumericSize TLetterSize"/> </xsd:simpleType>

Lecture 3: XML Schema XML and Content Management 23

slide-62
SLIDE 62

Unions

Unions can also be defined basing on anonymous types. Union of anonymous simple types

<xsd:simpleType name="TSize"> <xsd:union> <xsd:simpleType> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="34"/> <xsd:maxInclusive value="62"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType> <xsd:restriction base="xsd:token"> <xsd:enumeration value="S"/> <xsd:enumeration value="M"/> <xsd:enumeration value="L"/> <xsd:enumeration value="XL"/> <xsd:enumeration value="XXL"/> </xsd:restriction> </xsd:simpleType> </xsd:union> </xsd:simpleType>

Lecture 3: XML Schema XML and Content Management 24

slide-63
SLIDE 63

Default and fixed values

XML Schema can provide default value for attributes and elements using default or fixed attributes. Fixed value — default and the only one allowed DTD can provide default and fixed values for attributes only! Default values are used (only) by XML processors taking schema into account.

Lecture 3: XML Schema XML and Content Management 25

slide-64
SLIDE 64

Default and fixed values

XML Schema can provide default value for attributes and elements using default or fixed attributes. Fixed value — default and the only one allowed DTD can provide default and fixed values for attributes only! Default values are used (only) by XML processors taking schema into account. Fixed and default values <xsd:element name="greeting" type="xsd:string" default="Hello,"/> <xsd:attribute name="payment" type="TPaymentMeans" default="cash"/>

Lecture 3: XML Schema XML and Content Management 25

slide-65
SLIDE 65

Rules for default and fixed values

Attributes attribute occurs, no matter what is its value → the actual (document) value is used attribute does not occur → the default (schema) value is used

Lecture 3: XML Schema XML and Content Management 26

slide-66
SLIDE 66

Rules for default and fixed values

Attributes attribute occurs, no matter what is its value → the actual (document) value is used attribute does not occur → the default (schema) value is used Elements element occurs and is not empty → the actual (document) value is used element occurs and is empty → the default (schema) value is used element does not occur → the default value is not used

Lecture 3: XML Schema XML and Content Management 26

slide-67
SLIDE 67

Complex types

Application Complex types apply to elements and specify: content model attributes

Lecture 3: XML Schema XML and Content Management 27

slide-68
SLIDE 68

Complex types

Application Complex types apply to elements and specify: content model attributes Definition example <xsd:complexType name="TPerson"> <xsd:all> <xsd:element name="fname" type="xsd:string"/> <xsd:element name="lname" type="xsd:string"/> </xsd:all> <xsd:attribute name="sex" type="TSex"/> </xsd:complexType>

Lecture 3: XML Schema XML and Content Management 27

slide-69
SLIDE 69

Model groups

Content model specified with model groups

Lecture 3: XML Schema XML and Content Management 28

slide-70
SLIDE 70

Model groups

Content model specified with model groups Three kinds of model groups: <xsd:sequence> all elements (or subgroups) in the given

  • rder (comma in DTD)

Lecture 3: XML Schema XML and Content Management 28

slide-71
SLIDE 71

Model groups

Content model specified with model groups Three kinds of model groups: <xsd:sequence> all elements (or subgroups) in the given

  • rder (comma in DTD)

<xsd:choice> one from given elements or subgroups (| in DTD)

Lecture 3: XML Schema XML and Content Management 28

slide-72
SLIDE 72

Model groups

Content model specified with model groups Three kinds of model groups: <xsd:sequence> all elements (or subgroups) in the given

  • rder (comma in DTD)

<xsd:choice> one from given elements or subgroups (| in DTD) <xsd:all> all elements in any order (no corresponding construct in DTD)

Lecture 3: XML Schema XML and Content Management 28

slide-73
SLIDE 73

Model groups

Content model specified with model groups Three kinds of model groups: <xsd:sequence> all elements (or subgroups) in the given

  • rder (comma in DTD)

<xsd:choice> one from given elements or subgroups (| in DTD) <xsd:all> all elements in any order (no corresponding construct in DTD) Sequences and choices may be nested

Lecture 3: XML Schema XML and Content Management 28

slide-74
SLIDE 74

Model groups

Content model specified with model groups Three kinds of model groups: <xsd:sequence> all elements (or subgroups) in the given

  • rder (comma in DTD)

<xsd:choice> one from given elements or subgroups (| in DTD) <xsd:all> all elements in any order (no corresponding construct in DTD) Sequences and choices may be nested Elements and subgroups can be repeated

number of occurrences under control

Lecture 3: XML Schema XML and Content Management 28

slide-75
SLIDE 75

Number of occurrences

Elements can be repeated within boundaries specified with minOccurs and maxOccurs attributes. Special value unbounded for upper limit Default occurrence number interval: [1, 1] minOccurs and maxOccurs may be also used for nested groups.

Lecture 3: XML Schema XML and Content Management 29

slide-76
SLIDE 76

Controlling number of occurrences — examples

DTD <!ELEMENT person (title?, name+, surname, address*)>

Lecture 3: XML Schema XML and Content Management 30

slide-77
SLIDE 77

Controlling number of occurrences — examples

DTD <!ELEMENT person (title?, name+, surname, address*)> XML Schema <xsd:element name="person"> <xsd:complexType> <xsd:sequence> <xsd:element name="title" minOccurs="0"/> <xsd:element name="name" maxOccurs="unbounded"/> <xsd:element name="surname"/> <xsd:element name="address" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 30

slide-78
SLIDE 78

Controlling number of occurrences — examples

DTD <!ELEMENT person (title?, name+, surname, address*)> XML Schema <xsd:element name="person"> <xsd:complexType> <xsd:sequence> <xsd:element name="title" minOccurs="0"/> <xsd:element name="name" maxOccurs="unbounded"/> <xsd:element name="surname"/> <xsd:element name="address" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 30

slide-79
SLIDE 79

Controlling number of occurrences — examples

DTD <!ELEMENT person (title?, name+, surname, address*)> XML Schema <xsd:element name="person"> <xsd:complexType> <xsd:sequence> <xsd:element name="title" minOccurs="0"/> <xsd:element name="name" maxOccurs="unbounded"/> <xsd:element name="surname"/> <xsd:element name="address" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 30

slide-80
SLIDE 80

Controlling number of occurrences — examples

DTD <!ELEMENT person (title?, name+, surname, address*)> XML Schema <xsd:element name="person"> <xsd:complexType> <xsd:sequence> <xsd:element name="title" minOccurs="0"/> <xsd:element name="name" maxOccurs="unbounded"/> <xsd:element name="surname"/> <xsd:element name="address" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 30

slide-81
SLIDE 81

Number of occurrences — further examples

1 <xsd:element name="a" minOccurs="2" maxOccurs="5"/> Lecture 3: XML Schema XML and Content Management 31

slide-82
SLIDE 82

Number of occurrences — further examples

1 <xsd:element name="a" minOccurs="2" maxOccurs="5"/>

Element may occur from 2 to 5 times (any content).

Lecture 3: XML Schema XML and Content Management 31

slide-83
SLIDE 83

Number of occurrences — further examples

1 <xsd:element name="a" minOccurs="2" maxOccurs="5"/>

Element may occur from 2 to 5 times (any content).

2 <xsd:element name="b"/> Lecture 3: XML Schema XML and Content Management 31

slide-84
SLIDE 84

Number of occurrences — further examples

1 <xsd:element name="a" minOccurs="2" maxOccurs="5"/>

Element may occur from 2 to 5 times (any content).

2 <xsd:element name="b"/>

Element must occur exactly one time (any content).

Lecture 3: XML Schema XML and Content Management 31

slide-85
SLIDE 85

Number of occurrences — further examples

1 <xsd:element name="a" minOccurs="2" maxOccurs="5"/>

Element may occur from 2 to 5 times (any content).

2 <xsd:element name="b"/>

Element must occur exactly one time (any content).

3 <xsd:element name="c" minOccurs="0" fixed="24"/> Lecture 3: XML Schema XML and Content Management 31

slide-86
SLIDE 86

Number of occurrences — further examples

1 <xsd:element name="a" minOccurs="2" maxOccurs="5"/>

Element may occur from 2 to 5 times (any content).

2 <xsd:element name="b"/>

Element must occur exactly one time (any content).

3 <xsd:element name="c" minOccurs="0" fixed="24"/>

Element may occur at least one time. If it occurs as an empty element, a validating parser will fill it with content “24”. If it

  • ccurs as a non-empty element, its content must be equal to 24.

If it does not occur in a document, the parser will not insert an element.

Lecture 3: XML Schema XML and Content Management 31

slide-87
SLIDE 87

Number of occurrences — further examples

1 <xsd:element name="a" minOccurs="2" maxOccurs="5"/>

Element may occur from 2 to 5 times (any content).

2 <xsd:element name="b"/>

Element must occur exactly one time (any content).

3 <xsd:element name="c" minOccurs="0" fixed="24"/>

Element may occur at least one time. If it occurs as an empty element, a validating parser will fill it with content “24”. If it

  • ccurs as a non-empty element, its content must be equal to 24.

If it does not occur in a document, the parser will not insert an element.

4 <xsd:element name="d" maxOccurs="unbounded"

default="24"/>

Lecture 3: XML Schema XML and Content Management 31

slide-88
SLIDE 88

Number of occurrences — further examples

1 <xsd:element name="a" minOccurs="2" maxOccurs="5"/>

Element may occur from 2 to 5 times (any content).

2 <xsd:element name="b"/>

Element must occur exactly one time (any content).

3 <xsd:element name="c" minOccurs="0" fixed="24"/>

Element may occur at least one time. If it occurs as an empty element, a validating parser will fill it with content “24”. If it

  • ccurs as a non-empty element, its content must be equal to 24.

If it does not occur in a document, the parser will not insert an element.

4 <xsd:element name="d" maxOccurs="unbounded"

default="24"/> Element must occur exactly once. If it occurs as an empty element, the (validating) parser will fill it with content “24”. If it

  • ccurs as a non-empty element, the document value is used.

Lecture 3: XML Schema XML and Content Management 31

slide-89
SLIDE 89

Choice

DTD <!ELEMENT vehicle (train | aeroplane | car)>

Lecture 3: XML Schema XML and Content Management 32

slide-90
SLIDE 90

Choice

DTD <!ELEMENT vehicle (train | aeroplane | car)> XML Schema <xsd:element name="vehicle"> <xsd:complexType> <xsd:choice> <xsd:element name="train"/> <xsd:element name="aeroplane"/> <xsd:element name="car"/> </xsd:choice> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 32

slide-91
SLIDE 91

Choice

DTD <!ELEMENT vehicle (train | aeroplane | car)> XML Schema <xsd:element name="vehicle"> <xsd:complexType> <xsd:choice> <xsd:element name="train"/> <xsd:element name="aeroplane"/> <xsd:element name="car"/> </xsd:choice> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 32

slide-92
SLIDE 92

Free combination of elements

Without occurrence control

DTD <!ELEMENT vehicle (train | aeroplane | car)*>

Lecture 3: XML Schema XML and Content Management 33

slide-93
SLIDE 93

Free combination of elements

Without occurrence control

DTD <!ELEMENT vehicle (train | aeroplane | car)*> XML Schema <xsd:element name="vehicle"> <xsd:complexType> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element name="train"/> <xsd:element name="aeroplane"/> <xsd:element name="car"/> </xsd:choice> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 33

slide-94
SLIDE 94

Free combination of elements

Without occurrence control

DTD <!ELEMENT vehicle (train | aeroplane | car)*> XML Schema <xsd:element name="vehicle"> <xsd:complexType> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element name="train"/> <xsd:element name="aeroplane"/> <xsd:element name="car"/> </xsd:choice> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 33

slide-95
SLIDE 95

All elements in any order

DTD No such construct!

Lecture 3: XML Schema XML and Content Management 34

slide-96
SLIDE 96

All elements in any order

DTD No such construct! XML Schema <xsd:element name="book"> <xsd:complexType> <xsd:all> <xsd:element name="title"/> <xsd:element name="author"/> <xsd:element name="publisher" minOccurs="0"/> </xsd:all> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 34

slide-97
SLIDE 97

All elements in any order

DTD No such construct! XML Schema <xsd:element name="book"> <xsd:complexType> <xsd:all> <xsd:element name="title"/> <xsd:element name="author"/> <xsd:element name="publisher" minOccurs="0"/> </xsd:all> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 34

slide-98
SLIDE 98

All elements in any order

Restrictions: cannot contain other groups, only elements, cannot be nested in other group, elements cannot occur more than once, maxOccurs ≤ 1.

Lecture 3: XML Schema XML and Content Management 34

slide-99
SLIDE 99

Empty content

DTD <!ELEMENT hr EMPTY>

Lecture 3: XML Schema XML and Content Management 35

slide-100
SLIDE 100

Empty content

DTD <!ELEMENT hr EMPTY> XML Schema <xsd:element name="hr"> <xsd:complexType/> </xsd:element>

Lecture 3: XML Schema XML and Content Management 35

slide-101
SLIDE 101

Any content

Usually used to insert content from another namespace (e.g. nested XHTML, MathML, or SVG)

Lecture 3: XML Schema XML and Content Management 36

slide-102
SLIDE 102

Any content

Usually used to insert content from another namespace (e.g. nested XHTML, MathML, or SVG) For such purposes, XML Schema provides element <xsd:any> with two special attributes: namespace — namespace of allowed element; special values #any and #other processContents — should the inserted content be validated skip no validation lax validation if definition (schema) of a particular element available strict mandatory validation (default value)

Lecture 3: XML Schema XML and Content Management 36

slide-103
SLIDE 103

Any content

DTD <!ELEMENT nestedXHTML ANY>

Lecture 3: XML Schema XML and Content Management 37

slide-104
SLIDE 104

Any content

DTD <!ELEMENT nestedXHTML ANY> XML Schema <xsd:element name="nestedXHTML"> <xsd:complexType> <xsd:sequence> <xsd:any namespace="http://www.w3.org/1999/xhtml" maxOccurs="unbounded" processContents="skip"/> </xsd:sequence> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 37

slide-105
SLIDE 105

Any attributes

XML Schema provides means to allow occurrence of any attributes (from a given namespace). It would be useful e.g. to enable XLink.

Lecture 3: XML Schema XML and Content Management 38

slide-106
SLIDE 106

Any attributes

XML Schema provides means to allow occurrence of any attributes (from a given namespace). It would be useful e.g. to enable XLink. Example <xsd:element name="person"> <xsd:complexType> <xsd:sequence> <xsd:element name="website"> <xsd:complexType> <xsd:anyAttribute namespace="http://www.w3.org/1999/xlink"/> </xsd:complexType> </xsd:sequence> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 38

slide-107
SLIDE 107

Defining attributes

Occurrence of attributes is controlled by attribute use, which possible values are: required must occur

  • ptional may occur (default value)

prohibited must not occur (useful in type restrictions)

Lecture 3: XML Schema XML and Content Management 39

slide-108
SLIDE 108

Defining attributes: DTD vs XML Schema

DTD <!ELEMENT person EMPTY> <!ATTLIST person pesel CDATA #REQUIRED nip CDATA #IMPLIED> species CDATA #FIXED "Homo sapiens">

Lecture 3: XML Schema XML and Content Management 40

slide-109
SLIDE 109

Defining attributes: DTD vs XML Schema

DTD <!ELEMENT person EMPTY> <!ATTLIST person pesel CDATA #REQUIRED nip CDATA #IMPLIED> species CDATA #FIXED "Homo sapiens"> XML Schema <xsd:element name="person"> <xsd:complexType> <xsd:attribute name="pesel" use="required"/> <xsd:attribute name="nip"/> <xsd:attribute name="species" fixed="Homo sapiens"/> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 40

slide-110
SLIDE 110

Defining attributes: DTD vs XML Schema

DTD <!ELEMENT person EMPTY> <!ATTLIST person pesel CDATA #REQUIRED nip CDATA #IMPLIED> species CDATA #FIXED "Homo sapiens"> XML Schema <xsd:element name="person"> <xsd:complexType> <xsd:attribute name="pesel" use="required"/> <xsd:attribute name="nip"/> <xsd:attribute name="species" fixed="Homo sapiens"/> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 40

slide-111
SLIDE 111

Defining attributes: DTD vs XML Schema

DTD <!ELEMENT person EMPTY> <!ATTLIST person pesel CDATA #REQUIRED nip CDATA #IMPLIED> species CDATA #FIXED "Homo sapiens"> XML Schema <xsd:element name="person"> <xsd:complexType> <xsd:attribute name="pesel" use="required"/> <xsd:attribute name="nip"/> <xsd:attribute name="species" fixed="Homo sapiens"/> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 40

slide-112
SLIDE 112

Defining attributes: DTD vs XML Schema

DTD <!ELEMENT person EMPTY> <!ATTLIST person pesel CDATA #REQUIRED nip CDATA #IMPLIED> species CDATA #FIXED "Homo sapiens"> XML Schema <xsd:element name="person"> <xsd:complexType> <xsd:attribute name="pesel" use="required"/> <xsd:attribute name="nip"/> <xsd:attribute name="species" fixed="Homo sapiens"/> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 40

slide-113
SLIDE 113

Defining attributes: DTD vs XML Schema ctd.

DTD <!ATTLIST person sex (woman | man) #REQUIRED>

Lecture 3: XML Schema XML and Content Management 41

slide-114
SLIDE 114

Defining attributes: DTD vs XML Schema ctd.

DTD <!ATTLIST person sex (woman | man) #REQUIRED> XML Schema <xsd:element name="person"> <xsd:complexType> <xsd:attribute name="sex" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:NMTOKEN"> <xsd:enumeration value="woman"/> <xsd:enumeration value="man"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 41

slide-115
SLIDE 115

Defining attributes: DTD vs XML Schema ctd.

DTD <!ATTLIST person sex (woman | man) #REQUIRED> XML Schema <xsd:element name="person"> <xsd:complexType> <xsd:attribute name="sex" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:NMTOKEN"> <xsd:enumeration value="woman"/> <xsd:enumeration value="man"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 41

slide-116
SLIDE 116

Defining attributes — further examples

1 <xsd:attribute name="a" use="required"/> Lecture 3: XML Schema XML and Content Management 42

slide-117
SLIDE 117

Defining attributes — further examples

1 <xsd:attribute name="a" use="required"/>

Attribute must occur, it may take any value.

Lecture 3: XML Schema XML and Content Management 42

slide-118
SLIDE 118

Defining attributes — further examples

1 <xsd:attribute name="a" use="required"/>

Attribute must occur, it may take any value.

2 <xsd:attribute name="b"/> Lecture 3: XML Schema XML and Content Management 42

slide-119
SLIDE 119

Defining attributes — further examples

1 <xsd:attribute name="a" use="required"/>

Attribute must occur, it may take any value.

2 <xsd:attribute name="b"/>

Attribute is optional, it may take any value.

Lecture 3: XML Schema XML and Content Management 42

slide-120
SLIDE 120

Defining attributes — further examples

1 <xsd:attribute name="a" use="required"/>

Attribute must occur, it may take any value.

2 <xsd:attribute name="b"/>

Attribute is optional, it may take any value.

3 <xsd:attribute name="c" fixed="24"/> Lecture 3: XML Schema XML and Content Management 42

slide-121
SLIDE 121

Defining attributes — further examples

1 <xsd:attribute name="a" use="required"/>

Attribute must occur, it may take any value.

2 <xsd:attribute name="b"/>

Attribute is optional, it may take any value.

3 <xsd:attribute name="c" fixed="24"/>

Attribute is optional. If not given in document it take the value of 24; If given in document, it must have the value of 24.

Lecture 3: XML Schema XML and Content Management 42

slide-122
SLIDE 122

Defining attributes — further examples

1 <xsd:attribute name="a" use="required"/>

Attribute must occur, it may take any value.

2 <xsd:attribute name="b"/>

Attribute is optional, it may take any value.

3 <xsd:attribute name="c" fixed="24"/>

Attribute is optional. If not given in document it take the value of 24; If given in document, it must have the value of 24.

4 <xsd:attribute name="d" default="24"/> Lecture 3: XML Schema XML and Content Management 42

slide-123
SLIDE 123

Defining attributes — further examples

1 <xsd:attribute name="a" use="required"/>

Attribute must occur, it may take any value.

2 <xsd:attribute name="b"/>

Attribute is optional, it may take any value.

3 <xsd:attribute name="c" fixed="24"/>

Attribute is optional. If not given in document it take the value of 24; If given in document, it must have the value of 24.

4 <xsd:attribute name="d" default="24"/>

Attribute is optional. If not given in document it take the value of 24; If given in document, it takes the actual value from document (even if it is empty string).

Lecture 3: XML Schema XML and Content Management 42

slide-124
SLIDE 124

Mixed content model

DTD <!ELEMENT text (#PCDATA | strong | link)*>

Lecture 3: XML Schema XML and Content Management 43

slide-125
SLIDE 125

Mixed content model

DTD <!ELEMENT text (#PCDATA | strong | link)*> XML Schema <xsd:element name="text"> <xsd:complexType mixed="true"> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element name="strong"/> <xsd:element name="link"/> </xsd:choice> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 43

slide-126
SLIDE 126

Mixed content model

DTD <!ELEMENT text (#PCDATA | strong | link)*> XML Schema <xsd:element name="text"> <xsd:complexType mixed="true"> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element name="strong"/> <xsd:element name="link"/> </xsd:choice> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 43

slide-127
SLIDE 127

Mixed content model — XML Schema capabilities

XML Schema is capable to specify mixed models more precisely than DTD. In particular, it is possible to specify:

  • rder of elements (using <xsd:sequence>),

number of occurrence (using minOccurs and maxOccurs). Generally, a normal element content model is constructed and then arbitrary text is permitted to occur between subelements.

Lecture 3: XML Schema XML and Content Management 44

slide-128
SLIDE 128

Global and local definitions

Elements and attributes defined directly in <xsd:schema> children are global; the rest are local.

Lecture 3: XML Schema XML and Content Management 45

slide-129
SLIDE 129

Global and local definitions

Elements and attributes defined directly in <xsd:schema> children are global; the rest are local. One can use globally defined components in any place, using ref attribute: <xsd:element name="footnote"> <xsd:complexType> <xsd:attribute name="nr"/> </xsd:complexType> </xsd:element> <xsd:element name="title"> <xsd:complexType mixed="true"> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element ref="footnote"/> </xsd:choice> </xsd:complexType> </xsd:element>

Lecture 3: XML Schema XML and Content Management 45

slide-130
SLIDE 130

Global vs local

Global definitions allow author to modularize schema and avoid code duplication. Global element can always be used as root element of a document.

Use named groups or named complex types instead of global elements to prevent elements from being root elements.

Local definitions allows author to define elements with the same name and different types. For some namespace-related reasons, in typical applications, attributes should be defined locally.

Lecture 3: XML Schema XML and Content Management 46

slide-131
SLIDE 131

Note: Global and local definitions in DTD

In DTD All elemets are global. Element types are anonymous (except special types ANY and EMPTY). All attributes are local to their elements. Attribute types are predefined or anonymous.

Lecture 3: XML Schema XML and Content Management 47

slide-132
SLIDE 132

Element groups

Named, global model groups

DTD <!ENTITY % formatting ’b | i | u’>

Lecture 3: XML Schema XML and Content Management 48

slide-133
SLIDE 133

Element groups

Named, global model groups

DTD <!ENTITY % formatting ’b | i | u’> XML Schema <xsd:group name="formatting"> <xsd:choice> <xsd:element ref="b"/> <xsd:element ref="i"/> <xsd:element ref="u"/> </xsd:choice> </xsd:group>

Lecture 3: XML Schema XML and Content Management 48

slide-134
SLIDE 134

Element groups

Named, global model groups

DTD <!ENTITY % formatting ’b | i | u’> XML Schema <xsd:group name="formatting"> <xsd:choice> <xsd:element ref="b"/> <xsd:element ref="i"/> <xsd:element ref="u"/> </xsd:choice> </xsd:group>

Lecture 3: XML Schema XML and Content Management 48

slide-135
SLIDE 135

Element groups

Named, global model groups

DTD <!ENTITY % formatting ’b | i | u’> XML Schema <xsd:group name="formatting"> <xsd:choice> <xsd:element ref="b"/> <xsd:element ref="i"/> <xsd:element ref="u"/> </xsd:choice> </xsd:group>

Lecture 3: XML Schema XML and Content Management 48

slide-136
SLIDE 136

DTD <!ENTITY % time-attrs ’valid-from CDATA #IMPLIED valid-to CDATA #IMPLIED’> <!ENTITY % common-attrs ’status CDATA #IMPLIED %time-attrs;’>

Lecture 3: XML Schema XML and Content Management 49

slide-137
SLIDE 137

DTD <!ENTITY % time-attrs ’valid-from CDATA #IMPLIED valid-to CDATA #IMPLIED’> <!ENTITY % common-attrs ’status CDATA #IMPLIED %time-attrs;’> XML Schema <xsd:attributeGroup name="time-attrs"> <xsd:attribute name="valid-from"/> <xsd:attribute name="valid-to"/> </xsd:attributeGroup <xsd:attributeGroup name="common-attrs"> <xsd:attribute name="status"/> <xsd:attributeGroup ref="time-attrs"/> </xsd:attributeGroup

Lecture 3: XML Schema XML and Content Management 49

slide-138
SLIDE 138

DTD <!ENTITY % time-attrs ’valid-from CDATA #IMPLIED valid-to CDATA #IMPLIED’> <!ENTITY % common-attrs ’status CDATA #IMPLIED %time-attrs;’> XML Schema <xsd:attributeGroup name="time-attrs"> <xsd:attribute name="valid-from"/> <xsd:attribute name="valid-to"/> </xsd:attributeGroup <xsd:attributeGroup name="common-attrs"> <xsd:attribute name="status"/> <xsd:attributeGroup ref="time-attrs"/> </xsd:attributeGroup

Lecture 3: XML Schema XML and Content Management 49

slide-139
SLIDE 139

Constraints on uniqueness and references

Two mechanisms:

Lecture 3: XML Schema XML and Content Management 50

slide-140
SLIDE 140

Constraints on uniqueness and references

Two mechanisms:

1 special attribute types xsd:ID and xsd:IDREF — like in DTD Lecture 3: XML Schema XML and Content Management 50

slide-141
SLIDE 141

Constraints on uniqueness and references

Two mechanisms:

1 special attribute types xsd:ID and xsd:IDREF — like in DTD 2 XML Schema identity constraints: Lecture 3: XML Schema XML and Content Management 50

slide-142
SLIDE 142

Constraints on uniqueness and references

Two mechanisms:

1 special attribute types xsd:ID and xsd:IDREF — like in DTD 2 XML Schema identity constraints:

<xsd:unique>

Lecture 3: XML Schema XML and Content Management 50

slide-143
SLIDE 143

Constraints on uniqueness and references

Two mechanisms:

1 special attribute types xsd:ID and xsd:IDREF — like in DTD 2 XML Schema identity constraints:

<xsd:unique> <xsd:key>

Lecture 3: XML Schema XML and Content Management 50

slide-144
SLIDE 144

Constraints on uniqueness and references

Two mechanisms:

1 special attribute types xsd:ID and xsd:IDREF — like in DTD 2 XML Schema identity constraints:

<xsd:unique> <xsd:key> <xsd:keyref>

Lecture 3: XML Schema XML and Content Management 50

slide-145
SLIDE 145

Identity constraints: definitions

Three components of identity constraint:

Lecture 3: XML Schema XML and Content Management 51

slide-146
SLIDE 146

Identity constraints: definitions

Three components of identity constraint: scope element the constraint is defined within

Lecture 3: XML Schema XML and Content Management 51

slide-147
SLIDE 147

Identity constraints: definitions

Three components of identity constraint: scope element the constraint is defined within selector points elements the constraint applies to

Lecture 3: XML Schema XML and Content Management 51

slide-148
SLIDE 148

Identity constraints: definitions

Three components of identity constraint: scope element the constraint is defined within selector points elements the constraint applies to fields points elements and attributes which values must be unique (for nodes selected by selector, within a given scope)

Lecture 3: XML Schema XML and Content Management 51

slide-149
SLIDE 149

Identity constraints: definitions

Three components of identity constraint: scope element the constraint is defined within selector points elements the constraint applies to fields points elements and attributes which values must be unique (for nodes selected by selector, within a given scope) Selectors and fields specified with (a subset of) XPath language

Lecture 3: XML Schema XML and Content Management 51

slide-150
SLIDE 150

Become an XPath guru in 30 seconds

XPath is a language for addressing elements and attributes within a document tree.

Lecture 3: XML Schema XML and Content Management 52

slide-151
SLIDE 151

Become an XPath guru in 30 seconds

XPath is a language for addressing elements and attributes within a document tree. Syntax — almost like in file system:

Lecture 3: XML Schema XML and Content Management 52

slide-152
SLIDE 152

Become an XPath guru in 30 seconds

XPath is a language for addressing elements and attributes within a document tree. Syntax — almost like in file system: book/chapter — choose all chapter subelements from all book elements being children of current element,

Lecture 3: XML Schema XML and Content Management 52

slide-153
SLIDE 153

Become an XPath guru in 30 seconds

XPath is a language for addressing elements and attributes within a document tree. Syntax — almost like in file system: book/chapter — choose all chapter subelements from all book elements being children of current element, */chapter — chapter subelements of all children of current element,

Lecture 3: XML Schema XML and Content Management 52

slide-154
SLIDE 154

Become an XPath guru in 30 seconds

XPath is a language for addressing elements and attributes within a document tree. Syntax — almost like in file system: book/chapter — choose all chapter subelements from all book elements being children of current element, */chapter — chapter subelements of all children of current element, @nr — nr attribute of current element.

Lecture 3: XML Schema XML and Content Management 52

slide-155
SLIDE 155

Become an XPath guru in 30 seconds

XPath is a language for addressing elements and attributes within a document tree. Syntax — almost like in file system: book/chapter — choose all chapter subelements from all book elements being children of current element, */chapter — chapter subelements of all children of current element, @nr — nr attribute of current element. Namespaces complicate things a bit,

Lecture 3: XML Schema XML and Content Management 52

slide-156
SLIDE 156

Become an XPath guru in 30 seconds

XPath is a language for addressing elements and attributes within a document tree. Syntax — almost like in file system: book/chapter — choose all chapter subelements from all book elements being children of current element, */chapter — chapter subelements of all children of current element, @nr — nr attribute of current element. Namespaces complicate things a bit, In fact, XPath is more complex — lectures 6–8.

Lecture 3: XML Schema XML and Content Management 52

slide-157
SLIDE 157

Identity constraints: unique identifier

<accounts> <orders> <order> <number>125</number> ... </order> <order> <number>665</number> ... </order> </orders> <invoices> ... </invoices> </accounts>

Lecture 3: XML Schema XML and Content Management 53

slide-158
SLIDE 158

Identity constraints: unique identifier

<accounts> <orders> <order> <number>125</number> ... </order> <order> <number>665</number> ... </order> </orders> <invoices> ... </invoices> </accounts> Order numbers unique within document scope. They will be referred from invoices. <xsd:key name="orderId"> <xsd:selector xpath="orders /order"/> <xsd:field xpath="number"/> </xsd:key>

Lecture 3: XML Schema XML and Content Management 53

slide-159
SLIDE 159

Identity constraints: unique identifier

<accounts> <orders> <order> <number>125</number> ... </order> <order> <number>665</number> ... </order> </orders> <invoices> ... </invoices> </accounts> Order numbers unique within document scope. They will be referred from invoices. <xsd:key name="orderId"> <xsd:selector xpath="orders /order"/> <xsd:field xpath="number"/> </xsd:key>

Lecture 3: XML Schema XML and Content Management 53

slide-160
SLIDE 160

Identity constraints: unique identifier

<accounts> <orders> <order> <number>125</number> ... </order> <order> <number>665</number> ... </order> </orders> <invoices> ... </invoices> </accounts> Order numbers unique within document scope. They will be referred from invoices. <xsd:key name="orderId"> <xsd:selector xpath="orders /order"/> <xsd:field xpath="number"/> </xsd:key>

Lecture 3: XML Schema XML and Content Management 53

slide-161
SLIDE 161

Identity constraints: unique numbers and references

<accounts> ... <invoices> <invoice nr="123"> <year>2007</year> <order-ref nr="125"/>... </invoice> <invoice nr="123"> <year>2006</year> <order-ref nr="665"/>... </invoice> </invoices> </accounts>

Lecture 3: XML Schema XML and Content Management 54

slide-162
SLIDE 162

Identity constraints: unique numbers and references

<accounts> ... <invoices> <invoice nr="123"> <year>2007</year> <order-ref nr="125"/>... </invoice> <invoice nr="123"> <year>2006</year> <order-ref nr="665"/>... </invoice> </invoices> </accounts> Uniqueness of invoice numbers within a given year, in document scope: <xsd:unique name="uniqueInvoiceNumber"> <xsd:selector xpath="invoices /invoice"/> <xsd:field xpath="@nr"/> <xsd:field xpath="year"/> </xsd:unique>

Lecture 3: XML Schema XML and Content Management 54

slide-163
SLIDE 163

Identity constraints: unique numbers and references

<accounts> ... <invoices> <invoice nr="123"> <year>2007</year> <order-ref nr="125"/>... </invoice> <invoice nr="123"> <year>2006</year> <order-ref nr="665"/>... </invoice> </invoices> </accounts> Uniqueness of invoice numbers within a given year, in document scope: <xsd:unique name="uniqueInvoiceNumber"> <xsd:selector xpath="invoices /invoice"/> <xsd:field xpath="@nr"/> <xsd:field xpath="year"/> </xsd:unique>

Lecture 3: XML Schema XML and Content Management 54

slide-164
SLIDE 164

Identity constraints: unique numbers and references

<accounts> ... <invoices> <invoice nr="123"> <year>2007</year> <order-ref nr="125"/>... </invoice> <invoice nr="123"> <year>2006</year> <order-ref nr="665"/>... </invoice> </invoices> </accounts> Uniqueness of invoice numbers within a given year, in document scope: <xsd:unique name="uniqueInvoiceNumber"> <xsd:selector xpath="invoices /invoice"/> <xsd:field xpath="@nr"/> <xsd:field xpath="year"/> </xsd:unique>

Lecture 3: XML Schema XML and Content Management 54

slide-165
SLIDE 165

Identity constraints: unique numbers and references

<accounts> ... <invoices> <invoice nr="123"> <year>2007</year> <order-ref nr="125"/>... </invoice> <invoice nr="123"> <year>2006</year> <order-ref nr="665"/>... </invoice> </invoices> </accounts> Referencing order numbers: <xsd:keyref name="orderRef" refer="orderId"> <xsd:selector xpath="invoices /invoice /order-ref"/> <xsd:field xpath="@nr"/> </xsd:keyref>

Lecture 3: XML Schema XML and Content Management 54

slide-166
SLIDE 166

Identity constraints: unique numbers and references

<accounts> ... <invoices> <invoice nr="123"> <year>2007</year> <order-ref nr="125"/>... </invoice> <invoice nr="123"> <year>2006</year> <order-ref nr="665"/>... </invoice> </invoices> </accounts> Referencing order numbers: <xsd:keyref name="orderRef" refer="orderId"> <xsd:selector xpath="invoices /invoice /order-ref"/> <xsd:field xpath="@nr"/> </xsd:keyref>

Lecture 3: XML Schema XML and Content Management 54

slide-167
SLIDE 167

Identity constraints: unique numbers and references

<accounts> ... <invoices> <invoice nr="123"> <year>2007</year> <order-ref nr="125"/>... </invoice> <invoice nr="123"> <year>2006</year> <order-ref nr="665"/>... </invoice> </invoices> </accounts> Referencing order numbers: <xsd:keyref name="orderRef" refer="orderId"> <xsd:selector xpath="invoices /invoice /order-ref"/> <xsd:field xpath="@nr"/> </xsd:keyref>

Lecture 3: XML Schema XML and Content Management 54

slide-168
SLIDE 168

Identity constraints: all together

<xsd:element name="accounts" type="TAccounts"> <xsd:unique name="uniqueInvoiceNumber"> <xsd:selector xpath="invoices/invoice"/> <xsd:field xpath="@nr"/> <xsd:field xpath="year"/> </xsd:unique> <xsd:keyref name="orderRef" refer="orderId"> <xsd:selector xpath="invoices/invoice/order-ref"/> <xsd:field xpath="@nr"/> </xsd:keyref> <xsd:key name="orderId"> <xsd:selector xpath="orders/order"/> <xsd:field xpath="number"/> </xsd:key> </xsd:element>

Lecture 3: XML Schema XML and Content Management 55