Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ceng 520 XML Schemas. 10.03.200520.0 3.2003 IntroductionXML Schemas 2 Part 0: Introduction Why XML Schema?

Similar presentations


Presentation on theme: "Ceng 520 XML Schemas. 10.03.200520.0 3.2003 IntroductionXML Schemas 2 Part 0: Introduction Why XML Schema?"— Presentation transcript:

1 Ceng 520 XML Schemas

2 10.03.200520.0 3.2003 IntroductionXML Schemas 2 Part 0: Introduction Why XML Schema?

3 10.03.200520.0 3.2003 IntroductionXML Schemas 3 Purpose of XML schemas 1-The structure of instance documents –defines elements that can appear in a document, –defines attributes that can appear in a document, –defines which elements are child elements, –defines the sequence in which the child elements can appear, –defines the number of child elements, –defines whether an element is empty or can include text, –defines default values for elements and attributes. w Describe document class’s logical structure. w defines: 2-The datatype of each element/attribute Same Purpose is achieved with DTD

4 10.03.200520.0 3.2003 IntroductionXML Schemas 4 Why XML Schemas ?  DTDs present several limitations: –It's a different syntax You write your XML (instance) document using one syntax and the DTD using another syntax bad, inconsistent –Limited data type capability DTDs support a very limited capability for specifying data types (only PCDATA). You can't, for example, impose that an element is an integer with a range of 0 to 100. –Limited reuse capability (offer only ENTITY mechanism) –Constructors (“,”, “¦”) and occurrences indicators (“*”, “+”, “?”) don’t offer a lot of possibilities to constraint element’s occurrences.

5 10.03.200520.0 3.2003 IntroductionXML Schemas 5 XML Schemas Vs DTDs XML Schemas are a tremendous advancement over DTDs: –Enhanced data types 44+ versus 10 Can create your own data types –Written in the same syntax as instance documents You don't have to learn another language You can use your XML editor to edit your Schema files You can use your XML parser to parse your Schema files You can manipulate your Schema with the XML DOM You can transform your Schema with XSLT –XML Schemas are Object-oriented (Extensible) Can extend or restrict a type (derive new type definitions on the basis of old ones) XML Schemas are extensible, just like XML, because they are written in XML. With an extensible Schema definition you can: –Reuse your Schema in other Schemas –Create your own data types derived from standard types –Reference multiple schemas from the same document

6 10.03.200520.0 3.2003 IntroductionXML Schemas 6 Example: BookStore.dtd ATTLIST ELEMENT ID #PCDATA NMTOKEN ENTITY CDATA BookStore Book Title Author Date ISBN Publisher

7 10.03.200520.0 3.2003 IntroductionXML Schemas 7 Example: BookStore.xsd <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <!ELEMENT Book (Title, Author, Date, ISBN, Publisher)>

8 10.03.200520.0 3.2003 IntroductionXML Schemas 8 Let’s get started!  Data Types  Type Derivation  Name Spaces  Advanced XML SchemaConception  More XML Schema!!!!!  Conclusions  References

9 10.03.200520.0 3.2003 IntroductionXML Schemas 9 Part 1: Name Spaces

10 10.03.200520.0 3.2003 IntroductionXML Schemas 10 Name spaces element complexType schema sequence http://www.w3.org/2001/XMLSchema string integer boolean BookStore Book Title Author Date ISBN Publisher http://www.books.org (targetNamespace) This is the vocabulary that XML Schemas provide to define your new vocabulary One difference between XML Schemas and DTDs is that the XML Schema vocabulary is associated with a name (namespace). Likewise, the new vocabulary that you define must be associated with a name (namespace).

11 10.03.200520.0 3.2003 IntroductionXML Schemas 11 Example: BookStore.xsd < xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> Elements and data types: schema element complex Type sequence string boolean integer …etc Come from the namespace: http://www.w3.org/2001/XMLSchema An XML schema has « schema » as root element xsd = Xml-Schema Definition

12 10.03.200520.0 3.2003 IntroductionXML Schemas 12 Target Name Space <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> Elements defined by this schema - BookStore - Book - Title - Author - Date - ISBN - Publisher are to go in this space name

13 10.03.200520.0 3.2003 IntroductionXML Schemas 13 Default Name space This is referencing a Book element declaration. Since there is no namespace qualifier it is referencing the Book element in the default Namespace. The default namespace is http://www.books.org which is the targetNamespace! <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified">

14 10.03.200520.0 3.2003 IntroductionXML Schemas 14 <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns:bk="http://www.books.org" elementFormDefault="qualified"> Note that http://…/XMLSchema is the default namespace. Consequently, there are no namespace qualifiers on - schema - element - complexType - sequence - string Default XMLSchema, Qualify targetNamespace

