XML Schema Part 2 More on Schema Types

  • Slides: 47
Download presentation
XML Schema – Part 2 • • More on Schema Types & Derivation Abstact

XML Schema – Part 2 • • More on Schema Types & Derivation Abstact types & type substitution Uniqueness & Keys Additional schema mechanisms - include & import - open content • Comparison with DTD & other tech. 1

Content Types • The type hierarchy first branches into two groups: simple types and

Content Types • The type hierarchy first branches into two groups: simple types and complex types. • Complex types are divided into two groups: those with simple content and those with complex content. • While both forms of complex type allow attributes, only those with complex content allow child elements; those with simple content only allow character content. 2

<xsd: complex. Type name="Publication"> <xsd: sequence> <xsd: element name="Title" type="xsd: string" max. Occurs="unbounded"/> <xsd:

<xsd: complex. Type name="Publication"> <xsd: sequence> <xsd: element name="Title" type="xsd: string" max. Occurs="unbounded"/> <xsd: element name="Author"type="xsd: string"max. Occurs="unbounded"/> <xsd: element name="Date" type="xsd: g. Year"/> </xsd: sequence> </xsd: complex. Type > <xsd: complex. Type name="Book. Publication"> <xsd: complex. Content> <xsd: extension base="Publication"> <xsd: sequence> <xsd: element name="ISBN" type="xsd: string"/> <xsd: element name="Publisher" type="xsd: string"/> </xsd: sequence> </xsd: extension> </xsd: complex. Content> </xsd: complex. Type > <xsd: element name="Book" type="Book. Publication" max. Occurs="unbounded"/> 3

Content Type • As we saw there are four kinds of derivation: restriction, extension,

Content Type • As we saw there are four kinds of derivation: restriction, extension, list, and union. • All types derive, directly or indirectly, from the root type. The root type is any. Type. • The default syntax for complex types is complex content that restricts any. Type. 4

Schema Types 5

Schema Types 5

any. Type • The any. Type is the base type for all types which

any. Type • The any. Type is the base type for all types which do not specify a value for the base attribute. It is the base type for all elements which do not specify a type. – Example: <xsd: element name="foo"/> <xsd: complex. Type name="xsd: any. Type" mixed="true"> <xsd: sequence> <xsd: any min. Occurs="0" max. Occurs="unbounded"/> </xsd: sequence> <xsd: any. Attribute/> </xsd: complex. Type> This is the definition of the any. Type. 6

Complex Type <xsd: element name="book"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="title" type="xsd:

Complex Type <xsd: element name="book"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="title" type="xsd: string"/> <xsd: element name=“author" type="xsd: date“/> </xsd: sequence> </xsd: complex. Type> </xsd: element> <xsd: element name="book"> <complex. Content> <restriction base="any. Type"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="title" type="xsd: string"/> <xsd: element name=“author" type="xsd: date“/> </xsd: sequence> </xsd: complex. Type> </restriction> </complex. Content> </xsd: element> 7

Empty Element Your first inclination might be to associate the empty element with a

Empty Element Your first inclination might be to associate the empty element with a simple type. But that won't work since simple types allow data content. So it must be a complex type. The, ask yourself the next question. Will it allow element children? No. We need a <complex. Type> with <simple. Content>, right? Wrong. Complex types with simple content also allow data content, and we want an empty element. That leaves us with <complex. Type> with <complex. Content>, which ensures that there will not be any data content in the element. But we don't want child elements, either, and a complex type with complex content allows child elements. The key is that it doesn't require them. Simply leave the content model out of the type definition: <complex. Type name=“empty. Element"> <complex. Content> <restriction base="any. Type"> </restriction> </complex. Content> </complex. Type> 8

Empty Element DTD: Schema: <!ELEMENT image EMPTY> <!ATTLIST image href CDATA #REQUIRED> <xsd: element

Empty Element DTD: Schema: <!ELEMENT image EMPTY> <!ATTLIST image href CDATA #REQUIRED> <xsd: element name="image"> <xsd: complex. Type> <xsd: attribute name="href" type="xsd: any. URI" use="required"/> </xsd: complex. Type> </xsd: element> Instance doc (snippet): <image href="http: //www. xfront. com/In. Subway. gif"/> 9

Type Substitutability • As we saw earlier, substitution. Group gives us "element substitution", i.

