Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course.

Slides:



Advertisements
Similar presentations
CSPP51038 WSDL in depth. Advanced Schema features (required for understanding wsdl)
Advertisements

Copyright © [2002]. Roger L. Costello. All Rights Reserved. 1 XML Schemas Reference Manual Roger L. Costello XML Technologies Course.
XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
1 Web Data Management XML Schema. 2 In this lecture XML Schemas Elements v. Types Regular expressions Expressive power Resources W3C Draft:
XML 6.3 DTD 6. XML and DTDs A DTD (Document Type Definition) describes the structure of one or more XML documents. Specifically, a DTD describes:  Elements.
Copyright © Open Applications Group, Inc. All rights reserved 1 OAGi XML Schema User Report June 21, Michael.
1 XML DTD & XML Schema Monica Farrow G30
+ XSL eXtensible Stylesheet Language. + 2 XML Lecture Adapted from the work of Prof Mark Baker ACET, University of Reading.
1 Implementing Variable Content Containers XML Schemas: Best Practices A set of guidelines for designing XML Schemas Created by discussions on xml-dev.
1 XML Schema – Part 2 More on Schema Types & Derivation Abstact types & type substitution Uniqueness & Keys Additional schema mechanisms - include & import.
An Introduction to XML Schema CSCI 7818 by Ming Rutar.
Copyright (c) [2000]. Roger L. Costello. All Rights Reserved. 1 XML Schemas Roger L.
XML Schemas Lecture 10, 07/10/02. Acknowledgements A great portion of this presentation has been borrowed from Roger Costello’s excellent presentation.
Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course.
1 Substitution Groups in XML Schemas Tomer Shiran Winter 2003/4 Semester.
DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.
XML Schemas and Namespaces Lecture 11, 07/10/02. BookStore.dtd.
ΑΝΑΠΑΡΑΣΤΑΣΗ ΓΝΩΣΗΣ ΣΤΟΝ ΠΑΓΚΟΣΜΙΟ ΙΣΤΟ XML Schema
Sunday, June 28, 2015 Abdelali ZAHI : FALL 2003 : XML Schemas XML Schemas Presented By : Abdelali ZAHI Instructor : Dr H.Haddouti.
1 … more on XML Schemas. 2 Name Conflicts Whereas DTDs required every element to have a unique name, XML Schemas enable you to use the same name in multiple.
Document Type Definitions. XML and DTDs A DTD (Document Type Definition) describes the structure of one or more XML documents. Specifically, a DTD describes:
Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course.
Introduction to XML This material is based heavily on the tutorial by the same name at
XML and Data Exchanges IS617 Fall 2012 Dr. Dwyer.
XP New Perspectives on XML Tutorial 4 1 XML Schema Tutorial – Carey ISBN Working with Namespaces and Schemas.
XP New Perspectives on XML Tutorial 3 1 DTD Tutorial – Carey ISBN
Pemrograman Berbasis WEB XML part 2 -Aurelio Rahmadian- Sumber: w3cschools.com.
Validating DOCUMENTS with DTDs
1 Multi-Schema Project: Zero, One, or Many Namespaces? XML Schemas: Best Practices A set of guidelines for designing XML Schemas Created by discussions.
Why XML ? Problems with HTML HTML design - HTML is intended for presentation of information as Web pages. - HTML contains a fixed set of markup tags. This.
XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML.
Chapter 4: Document Type Definitions. Chapter 4 Objectives Learn to create DTDs Validate an XML document against a DTD Use DTDs to create XML documents.
Creating Extensible Content Models XML Schemas: Best Practices A set of guidelines for designing XML Schemas Created by discussions on xml-dev.
1 XML Schemas. 2 Useful Links Schema tutorial links:

Dr. Azeddine Chikh IS446: Internet Software Development.
Copyright © [2001]. Roger L. Costello. All Rights Reserved. 1 XML Schemas (Primer)
XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
XP 1 DECLARING A DTD A DTD can be used to: –Ensure all required elements are present in the document –Prevent undefined elements from being used –Enforce.
Ceng 520 XML Schemas IntroductionXML Schemas 2 Part 0: Introduction Why XML Schema?
1 Tutorial 13 Validating Documents with DTDs Working with Document Type Definitions.
Extending XML Schemas XML Schemas: Best Practices A set of guidelines for designing XML Schemas Created by discussions on xml-dev.
Default Namespace - targetNamespace or XMLSchema? XML Schemas: Best Practices A set of guidelines for designing XML Schemas Created by discussions on xml-dev.
IVOA Registry videocon 2004/05/13-14 Gerard Lemson1 Model based schema.
Beginning XML 4th Edition. Chapter 5: XML Schemas.
Copyrighted material John Tullis 10/17/2015 page 1 04/15/00 XML Part 3 John Tullis DePaul Instructor
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation Identity Constraints.
New Perspectives on XML, 2nd Edition
XML Schema. Why Schema? To define a class of XML documents Serve same purpose as DTD “Instance document" used for XML document conforming to schema.
XML – Part III. The Element … This type of element either has the element content or the mixed content (child element and data) The attributes of the.
Sheet 1XML Technology in E-Commerce 2001Lecture 2 XML Technology in E-Commerce Lecture 2 Logical and Physical Structure, Validity, DTD, XML Schema.
XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents.
1 Tutorial 14 Validating Documents with Schemas Exploring the XML Schema Vocabulary.
Tutorial 13 Validating Documents with Schemas
Processing of structured documents Spring 2003, Part 3 Helena Ahonen-Myka.
Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an.
XML Schema (W3C) Thanks to Jussi Pohjolainen TAMK University of Applied Sciences.
XSD: XML Schema Language Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
XML Schema Definition (XSD). Definition of a Schema It is a model for describing the structure and content of data The XML Schema was developed as a content.
Deriving Complex Types In XML Schema By: Roy Navon.
Tutorial 2: XML Working with Namespaces. 2 Name Collision This figure shows two documents each with a Name element.
XML Validation II Advanced DTDs + Schemas Robin Burke ECT 360.
1 Global versus Local XML Schemas: Best Practices A set of guidelines for designing XML Schemas Created by discussions on xml-dev.
CITA 330 Section 4 XML Schema. XML Schema (XSD) An alternative industry standard for defining XML dialects More expressive than DTD Using XML syntax Promoting.
XML SCHEMA 1 CH 20. Objective 2 What’s wrong with DTDs? What is a schema? The W3C XML Schema Language Hello schemas Complex types Simple types Deriving.
4 Copyright © 2004, Oracle. All rights reserved. Validating XML by Using XML Schema.
CITA 330 Section 2 DTD. Defining XML Dialects “Well-formedness” is the minimal requirement for an XML document; all XML parsers can check it Any useful.
XML Schemas Dr. Awad Khalil Computer Science Department AUC.
New Perspectives on XML
New Perspectives on XML
Presentation transcript:

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 2 Name Conflicts Whereas DTDs required every element to have a unique name, XML Schemas enable you to use the same name in multiple places. Where can the same name be used, and where will there be a name conflict? Here are the things to remember: –Type definitions (complexType and simpleType) are placed in one symbol space. Element declarations are placed in a second symbol space and attribute declarations are placed in a third symbol space. Hence, you can have a type and an element and an attribute all with the same name! –Each type definition creates a new symbol space

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 3 What's Legal? Legal –Element, attribute, type (complex or simple) with the same name –Same name in different Symbol Spaces –Same name in different namespaces Illegal –Same name and same Symbol Space but different type Legal –Same name and same Symbol Space and same type

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved </xsd:complexType Same name, type, Symbol Space --> Legal... </xsd:complexType Same name, Symbol Space, different type --> Illegal

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 5 ScopeTest.xsd (see example 22) Global element symbol space Global type symbol space BookOnCars Title Global attribute symbol space Title

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 6 anonymous symbol space Chapter Title anonymous symbol space Title Section anonymous symbol space Title symbol space CarManufacturer Year ScopeTest.xsd (see example 22)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 7 But, but, but,... what does this all mean in terms of the namespace that the schema document is defining??? –i.e., the different Symbol Spaces are allowing multiple items with the same name. Is this going to result in a lot of name collisions in the namespace?

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 8 BookOnCars Chapter Section Title Name collisions!!! CarBooks Namespace?

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 9 Global/Local Elements and Namespaces Only global elements are in the namespace! Local elements are associated with the global elements. In our example, the only elements in the namespace are BookOnCars and Title (the globally-declared Title element) BookOnCars has two local elements associated with it - Chapter and Title. –Chapter has two elements associated with it - Title and Section Title has one element associated with it - Title

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 10 BookOnCars Title CarBooks Namespace [1] [1] Later we will see that the namespace also contains the global types and attributes.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 11 Same Situation with Attributes Attributes in an XML document are in the same situation as the schema local elements - there can be many attributes with the same name in an XML document; thus, there would be many name collisions if attributes were made part of a default namespace. Instead, we say that attributes are not in a default namespace. Rather, they are associated with elements which are in the namespace. (See next slide for example)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 12 The secret to using a 35mm camera is … Book Namespace Book Section Body Chapter Notice that there are multiple title attributes. If they were all in the namespace then there would be a 3-way name collision. The namespace does not include the attributes. [ On the other hand, one could argue that the attributes are in the namespace by virtue of the fact that they are associated with elements which are in the namespace. ] Default namespace declaration

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 13 But, but, but,... In the instance documents we have been qualifying all elements, thus indicating that they are all in the namespace: My Life and Times Paul McCartney July, McMillin Publishing... Default name- space declaration asserts that all these elements are in the Publishing name- space.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 14 Unqualified Local Elements - How? Okay, so local elements are not really in targetNamespace (rather, they are in it, but only by association with a global element which is in it). So how can we indicate to instance document creators that they should only qualify the global elements? –Answer: elementFormDefault="unqualified"

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 15 elementFormDefault In all of our examples thus far we have set the value of this schema attribute to "qualified". The "qualified" value means that in an instance document all elements must be qualified. Alternatively, you can assign elementFormDefault the value "unqualified". The "unqualified" value means that in an instance document only the global elements can be qualified and the local elements must not be qualified.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 16 <xsd:schema xmlns:xsd=" targetNamespace=" xmlns=" elementFormDefault="unqualified"> (see example 23) Notice that there is only one global element in the schema

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 17 <b:BookCatalogue xmlns:b=" xmlns:xsi=" xsi:schemaLocation= " BookCatalogue.xsd"> My Life and Times Paul McCartney McMillin Publishing Illusions The Adventures of a Reluctant Messiah Richard Bach Dell Publishing Co. The First and Last Freedom J. Krishnamurti Harper & Row Now we don’t use a default namespace declaration, and we qualify only the global element - BookCatalogue. (see example 19)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 18 What’s Validated? In the previous example, do the local, unqualified elements - Book, Title, Author, Date, ISBN, Publisher - get validated? Yes! Everything is validated just as before, when we used elementFormDefault="qualified"

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 19 Use qualified or unqualified? In an instance document, regardless of whether you qualify all elements or just the global elements (as dictated by the value of elementFormDefault) they all get validated. So, what difference does it make whether you assign elementFormDefault the value of qualified or unqualified? Case 1: elementFormDefault="unqualified" and thus in the instance documents only the global elements are qualified –Pro: hides namespace complexity in the schema –Con: if the schema is modified by making local declarations global then all instance documents are impacted; the user needs to keep track of which elements are global versus which elements are local. Case 2: element elementFormDefault="qualified" and thus in the instance documents all elements are qualified –Pro: if the schema is modified by making local declarations global then the instance documents are not impacted; the user doesn’t need to keep track of which elements are global and which elements are local; for copyright/traceability purposes it may be desirable to explicitly expose the namespaces in the instance document –Con: exposes namespace complexity to the instance documents

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 20 Type Substitutability As we saw earlier, substitutionGroup gives us "element substitutability", i.e., the ability to substitute one element for another. Now we will see how to achieve "type substitutability", i.e., the ability to substitute an element’s content model with another content model. Here’s how type substitutability works: A base type can be substituted by any derived type. –Example. Suppose that the Book type is derived from Publication. If we declare an element, Listing, to be of type Publication (the base type) then in the instance document Listing's content can be either a Publication or a Book (since Book is a Publication).

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 21 <xsd:schema xmlns:xsd=" targetNamespace=" xmlns=" elementFormDefault="unqualified"> (see example 24) Book extends Publication Listing is of type Publication (the base type) Publication is the base type Note this

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 22 <cat:Catalogue xmlns:cat =" xmlns:xsi=" xsi:schemaLocation=" BookCatalogue.xsd"> Staying Young Forever Karin Granstrom Jordan, M.D. December, 1999 Illusions The Adventures of a Reluctant Messiah Richard Bach Dell Publishing Co. The First and Last Freedom J. Krishnamurti Harper & Row BookCatalogue.xml (see example 24) This Listing’s content model is the Publication type This Listing’s content model is the Book type This Listing’s content model is the Book type

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 23 Illusions The Adventures of a Reluctant Messiah Richard Bach Dell Publishing Co. "The Listing element is declared to be of type Publication. Book is derived from Publication. Therefore, Book is a Publication. Thus, the content of Listing can be a Book. However, to indicate that the content is not the source type, but rather a derived type, we need to specify the derived type that is being used. The attribute 'type' comes from the XML Schema Instance (xsi) namespace." Note in the schema that the Book type is a global type definition. By qualifying Book (cat:Book) we are asserting that the Book type comes from the catalogue namespace.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 24 Why is xsi:type Needed? Why, in an instance document, do we need to indicate the derived type being used? Couldn’t the schema validator figure out which type was being used? –Answer: Easier to implement a schema validator Good practice

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 25 Catalogue Namespace Publication (T) Book (T) Catalogue (E) T = Type E = Element A namespace consists of all of the global stuff in the schema - global elements, attributes and types!

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 26 Summary of the Contents of the Namespace that a Schema Creates What is in the namespace that a schema creates? –The namespace is comprised of only the global stuff: Global elements Global attributes Global complexTypes Do Lab 13

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 27 block Attribute You may add an attribute, block, to either an element or a complexType definition. –If you add a block attribute to an element then the content model of that element may not be replaced by a derived type –If you add a block attribute to a complexType then that complexType’s content model may not be replaced by a derived type in any element which is declared of that complexType.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 28 Illusions The Adventures of a Reluctant Messiah Richard Bach Dell Publishing Co. Schema: Instance doc: The Publication type, and types derived from Publication may be substituted for the content model of Listing, e.g., Book may be used.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 29 Illusions The Adventures of a Reluctant Messiah Richard Bach Dell Publishing Co. Schema: Instance doc: This prohibits the use of types derived from Publication from being used as the content model of Listing, i.e.., this is not allowed

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 30 Illusions The Adventures of a Reluctant Messiah Richard Bach Dell Publishing Co. Schema: Instance doc: This prohibits Publication’s content model from being replaced by a derived type in any element declared to be of Publication type, such as Listing, i.e., this is not allowed

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 31 Block Attribute block="extension" –Prohibits you from substituting a derived-by-extension type for an element's content block="restriction" –Prohibits you from substituting a derived-by-restriction type for an element's content block="#all" –Prohibits you from substituting any derived type for an element's content block="substitution" –This prohibits element substitution

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 32 Abstract Elements You can declare an element to be abstract –Example. An abstract element is a template/placeholder element: –If an element is declared abstract then in an XML instance document that element may not appear. Example. The element shown above may not appear in an instance document. –However, elements that are substitutionGroup’ed to the abstract type may appear in its place.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 33 (see example 25) Since the Publication element is abstract, only substitutionGroup’ed elements can appear as children of Catalogue. The Book and Magazine elements are substitutionGroup'ed to the Publication element.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 34 <Catalogue xmlns=" xmlns:xsi=" xsi:schemaLocation= " Catalogue.xsd"> Natural Health 1999 Illusions The Adventures of a Reluctant Messiah Richard Bach Dell Publishing Co. The First and Last Freedom J. Krishnamurti Harper & Row (see example 25) An XML Instance Document Conforming to Catalogue.xsd

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 35 Abstract complexType You can declare a complexType to be abstract –Example. An abstract complexType 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 PublicationType (shown above) may not have that type’s content model. –However, complexType’s that are derived from the abstract type may substitute for the abstract type.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 36 Note that PublicationType is declared abstract. Book derives from PublicationType. By default abstract="false". Thus, this type can substitute for the PublicationType. (see example 26)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 37 <BookCatalogue xmlns=" xmlns:xsi=" xsi:schemaLocation= " BookCatalogue.xsd"> My Life and Times Paul McCartney McMillin Publishing FooManchu Don Knox 1951 (see example 26) The content model of each element must be from a type that derives from PublicationType. In the schema there are two such types - Book and SingleAuthorPublication.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 38 Review of Abstract Elements and Abstract complexTypes If you declare an element to be abstract –- -> Use element substitution for the abstract element (as provided by substitutionGroup) If you declare a complexType to be abstract –- -> Use type substitution for the abstract type (as provided by type derivation) Do Lab 14

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 39 Redefining a Type from the Included Schema The element does the same thing as an element (i.e., it allows you to access components in other schemas, provided they have the same namespace), plus it enables you to redefine one or more components (simpleType, complexType, attributeGroup, or group)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 40 LibraryBookCatalogue.xsd (snippet, see example27) Library.xsd (snippet, see example27)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 41 This element does two things: - it includes the components from LibraryBookCatalogue.xsd - it redefines CardCatalogueEntry (in LibraryBookCatalogue.xsd) by extending it with a new element (Review). Library.xsd (snippet, see example27)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 42 Note about When a schema redefines a component then it's as though the old version of the component no longer exists. Any reference to the redefined component (either in the included schema or in the including schema) is to this new version.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 43 LibraryBookCatalogue.xsd (snippet, see example27) Because CardCatalogueEntry has been redefined, Book's content now also includes Review.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 44 Element [simpleType or complexType or attributeGroup or group]* “The element can redefine zero or more components in the referenced schema.”

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 45 Redefining a Schema with no targetNamespace External components from schemas that have no namespace can also be redefined. The redefined components become part of the redefining schema's namespace. Thus, a schema with no namespace may "blend in" with a variety of different schemas and take on each schema's namespace! Just like a chameleon!

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 46 <xsd:schema xmlns:xsd=" elementFormDefault="qualified"> BookCatalogue.xsd (see example28) Note that there is no targetNamespace!

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 47 Library.xsd (snippet, see example28) As soon as we redefine BookCatalogue.xsd it takes on the library namespace.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 48 Note about redefining a schema with no targetNamespace If the components in the schema with no targetNamespace ref one another then the ing schema must be designed so that the targetNamespace is the default namespace! When this schema takes on a namespace then type="CardCatalogueEntry" will be referring to the default namespace. If the default namespace is not the targetNamespace then this reference will break!

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 49 Version Management The schema element has an optional attribute, version, which you may use to indicate the version of your schema (for private version documentation of the schema) <xsd:schema xmlns:xsd=" targetNamespace=" xmlns=" elementFormDefault="qualified" version="1.0">...

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 50 null Content You can indicate in a schema that an element may be null in the instance document. Empty content vs null: –Empty: an element with an empty content is constrained to have no content. –null: an instance document element may indicate no value is available by setting an attribute - xsi:null - equal to 'true' John Doe XML Schema: XML instance document: The content of middle can be a NMTOKEN value or, its content can be undefined.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 51 ur-type The ur-type is the base type for all types which do not specify a value for the base attribute. It is the type for all elements which do not specify a type. –Example:

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 52 anyType You may declare an element to be of type "anyType" The anyType is equivalent to ur-type. Whereas ur-type cannot be used directly, anyType can (see next slide)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 53 Note about ur-type You cannot directly use the ur-type: Instead, it is the default type when a type is not specified in an element declaration, or when you specify the type as "anyType" No type specified so it defaults to ur-typeanyType is equivalent to ur-type

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 54 Note about schemaLocation schemaLocation is just a hint to the XML Parser "The choice of which schema to use ultimately lies with the consumer. If you as a consumer wish to rely on the schemaLocation idiom, then you should purchase/use processors that will honor that for you. The reason that some other processors might not provide that service to you is that they are designed to run in environments where it is impractical or undesirable to allow the document author to force reference to and use of some particular schema document." (Noah Mendelsohn, XML Schema WG) For this tutorial I have used an XML Schema Validator which uses the schemaLocation idiom.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 55 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 over which something is unique

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 56 unique vs key Key: an element or attribute (or combination thereof) which is defined to be a key must –always be present (minOccurs must be greater than zero) –be non-nullable (i.e., nullable="false") –be unique Key implies unique, but unique does not imply key

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 57 Using ISBN as a Key When a book is published it has an ISBN, which is guaranteed to be unique. In the BookCatalogue we should be able to express that each Book's ISBN element is unique. Further, let's make the ISBN elements keys (i.e., both unique and required to exist).

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 58 <xsd:schema xmlns:xsd=" targetNamespace=" xmlns=" elementFormDefault="qualified"> (see example 29)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved "Within we define a key, called PK. Select each, and within each the ISBN element is a key." In other words, within each must have an and it must be unique. This is nice! We are using the content of a field as a key! (No longer limited to ID attributes for defining uniqueness.)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 60 <BookCatalogue xmlns=" xmlns:xsi=" xsi:schemaLocation= " BookCatalogue.xsd"> My Life and Times Paul McCartney McMillin Publishing Illusions The Adventures of a Reluctant Messiah Richard Bach Dell Publishing Co. The First and Last Freedom J. Krishnamurti Harper & Row (see example 29) A schema-validator will verify that each Book has an ISBN element and that the values are all unique.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 61 Notes about It must be nested within an It must come at the end of (after the content model, and attribute declarations) Use the element as a child of to select a set of elements for which the key applies. Use the element as a child of to identify the element or attribute that is to be the key –There can be multiple elements. See next example.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 62 <xsd:schema xmlns:xsd=" targetNamespace=" xmlns=" elementFormDefault="qualified"> The key is the combination of the First and Last name. (See example30)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 63 <Y2KFamilyReunion xmlns=" xmlns:xsi=" xsi:schemaLocation= " Y2KFamilyReunion.xsd"> Peter Brown Peter Costello A schema-validator will verify that each First name/Last name combination is unique.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 64 unique The element is used exactly like the element is used. It has a and one or more elements, just like has. The only difference is that the schema validator will simply validate that, whenever present, the values are unique.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 65 <xsd:schema xmlns:xsd=" targetNamespace=" xmlns=" elementFormDefault="qualified"> (see example 31) Note: ISBN is optional Require every ISBN be unique.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 66 <BookCatalogue xmlns=" xmlns:xsi=" xsi:schemaLocation= " BookCatalogue24.xsd"> My Life and Times Paul McCartney 1998 McMillin Publishing Illusions The Adventures of a Reluctant Messiah Richard Bach Dell Publishing Co. The First and Last Freedom J. Krishnamurti Harper & Row (see example 31) A schema-validator will verify that each Book which has an ISBN element, has a unique value (note that the first Book does not have an ISBN. That's perfectly valid!)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 67 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 these elements must match the value of an element referred to by this".

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 68 <Library xmlns=" xmlns:xsi=" xsi:schemaLocation= " AuthorSigningAtLibrary27.xsd">... Illusions The Adventures of a Reluctant Messiah Richard Bach Dell Publishing Co.... Richard Bach Illusions The Adventures of a Reluctant Messiah 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 GuestAuthor matches one of the ISBNs in the BookCatalogue. A key element A keyref element

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 69 AuthorSigningAtLibrary.xsd (snippet, see example32)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 70 This tells the schema validator to validate that every Book (in BookCatalogue) has an ISBN, and that ISBN must be unique. 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.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 71 Note about key and keyref If there are 2 fields in the key, then there must be 2 fields in the keyref, if there are 3 fields in the key, then there must be 3 fields in the keyref, etc. Further, the fields in the keyref must match in type and position to the key.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 72 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.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 73 Mixed Content In all of our examples the content of each element was either –all elements, or –all data An element that contains a mix of elements and (string) data is called "mixed content". Mixed content has many applications. For example, XSLT uses mixed content frequently in template rules, e.g., The title of the book is: The author of the book is: Notice that the content of the xsl:template element is a mix of string data and elements.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 74 Specifying Mixed Content when Declaring an Element The element has an optional attribute, mixed. By default, mixed="false". To specify that an element can have mixed content use

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 75 <Letter xmlns=" xmlns:xsi=" xsi:schemaLocation= " Letter.xsd"> Dear Sirs: This letter is to inform you that we are are finding your tool very useful. Letter.xml (see example33)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 76 <xsd:schema xmlns:xsd=" targetNamespace=" xmlns=" elementFormDefault="qualified"> <xsd:element name="emp" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> Letter.xsd (see example33)

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 77 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 element is greater than the value of the obstacle element. –Ensure that: if the value of the attribute, mode, is "air", then the value of the element,, is either airplane or hot-air balloon if mode="water" then is either boat or hovercraft if mode="ground" then is either car or bicycle. –Ensure that the value of the is equal to the value of, where these elements are in separate documents! To check all our constraints we will need to supplement XML Schemas with another tool.

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 78 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 elements in the XSD document. Then, a tool (Schematron) will extract and process those constraints. See my tutorial on Extending XML Schemas for more info...