15 10.03.200520.0 3.2003 IntroductionXML Schemas 15 Name Space: Conclusion BookStore.xml BookStore.xsd targetNamespace="http://www.books.org" schemaLocation="http://www.books.org BookStore.xsd" - defines elements in namespace http://www.books.org - uses elements from namespace http://www.books.org A schema defines a new vocabulary. Instance documents use that new vocabulary.

16 10.03.200520.0 3.2003 IntroductionXML Schemas 16 BookStore.xmlBookStore.xsd XMLSchema.xsd (schema-for-schemas) Validate that the xml document conforms to the rules described in BookStore.xsd Validate that BookStore.xsd is a valid schema document, i.e., it conforms to the rules described in the schema-for-schemas  Multiple levels of checking

17 10.03.200520.0 3.2003 IntroductionXML Schemas 17 Part 2: Data Types

18 10.03.200520.0 3.2003 IntroductionXML Schemas 18 Elements, Attributes and attribute group declaration (1)  Element Declaration:  Attribute Declaration:

19 10.03.200520.0 3.2003 IntroductionXML Schemas 19 Use attributeValue attribute required- 8 optional- fixed2 default8 prohibited- Elements, Attributes and attribute group declaration (2)

20 10.03.200520.0 3.2003 IntroductionXML Schemas 20 Element Group

21 10.03.200520.0 3.2003 IntroductionXML Schemas 21 Attribute group

22 10.03.200520.0 3.2003 IntroductionXML Schemas 22 Data Types Simple Type or Complex Type –Use the simpleType element when the element does not contain attributes or child elements. –Use the complexType element when you want to define child elements and/or attributes of an element.  There are four kinds of complex elements: –empty elements –elements that contain only other elements –elements that contain only text –elements that contain both other elements and text Note: Each of these elements may contain attributes as well!

23 10.03.200520.0 3.2003 IntroductionXML Schemas 23 Simple Types: Predefined Types Similar to programming language

24 10.03.200520.0 3.2003 IntroductionXML Schemas 24 Simple type: examples 

25 10.03.200520.0 3.2003 IntroductionXML Schemas 25 Complex types: Constructors and occurrence indicators  Sequence and choice <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.binary.org" xmlns="http://www.binary.org" elementFormDefault="qualified"> DTD: XML Schema:

26 10.03.200520.0 3.2003 IntroductionXML Schemas 26 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> XML Schema: Problem: create an element, Book, which contains Author, Title, Date, ISBN, and Publisher, in any order (Note: this is very difficult and ugly with DTDs). Expressing Any Order

27 10.03.200520.0 3.2003 IntroductionXML Schemas 27 w The default value for minOccurs is "1“ w The default value for maxOccurs is "1" DTD * + ? MinOccurs XML SCHEMA MaxOccurs 0Unbounded 1 (default value)Unbounded 01 (default value) MinOccurs And MaxOccurs

28 10.03.200520.0 3.2003 IntroductionXML Schemas 28 Empty Complex Type <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.photography.org" xmlns="http://www.photography.org" elementFormDefault="qualified"> Schema: Instance Doc: DTD:

29 10.03.200520.0 3.2003 IntroductionXML Schemas 29 Type Attribute or ComplexType child element  An element declaration can have a type attribute, or a complexType child element, but it cannot have both a type attribute and a complexType child element. … ERROR

30 10.03.200520.0 3.2003 IntroductionXML Schemas 30 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> Referring Elements Declarations

31 10.03.200520.0 3.2003 IntroductionXML Schemas 31 Inlining Element Declarations <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> Note that we have moved all the element declarations inline, and we are no longer referring to the element declarations. This results in a much more compact schema!

32 10.03.200520.0 3.2003 IntroductionXML Schemas 32 Part 3: Type Derivation

33 10.03.200520.0 3.2003 IntroductionXML Schemas 33 Creating your own Datatypes  A new datatype can be defined from an existing datatype (called the "base" type) by specifying values for one or more of the optional facets for the base type.  Two techniques: Type restriction and Type extension.  We can restrict and extend simple and complex types.  Example. The integer data type has 8 optional facets: totalDigits pattern whitespace enumeration maxInclusive maxExclusive minInclusive minExclusive

34 10.03.200520.0 3.2003 IntroductionXML Schemas 34 Restriction of Simple Types (facet) This creates a new datatype called 'EarthSurfaceElevation'. Elements declared to be of this type can hold an integer. However, the integer is restricted to have a value between -1290 and 29035, inclusive.