Type Substitutability • As we saw earlier, substitution. Group gives us "element substitution", i. e. , the ability to substitute one element for another. Now we will see how to achieve "type substitution", i. e. , the ability to substitute an element’s content with another content. • Here’s the principle of type substitutability: A base type can be substituted by any derived type. – Example. Suppose that Book. Type is derived from Publication. Type. If we declare an element, Publication, to be of type Publication. Type (the base type) then in the instance document Publication's content can be either a Publication. Type or a 10 Book. Type.

<? xml version="1. 0"? > <xsd: schema xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" target.

<? xml version="1. 0"? > <xsd: schema xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" target. Namespace="http: //www. books. org" xmlns="http: //www. books. org" element. Form. Default="unqualified"> <xsd: complex. Type name="Publication. Type"> <xsd: sequence> <xsd: element name="Title" type="xsd: string"/> <xsd: element name="Author" type="xsd: string" max. Occurs="unbounded"/> <xsd: element name="Date" type="xsd: year"/> </xsd: sequence> </xsd: complex. Type> <xsd: complex. Type name="Book. Type"> <xsd: complex. Content> <xsd: extension base="Publication. Type"> <xsd: sequence> <xsd: element name="ISBN" type="xsd: string"/> <xsd: element name="Publisher" type="xsd: string"/> </xsd: sequence> </xsd: extension> </xsd: complex. Content> </xsd: complex. Type> <xsd: element name="Book. Store"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="Publication" max. Occurs="unbounded" type="Publication. Type"/> </xsd: sequence> </xsd: complex. Type> </xsd: element> </xsd: schema> Publication. Type is the base type Book. Type extends Publication. Type Publication is of type Publication. Type (the base type) 11

<? xml version="1. 0"? > <bk: Book. Store xmlns: bk="http: //www. books. org" xmlns:

<? xml version="1. 0"? > <bk: Book. Store xmlns: bk="http: //www. books. org" xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance" xsi: schema. Location= "http: //www. books. org Book. Store. xsd"> <Publication> <Title>Staying Young Forever</Title> <Author>Karin Granstrom Jordan, M. D. </Author> <Date>1999</Date> </Publication> <Publication xsi: type="bk: Book. Type"> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0 -440 -34319 -4</ISBN> <Publisher>Dell Publishing Co. </Publisher> </Publication> <Publication xsi: type="bk: Book. Type"> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0 -06 -064831 -7</ISBN> <Publisher>Harper & Row</Publisher> </Publication> </bk: Book. Store> This Publication’s content model is Publication. Type This Publication’s content model is Book. Type Book. Store. xml 12

Abstract Elements • The head element must be global. • Same type as the

Abstract Elements • The head element must be global. • Same type as the head or derived within substitution group • Used in place of the head element. • • Generic head element, Shouldn't be used directly, but in one of its derived forms. Declare head as abstract. Analogous to abstract classes in O/O. <xs: element name="name-elt" type="xs: string" abstract="true"/> <xs: element name="name" type="xs: string" substitution. Group="name-elt"/> <xs: element name="surname" type="xs: string" substitution. Group="name-elt"/> This example defines name-elt as an abstract element that should be replaced either by name or surname everywhere it is referenced. 13

Abstract complex. Type • You can declare a complex. Type to be abstract –

Abstract complex. Type • You can declare a complex. Type to be abstract – Example. <xsd: complex. Type name="Publication. Type" abstract="true"/> • An abstract complex. Type is a template/placeholder type: – If an element is declared to be a type that is abstract then in an XML instance document the content model of that element may not be that of the abstract type. • Example. An element declared to be of type Publication. Type (shown above) may not have that type’s content model. – However, complex. Type’s that are derived from the abstract type may substitute for the abstract type. 14

<xsd: complex. Type name="Publication. Type" abstract="true"> <xsd: sequence> <xsd: element name="Title" type="xsd: string" max.

