1 XML Schema – Part 2 More on Schema Types & Derivation Abstact types & type substitution Uniqueness & Keys Additional schema mechanisms - include & import.

Slides:



Advertisements
Similar presentations
1 Web Data Management XML Schema. 2 In this lecture XML Schemas Elements v. Types Regular expressions Expressive power Resources W3C Draft:
Advertisements

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.
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.
An Introduction to XML Schema CSCI 7818 by Ming Rutar.
Document Type Definitions
XML Schemas Lecture 10, 07/10/02. Acknowledgements A great portion of this presentation has been borrowed from Roger Costello’s excellent presentation.
Lecture 14 XML Validation. a simple element containing text attribute; attributes provide additional information about an element and consist of a name.
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.
1 XML Schemas Marco Mesiti This Presentation has been extracted from Roger L. Costello (XML Technologies Course)
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.
Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course.
1 Modelling Hachim Haddouti Al Akhawayn University SSE
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.
1 Advanced Topics XML and Databases. 2 XML u Overview u Structure of XML Data –XML Document Type Definition DTD –Namespaces –XML Schema u Query and Transformation.
Manohar – Why XML is Required Problem: We want to save the data and retrieve it further or to transfer over the network. This.
Introduction to XML: Part I By Sandeep Jangity CS 157B, Section 2 Dr. Lee.
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
Lecture 15 XML Validation. a simple element containing text attribute; attributes provide additional information about an element and consist of a name.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation Document Type Definition.
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)
Neminath Simmachandran
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Schemas Ellen Pearlman Eileen Mullin Programming the Web Using XML.
1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1) (Structures)
Schemas1 XML Schema More Powerful. Schemas 2 DTD – Schema - Relax An XML schema is a description of a type of XML document, typically expressed in terms.
XML CPSC 315 – Programming Studio Fall 2008 Project 3, Lecture 1.
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.
Of 33 lecture 3: xml and xml schema. of 33 XML, RDF, RDF Schema overview XML – simple introduction and XML Schema RDF – basics, language RDF Schema –
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
IS432 Semi-Structured Data Lecture 2: DTD Dr. Gamal Al-Shorbagy.
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.
An OO schema language for XML SOX W3C Note 30 July 1999.
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.
An Introduction to XML Sandeep Bhattaram
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.
XSD: XML Schema Language Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
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.
XML Validation. a simple element containing text attribute; attributes provide additional information about an element and consist of a name value pair;
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.
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 QUESTIONS AND ANSWERS
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
XML Data DTDs, IDs & IDREFs.
New Perspectives on XML
Presentation transcript:

1 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.

2 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.

3

4 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 anyType. The default syntax for complex types is complex content that restricts anyType.

5 Schema Types

6 anyType The anyType 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: This is the definition of the anyType.

7 Complex Type

8 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 with, right? Wrong. Complex types with simple content also allow data content, and we want an empty element. That leaves us with with, 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:

9 Empty Element <xsd:attribute name="href" type="xsd:anyURI" use="required"/> Schema: Instance doc (snippet): DTD:

10 Type Substitutability As we saw earlier, substitutionGroup 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 BookType is derived from PublicationType. If we declare an element, Publication, to be of type PublicationType (the base type) then in the instance document Publication's content can be either a PublicationType or a BookType.

11 <xsd:schema xmlns:xsd=" targetNamespace=" xmlns=" elementFormDefault="unqualified"> BookType extends PublicationType Publication is of type PublicationType (the base type) PublicationType is the base type

12 <bk:BookStore xmlns:bk=" xmlns:xsi=" xsi:schemaLocation= " BookStore.xsd"> Staying Young Forever Karin Granstrom Jordan, M.D Illusions The Adventures of a Reluctant Messiah Richard Bach Dell Publishing Co. The First and Last Freedom J. Krishnamurti Harper & Row BookStore.xml This Publication’s content model is PublicationType This Publication’s content model is BookType This Publication’s content model is BookType

13 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. This example defines name-elt as an abstract element that should be replaced either by name or surname everywhere it is referenced. Abstract Elements

14 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.

15 Note that PublicationType is declared abstract. Book derives from PublicationType. By default abstract="false". Thus, this type can substitute for the PublicationType.

16 <BookStore xmlns=" xmlns:xsi=" xsi:schemaLocation= " BookStore.xsd"> My Life and Times Paul McCartney McMillin Publishing FooManchu Don Keyote 1951 The content model of each element must be from a type that derives from PublicationType. In the schema there are two such types - BookType and SingleAuthorPublication.

17 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)

18 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

19 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-nillable (i.e., nillable="false") –be unique Key implies unique, but unique does not imply key

20 <xsd:schema xmlns:xsd=" targetNamespace=" xmlns=" xmlns:bk=" elementFormDefault="qualified">

21... "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.

22 <BookStore xmlns=" xmlns:xsi=" xsi:schemaLocation= " BookStore.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 A schema-validator will verify that each Book has an ISBN element and that the values are all unique.

23 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.

24 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.

25 <xsd:schema xmlns:xsd=" targetNamespace=" xmlns=" xmlns:bk=" elementFormDefault="qualified"> Note: ISBN is optional Require every ISBN be unique.

26 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".

27 <Library xmlns=" xmlns:xsi=" xsi:schemaLocation= " AuthorSigningAtLibrary.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 BookStore. A key element A keyref element

28 AuthorSigningAtLibrary.xsd

29 This tells the schema-validator to validate that every Book (in BookStore) 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.

30 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.

31 Additional schema mechanisms include & import open content

32 include & import xsd:include similar to a copy and paste overriding the definitions of the included schema isn’t allowed.

33 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 … LibraryBook.xsd LibraryEmployee.xsd Library.xsd

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

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

36 <xsd:schema xmlns:xsd=" targetNamespace=" xmlns:nikon=" xmlns:olympus=" xmlns:pentax=" elementFormDefault="qualified"> <xsd:import namespace=" schemaLocation="Nikon.xsd"/> <xsd:import namespace=" schemaLocation="Olympus.xsd"/> <xsd:import namespace=" schemaLocation="Pentax.xsd"/> These import elements give us access to the components in these other schemas. Here I am using the body_type that is defined in the Nikon namespace

37 Extensible Instance Documents The 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!

38 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 and elements in the schema. Sprinkling and elements liberally throughout your schema will yield benefits in terms of how evolvable your schema is.

39 anyAttribute The element enables the instance document author to extend his/her document with attributes not specified by the schema. Now an instance document author can add any number of attributes onto a element (as well as extend the element content).

40 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 and elements.

41 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

42 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.

43 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.

44 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 W3C 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

45 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: E cannot have attribute 'one‘ alone.

46 Schema Validators Command Line Only –XSV by Henry Thompson ftp://ftp.cogsci.ed.ac.uk/pub/XSV/XSV12.EXE Has a Programmatic API –xerces by Apache –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.) –MSXML4.0 GUI Oriented –XML Spy –Turbo XML

47 Sources