35 10.03.200520.0 3.2003 IntroductionXML Schemas 35 … Facets: - length - minlength - maxlength - pattern - enumeration - minInclusive - maxInclusive - minExclusive - maxExclusive... Sources: - string - boolean - number - float - double - duration - dateTime - time... General Form of restricting simple types by Specifying Facet Values

36 10.03.200520.0 3.2003 IntroductionXML Schemas 36 Enumeration And Pattern facets This creates a new type called US-Flag-Colors. An element declared to be of this type must have either the value red, or white, or blue. This creates a new type called Emailadress. An element declared to be of this type must have the form string@string.

37 10.03.200520.0 3.2003 IntroductionXML Schemas 37 Restriction of Complex Types

38 10.03.200520.0 3.2003 IntroductionXML Schemas 38 Extension of Simple types Element with Simple Content (don’t contain other elements) and Attributes Example: 5440

39 10.03.200520.0 3.2003 IntroductionXML Schemas 39 Extension of Complex Types Title Author Date Publication ISBN Publisher BookPublication Add elements or attributes to a complex type

40 10.03.200520.0 3.2003 IntroductionXML Schemas 40 Example: Fixing a Facet Value Error! Cannot change the value of a fixed facet! Controlling Type Derivations

41 10.03.200520.0 3.2003 IntroductionXML Schemas 41 Prohibiting Derivations  Sometimes we may want to create a type and disallow all derivations of it, or just disallow extension derivations, or disallow restriction derivations. –Rationale: "For example, I may create a complexType and make it publicly available for others to use. However, I don't want them to extend it with their proprietary extensions or subset it to remove, say, copyright information." (Jon Cleaver) <xsd:complexType name="Publication" final="#all" Publication cannot be extended nor restricted <xsd:complexType name="Publication" final="restriction" Publication cannot be restricted <xsd:complexType name="Publication" final="extension" Publication cannot be extended

42 10.03.200520.0 3.2003 IntroductionXML Schemas 42 Type Derivation and Attributes

43 10.03.200520.0 3.2003 IntroductionXML Schemas 43 A Brief Summary

44 10.03.200520.0 3.2003 IntroductionXML Schemas 44 required optional prohibited xsd:string xsd:integer xsd:boolean... … 1 2 Declaring Attributes

45 10.03.200520.0 3.2003 IntroductionXML Schemas 45 1. Element with Simple Content. Declaring an element using a built-in type: Declaring an element using a user-defined simpleType: An alternative formulation of the above flag example is to inline the simpleType definition: Summary of Declaring Elements

46 10.03.200520.0 3.2003 IntroductionXML Schemas 46 Summary of Declaring Elements (cont.) 2. Element Contains Child Elements Defining the child elements inline: An alternate formulation of the above Person example is to create a named complexType and then use that type:

47 10.03.200520.0 3.2003 IntroductionXML Schemas 47 Summary of Declaring Elements (cont.) 3. Element Contains a complexType that is an Extension of another complexType

48 10.03.200520.0 3.2003 IntroductionXML Schemas 48 4. Element Contains a complexType that is a Restriction of another complexType Summary of Declaring Elements (cont.)

49 10.03.200520.0 3.2003 IntroductionXML Schemas 49 5. Element Contains Simple Content and Attributes Summary of Declaring Elements

50 10.03.200520.0 3.2003 IntroductionXML Schemas 50 Part 4: Advanced XML Schema Conception

51 10.03.200520.0 3.2003 IntroductionXML Schemas 51 Element Substitution  Oftentimes in daily conversation there are several ways to express something. –In Boston we use the words "T" and "subway" interchangeably. For example, "we took the T into town", or "we took the subway into town". Thus, "T" and "subway" are substitutable. Which one is used may depend upon what part of the state you live in, what mood you're in, or any number of factors.  We would like to be able to express this substitutability in XML Schemas. –That is, we would like to be able to declare in a schema an element called "subway", an element called "T", and state that "T"may be substituted for "subway". Instance documents can then use either or, depending on their preference.

52 10.03.200520.0 3.2003 IntroductionXML Schemas 52 Element Substitution (cont…) Schema: Instance doc: Alternative instance doc (substitute T for subway): This example shows the element being substituted with the element. Red Line Red Line

53 10.03.200520.0 3.2003 IntroductionXML Schemas 53 w An element may wish to block other elements from substituting with it. This is achieved by adding a block attribute. 1. Transitive: if element A can substitute for element B, and element B can substitute for element C, then element A can substitute for element C. A --> B --> C then A --> C 2. Non-symmetric: if element A can substitute for element B, it is not the case that element B can substitute for element A. Note: Element Substitution is: Blocking Element Substitution

54 10.03.200520.0 3.2003 IntroductionXML Schemas 54 Anonymous types and Named Types … … … … Anonymous Type Anonymous Type Named Type Named Type Named Type can be reused by other elements Named Type can be reused by other elements