<xsd: complex. Type name="Publication. Type" abstract="true"> <xsd: sequence> <xsd: element name="Title" type="xsd: string" max. Occurs="unbounded"/> <xsd: element name="Author" type="xsd: string" max. Occurs="unbounded"/> <xsd: element name="Date" type="xsd: year"/> </xsd: sequence> </xsd: complex. Type> <xsd: complex. Type name="Book. Type"> <xsd: complex. Content> <xsd: extension base="Publication. Type"> <xsd: sequence> <xsd: element name="ISBN" type="xsd: string"/> <xsd: element name="Publisher" type="xsd: string"/> </xsd: sequence> </xsd: extension> </xsd: complex. Content> </xsd: complex. Type> <xsd: complex. Type name="Single. Author. Publication"> <xsd: complex. Content> <xsd: restriction base="Publication. Type"> <xsd: sequence> <xsd: element name="Title" type="xsd: string" max. Occurs="unbounded"/> <xsd: element name="Author" type="xsd: string"/> <xsd: element name="Date" type="xsd: year"/> </xsd: sequence> </xsd: restriction> </xsd: complex. Content> </xsd: complex. Type> <xsd: element name="Book. Store"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="Book" max. Occurs="unbounded" type="Publication. Type"/> </xsd: sequence> </xsd: complex. Type> </xsd: element> Note that Publication. Type is declared abstract. Book derives from Publication. Type. By default abstract="false". Thus, this type can substitute for the Publication. Type. 15

<? xml version="1. 0"? > <Book. Store xmlns="http: //www. books. org" xmlns: xsi="http: //www.

<? xml version="1. 0"? > <Book. Store xmlns="http: //www. books. org" xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance" xsi: schema. Location= "http: //www. books. org Book. Store. xsd"> <Book xsi: type="Book. Type"> <Title>My Life and Times</Title> <Author>Paul Mc. Cartney</Author> <Date>1998</Date> <ISBN>94303 -12021 -43892</ISBN> <Publisher>Mc. Millin Publishing</Publisher> </Book> <Book xsi: type="Single. Author. Publication"> <Title>Foo. Manchu</Title> <Author>Don Keyote</Author> <Date>1951</Date> </Book. Store> The content model of each <Book> element must be from a type that derives from Publication. Type. In the schema there are two such types - Book. Type and Single. Author. Publication. 16

Review of Abstract Elements and Abstract complex. Types • If you declare an element

Review of Abstract Elements and Abstract complex. Types • If you declare an element to be abstract – - -> Use element substitution for the abstract element (as provided by substitution. Group) • If you declare a complex. Type to be abstract – - -> Use type substitution for the abstract type (as provided by type derivation) 17