55 10.03.200520.0 3.2003 IntroductionXML Schemas 55 <Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.book.org Book.xsd http://www.employee.org Employee.xsd"> My Life and Times Paul McCartney 1998 1-56592-235-2 Macmillan Publishing Illusions The Adventures of a Reluctant Messiah Richard Bach 1977 0-440-34319-4 Dell Publishing Co. The First and Last Freedom J. Krishnamurti 1954 0-06-064831-7 Harper & Row John Doe 123-45-6789 Sally Smith 000-11-2345 Library.xml Validating against two schemas The elements are defined in Book.xsd, and the elements are defined in Employee.xsd. 1. A schema validator will validate each Book element against Book.xsd. 2. It will validate each Employee element against Employee.xsd. Assembling an Instance Document from Multiple Schema Documents

56 10.03.200520.0 3.2003 IntroductionXML Schemas 56 … … 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  

57 10.03.200520.0 3.2003 IntroductionXML Schemas 57 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 A A.xsd Namespace B Namespace B B.xsd C.xsd <xsd:import namespace="A" schemaLocation="A.xsd"/> <xsd:import namespace="B" schemaLocation="B.xsd"/> … <xsd:import namespace="A" schemaLocation="A.xsd"/> <xsd:import namespace="B" schemaLocation="B.xsd"/> …

58 10.03.200520.0 3.2003 IntroductionXML Schemas 58 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.camera.org" xmlns:nikon="http://www.nikon.com" xmlns:olympus="http://www.olympus.com" xmlns:pentax="http://www.pentax.com" elementFormDefault="qualified"> <xsd:import namespace="http://www.nikon.com" schemaLocation="Nikon.xsd"/> <xsd:import namespace="http://www.olympus.com" schemaLocation="Olympus.xsd"/> <xsd:import namespace="http://www.pentax.com" schemaLocation="Pentax.xsd"/> Camera.xsd These import elements give us access to the components in these other schemas. Use of the body_type that is defined in the Nikon namespace Assembling a Schema from Multiple Schema Documents with Different Namespaces

59 10.03.200520.0 3.2003 IntroductionXML Schemas 59 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)

60 10.03.200520.0 3.2003 IntroductionXML Schemas 60 LibraryBook.xsd Library.xsd Redefining BookType to also have a Summary element. Redefining a Type from the Included Schema

61 10.03.200520.0 3.2003 IntroductionXML Schemas 61 Annotating Schemas  The element is used for documenting the schema, both for humans and for programs. –Use for providing a comment to humans –Use for providing a comment to programs The content is any well-formed XML  Note that annotations have no effect on schema validation The following constraint is not expressible with XML Schema: The value of element A should be greater than the value of element B. So, we need to use a separate tool (e.g., Schematron) to check this constraint. We will express this constraint in the appinfo section (below). A should be greater than B

62 10.03.200520.0 3.2003 IntroductionXML Schemas 62 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.

63 10.03.200520.0 3.2003 IntroductionXML Schemas 63 unique vs key  Key: an element or attribute (or combination) 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 Example: In the BookStore 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).

64 10.03.200520.0 3.2003 IntroductionXML Schemas 64... "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. Example 1

65 10.03.200520.0 3.2003 IntroductionXML Schemas 65 Example 2 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" xmlns:bk="http://www.books.org" elementFormDefault="qualified"> Note: ISBN is optional Require every ISBN be unique.

66 10.03.200520.0 3.2003 IntroductionXML Schemas 66 Part 5: Conclusions

67 10.03.200520.0 3.2003 IntroductionXML Schemas 67 Conclusions (1)  Namespaces, data types…make XML Shema very powerful w 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! w To check all our constraints we will need to supplement XML Schemas with another tool.

68 10.03.200520.0 3.2003 IntroductionXML Schemas 68 Conclusions (2) 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.

69 10.03.200520.0 3.2003 IntroductionXML Schemas 69 Part 6: References

70 10.03.200520.0 3.2003 IntroductionXML Schemas 70 XML 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 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 –MSXML4.0 http://www.microsoft.com  GUI Oriented –XML Spy http://www.xmlspy.com –Turbo XML http://www.extensibility.com

71 10.03.200520.0 3.2003 IntroductionXML Schemas 71 References  http://www.w3.org/TR/xmlschema-0/ (Primer)  http://www.w3.org/TR/xmlschema-1/ (Structures)  http://www.w3.org/TR/xmlschema-2/ (Datatypes)


Download ppt "Ceng 520 XML Schemas. 10.03.200520.0 3.2003 IntroductionXML Schemas 2 Part 0: Introduction Why XML Schema?"

Similar presentations


Ads by Google