Uniqueness & Keys • DTDs provide the ID attribute datatype for uniqueness (i. e.

Uniqueness & Keys • DTDs provide the ID attribute datatype for uniqueness (i. e. , an ID value must be unique throughout the entire document, and the XML parser enforces this). • XML Schema has much enhanced uniqueness capabilities: – enables you to define element content to be unique. – enables you to define non-ID attributes to be unique. – enables you to define a combination of element content and attributes to be unique. – enables you to distinguish between unique versus key. – enables you to declare the range of the document 18 over which something is unique

unique vs key • Key: an element or attribute (or combination thereof) which is

unique vs key • Key: an element or attribute (or combination thereof) which is defined to be a key must: – always be present (min. Occurs must be greater than zero) – be non-nillable (i. e. , nillable="false") – be unique • Key implies unique, but unique does not imply key 19

<? xml version="1. 0"? > <xsd: schema xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" target.

<? xml version="1. 0"? > <xsd: schema xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" target. Namespace="http: //www. books. org" xmlns: bk="http: //www. books. org" element. Form. Default="qualified"> <xsd: element name="Book. Store"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="Book" max. Occurs="unbounded"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="Title" type="xsd: string"/> <xsd: element name="Author" type="xsd: string"/> <xsd: element name="Date" type="xsd: string"/> <xsd: element name="ISBN" type="xsd: string"/> <xsd: element name="Publisher" type="xsd: string"/> </xsd: sequence> </xsd: complex. Type> </xsd: element> </xsd: sequence> </xsd: complex. Type> <xsd: key name="PK"> <xsd: selector xpath="bk: Book"/> <xsd: field xpath="bk: ISBN"/> </xsd: key> </xsd: element> </xsd: schema> 20

<xsd: element name="Book. Store">. . . <xsd: key name="PK"> <xsd: selector xpath="bk: Book"/> <xsd:

<xsd: element name="Book. Store">. . . <xsd: key name="PK"> <xsd: selector xpath="bk: Book"/> <xsd: field xpath="bk: ISBN"/> </xsd: key> </xsd: element> "Within <Book. Store> we define a key, called PK. Select each <Book>, and within each <Book> the ISBN element is a key. " In other words, within <Book. Store> each <Book> must have an <ISBN> and it must be unique. 21

<? xml version="1. 0"? > <Book. Store xmlns="http: //www. books. org" xmlns: xsi="http: //www.

<? xml version="1. 0"? > <Book. Store xmlns="http: //www. books. org" xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance" xsi: schema. Location= "http: //www. books. org Book. Store. xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul Mc. Cartney</Author> <Date>1998</Date> <ISBN>1 -56592 -235 -2</ISBN> <Publisher>Mc. Millin Publishing</Publisher> </Book> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0 -440 -34319 -4</ISBN> <Publisher>Dell Publishing Co. </Publisher> </Book> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0 -06 -064831 -7</ISBN> <Publisher>Harper & Row</Publisher> </Book. Store> A schema-validator will verify that each Book has an ISBN element and that the values are all unique. 22

Notes about <key> • It must be nested within an <element> • It must

Notes about <key> • It must be nested within an <element> • It must come at the end of <element> (after the content model, and attribute declarations) • Use the <selector> element as a child of <key> to select a set of elements for which the key applies. • Use the <field> element as a child of <key> to identify the element or attribute that is to be the key – There can be multiple <field> elements. 23

unique • The <unique> element is used exactly like the <key> element is used.

unique • The <unique> element is used exactly like the <key> element is used. It has a <selector> and one or more <field> elements, just like <key> has. • The only difference is that the schema validator will simply validate that, whenever present, the values are unique. 24

<? xml version="1. 0"? > <xsd: schema xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" target.

<? xml version="1. 0"? > <xsd: schema xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" target. Namespace="http: //www. books. org" xmlns: bk="http: //www. books. org" element. Form. Default="qualified"> <xsd: element name="Book. Store"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="Book" max. Occurs="unbounded"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="Title" type="xsd: string"/> <xsd: element name="Author" type="xsd: string"/> <xsd: element name="Date" type="xsd: string"/> <xsd: element name="ISBN" type="xsd: string" min. Occurs="0"/> <xsd: element name="Publisher" type="xsd: string"/> </xsd: sequence> </xsd: complex. Type> </xsd: element> </xsd: sequence> </xsd: complex. Type> <xsd: unique name="UNIQ"> <xsd: selector xpath="bk: Book"/> <xsd: field xpath="bk: ISBN"/> </xsd: unique> </xsd: element> </xsd: schema> Note: ISBN is optional Require every ISBN be unique. 25

Referencing a key • Recall that by declaring an element of type IDREF then

Referencing a key • Recall that by declaring an element of type IDREF then that element must reference an ID attribute, and an XML Parser will verify that the IDREF value corresponds to a legitimate ID value. • Similarly, you can define a keyref which asserts, "the value of this element must match the value of an element referred to by this". 26

<? xml version="1. 0"? > <Library xmlns="http: //www. library. org" xmlns: xsi="http: //www. w

<? xml version="1. 0"? > <Library xmlns="http: //www. library. org" xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance" xsi: schema. Location= "http: //www. library. org Author. Signing. At. Library. xsd"> <Books> <Book> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0 -440 -34319 -4</ISBN> <Publisher>Dell Publishing Co. </Publisher> </Book>. . . </Books> <Guest. Authors> <Author> <Name>Richard Bach</Name> <Book. For. Signing> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <ISBN>0 -440 -34319 -4</ISBN> </Book. For. Signing> </Author> </Guest. Authors> </Library> A keyref element A key element Suppose that we define a key for ISBN (i. e. , each Book must have an ISBN and it must be unique) We would like to ensure that the ISBN for the Guest. Author matches one of the ISBNs in 27 the Book. Store.

<xsd: element name="Library"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="Books"> <xsd: complex. Type>

<xsd: element name="Library"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="Books"> <xsd: complex. Type> <xsd: sequence> <xsd: element ref="Book" max. Occurs="unbounded"/> </xsd: sequence> </xsd: complex. Type> </xsd: element> <xsd: element ref="Guest. Authors"/> </xsd: sequence> </xsd: complex. Type> <xsd: key name="PK"> <xsd: selector xpath="bk: Books/bk: Book"/> <xsd: field xpath="bk: ISBN"/> </xsd: key> <xsd: keyref name="isbn. Ref" refer="PK"> <xsd: selector xpath="bk: Guest. Authors/bk: Author/bk: Book. For. Signing"/> <xsd: field xpath="bk: ISBN"/> </xsd: keyref> </xsd: element> Author. Signing. At. Library. xsd 28

<xsd: key name="PK"> <xsd: selector xpath="bk: Book. Store/bk: Book"/> <xsd: field xpath="bk: ISBN"/> </xsd:

<xsd: key name="PK"> <xsd: selector xpath="bk: Book. Store/bk: Book"/> <xsd: field xpath="bk: ISBN"/> </xsd: key> This tells the schema-validator to validate that every Book (in Book. Store) has an ISBN, and that ISBN must be unique. <xsd: keyref name="isbn. Ref" refer="PK"> <xsd: selector xpath="bk: Guest. Authors/bk: Author/bk: Book. For. Signing"/> <xsd: field xpath="bk: ISBN"/> </xsd: keyref> This tells the schema-validator that the ISBN of the Book that the Author is signing must refer to one of the ISBN elements in the collection defined by the PK key. 29

Specifying scope of uniqueness in XML Schemas • The key/keyref/unique elements may be placed

Specifying scope of uniqueness in XML Schemas • The key/keyref/unique elements may be placed anywhere in your schema (that is, at the bottom of any element declaration) • Where you place them determines the scope of the uniqueness • Example. We may desire to have uniqueness in a localized region of instance documents. Thus, we would use key/keyref/unique within the element for that region. 30

Additional schema mechanisms include & import open content 31

Additional schema mechanisms include & import open content 31

include & import • xsd: include • similar to a copy and paste •

include & import • xsd: include • similar to a copy and paste • overriding the definitions of the included schema isn’t allowed. <xsd: include schema. Location="character. xsd"/> 32

Assembling a Schema from Multiple Schema Documents • The include element allows you to

Assembling a Schema from Multiple Schema Documents • The include element allows you to access components in other schemas – All the schemas you include must have the same namespace as your schema (i. e. , the schema that is doing the include) – The net effect of include is as though you had typed all the definitions directly into the containing schema Library. Employee. xsd Library. Book. xsd <xsd: schema …> <xsd: include schema. Location="Library. Book. xsd"/> <xsd: include schema. Location="Library. Employee. xsd"/> … </xsd: schema> Library. xsd 33

Assembling a Schema from Multiple Schema Documents with Different Namespaces • The import element

Assembling a Schema from Multiple Schema Documents with Different Namespaces • The import element allows you to access elements and types in a different namespace Namespace A Namespace B A. xsd B. xsd <xsd: schema …> <xsd: import namespace="A" schema. Location="A. xsd"/> <xsd: import namespace="B" schema. Location="B. xsd"/> … </xsd: schema> C. xsd 34

Camera Schema Nikon. xsd Olympus. xsd Pentax. xsd Camera. xsd 35

Camera Schema Nikon. xsd Olympus. xsd Pentax. xsd Camera. xsd 35

<? xml version="1. 0"? > <xsd: schema xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" target.

<? xml version="1. 0"? > <xsd: schema xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" target. Namespace="http: //www. camera. org" xmlns: nikon="http: //www. nikon. com" xmlns: olympus="http: //www. olympus. com" xmlns: pentax="http: //www. pentax. com" element. Form. Default="qualified"> These import <xsd: import namespace="http: //www. nikon. com" schema. Location="Nikon. xsd"/> elements give <xsd: import namespace="http: //www. olympus. com" us access to schema. Location="Olympus. xsd"/> the components <xsd: import namespace="http: //www. pentax. com" in these other schema. Location="Pentax. xsd"/> schemas. <xsd: element name="camera"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="body" type="nikon: body_type"/> <xsd: element name="lens" type="olympus: lens_type"/> <xsd: element name="manual_adapter" type="pentax: manual_adapter_type"/> </xsd: sequence> </xsd: complex. Type> </xsd: element> <xsd: schema> Here I am using the body_type that is defined in the Nikon namespace 36

Extensible Instance Documents • The <any> element enables instance document authors to create instance

Extensible Instance Documents • The <any> element enables instance document authors to create instance documents containing elements above and beyond what was specified by the schema. The instance documents are said to be extensible. Contrast this schema with previous schemas where the content of all our elements were always fixed and static. • We are empowering the instance document author with the ability to define what data makes sense to him/her! 37

Open Content • Definition: an open content schema is one that allows instance documents

Open Content • Definition: an open content schema is one that allows instance documents to contain additional elements beyond what is declared in the schema. This is achieved by using the <any> and <any. Attribute> elements in the schema. • Sprinkling <any> and <any. Attribute> elements liberally throughout your schema will yield benefits in terms of how evolvable your schema is. 38

any. Attribute • The <any. Attribute> element enables the instance document author to extend

any. Attribute • The <any. Attribute> element enables the instance document author to extend his/her document with attributes not specified by the schema. <xsd: element name="Book"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="Title" type="xsd: string"/> <xsd: element name="Author" type="xsd: string"/> <xsd: element name="Date" type="xsd: string"/> <xsd: element name="ISBN" type="xsd: string"/> <xsd: element name="Publisher" type="xsd: string"/> <xsd: any min. Occurs="0"/> </xsd: sequence> <xsd: any. Attribute/> </xsd: complex. Type> </xsd: element> Now an instance document author can add any number of attributes onto a <Book> element (as well as extend the element content). 39

DTD vs Schema ü Enhanced datatypes • 44+ versus 10 • Can create your

DTD vs Schema ü Enhanced datatypes • 44+ versus 10 • Can create your own datatypes ü Written in the same syntax as instance documents ü Better maintainence and readability • Object-oriented - can extend or restrict a type • Modular - include & import ü Extensibility • An open content schema by using the <any> and <any. Attribute> elements. 40

DTD vs Schema üCan specify element content as being unique (keys on content) and

DTD vs Schema üCan specify element content as being unique (keys on content) and uniqueness within a region üCan define elements with nil content üCan define substitutable elements üCan express sets, i. e. , can define the child elements to occur in any order 41

Not “All Powerful” • XML Schemas is very powerful • However, it is not

Not “All Powerful” • XML Schemas is very powerful • However, it is not "all powerful". There are many constraints that it cannot express. Here are some examples: – Ensure that the value of the aircraft <Elevation> element is greater than the value of the obstacle <Height> element. – Ensure that: • if the value of the attribute, mode, is "air", then the value of the element, <Transportation>, is either airplane or hot-air balloon • if mode="water" then <Transportation> is either boat or hovercraft • if mode="ground" then <Transportation> is either car or bicycle. – Ensure that the value of the <Payment. Received> is equal to the value of <Payment. Due>, where these elements are in separate documents! • To check all our constraints we will need to supplement XML Schemas with another tool. 42

Two Approaches to Extending XML Schemas • XSLT/XPath – The first approach is to

Two Approaches to Extending XML Schemas • XSLT/XPath – The first approach is to supplement the XSD document with a stylesheet • Schematron – The second approach is to embed the additional constraints within <appinfo> elements in the XSD document. Then, a tool (Schematron) will extract and process those constraints. 43

Schematron • The Schematron differs in basic concept from other schema languages in that

Schematron • The Schematron differs in basic concept from other schema languages in that it not based on grammars but on finding tree patterns in the parsed document. This approach allows many kinds of structures to be represented which are inconvenient and difficult in grammar-based schema languages. • XSLT/XPath based • W 3 C Schemas are conservative: everything not permitted is forbidden. • Schematron is liberal: everything not forbidden is permitted. • No data typing; validation only • Handles unordered structures very well • Handles descendant constraints very well • Almost self-documenting 44

Element - conditional definition For instance, the following Schematron schema states that if the

Element - conditional definition For instance, the following Schematron schema states that if the element E has the attribute one, then it must have the second attribute two as well: <rule context='E'> <report test='(@one) or not(@one and @two)'> E cannot have attribute 'one‘ alone. </report> </rule> 45

Schema Validators • • • Command Line Only – XSV by Henry Thompson •

Schema Validators • • • Command Line Only – XSV by Henry Thompson • ftp: //ftp. cogsci. ed. ac. uk/pub/XSV 12. EXE Has a Programmatic API – xerces by Apache • http: //www. apache. org/xerces-j/index. html – IBM Schema Quality Checker (Note: this tool is only used to check your schema. It cannot be used to validate an instance document against a schema. ) • http: //www. alphaworks. ibm. com/tech/xmlsqc – MSXML 4. 0 • http: //www. microsoft. com GUI Oriented – XML Spy • http: //www. xmlspy. com – Turbo XML • http: //www. extensibility. com 46

Sources • http: //www. w 3. org/XML/Schema • http: //www. xfront. com/ • http:

Sources • http: //www. w 3. org/XML/Schema • http: //www. xfront. com/ • http: //www. xml. com/ 47