Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course."— Presentation transcript:

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

2 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. However, there are some situations where it is illegal to use the same name. Where can the same name be used, and where will there be a name conflict? Here are the rules that regulate names: –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

3 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

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

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

6 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 25)

7 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?

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

9 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

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

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

12 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. Default namespace declaration

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

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

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

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

17 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 17 <bk:BookStore xmlns:bk="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org BookStore.xsd"> My Life and Times Paul McCartney 1998 94303-12021-43892 McMillin 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 Now we don’t use a default namespace declaration, and we qualify only the global element - BookStore. (see example 26)

18 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"

19 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 19 Recall the Camera Example Camera.xsd Nikon.xsd Olympus.xsd Pentax.xsd

20 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 20 Camera.xml This is what the instance document looks like when elementFormDefault="qualified" <camera xmlns="http://www.camera.org" xmlns:nikon="http://www.nikon.com" xmlns:olympus="http://www.olympus.com" xmlns:pentax="http://www.pentax.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.camera.org Camera.xsd> Ergonomically designed casing for easy handling 300mm 1.2 1/10,000 sec to 100 sec Ergonomically designed casing for easy handling 300mm 1.2 1/10,000 sec to 100 sec

21 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 21 <my:camera xmlns:my="http://www.camera.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.camera.org Camera.xsd"> Ergonomically designed casing for easy handling</descriptio 300mm 1.2 1/10,000 sec to 100 sec Instance document with namespaces hidden (localized) within the schema Ergonomically designed casing for easy handling 300mm 1.2 1/10,000 sec to 100 sec Instance document when elementFormDefault="unqualified" --> The fact that the element comes from the Nikon schema, the and elements come from the Olympus schema, and the element comes from the Pentax schema is totally transparent to the instance document. Instance Document

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

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

24 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 24 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="unqualified"> (see example 27) BookType extends PublicationType Publication is of type PublicationType (the base type) PublicationType is the base type Note this

25 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 25 <bk:BookStore xmlns:bk="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org BookStore.xsd"> Staying Young Forever Karin Granstrom Jordan, M.D. 1999 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 BookStore.xml (see example 27) This Publication’s content model is PublicationType This Publication’s content model is BookType This Publication’s content model is BookType

26 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 26 Illusions The Adventures of a Reluctant Messiah Richard Bach 1977 0-440-34319-4 Dell Publishing Co. "The Publication element is declared to be of type PublicationType. BookType is derived from PublicationType. Therefore, BookType is a PublicationType. Thus, the content of Publication can be a BookType. 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 BookType is a global type definition. By qualifying BookType (bk:BookType) we are asserting that BookType comes from the http://www.books.org namespace.

27 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 27 http://www.books.org Namespace PublicationType (T) BookType (T) Publication (E) T = Type E = Element A namespace consists of all of the global stuff in the schema - global elements, attributes and types!

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

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

30 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 30 Illusions The Adventures of a Reluctant Messiah Richard Bach 1977 0-440-34319-4 Dell Publishing Co. Schema: Instance doc: The PublicationType, and types derived from PublicationType may be substituted for the content model of Publication, e.g., BookType may be used.

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

32 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 32 Illusions The Adventures of a Reluctant Messiah Richard Bach 1977 0-440-34319-4 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 PublicationType, such as Publication, i.e., this is not allowed

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

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

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

36 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 36 <BookStore xmlns="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org BookStore.xsd"> Natural Health 1999 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 (see example 28) An XML Instance Document Conforming to BookStore.xsd

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

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

39 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 39 <BookStore xmlns="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org BookStore.xsd"> My Life and Times Paul McCartney 1998 94303-12021-43892 McMillin Publishing FooManchu Don Keyote 1951 (see example 29) 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.

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

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

42 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 42 LibraryBook.xsd (snippet, see example30) Library.xsd (snippet, see example30) Redefining BookType to also have a Summary element.

43 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 43 This element does two things: - it includes the components from LibraryBook.xsd - it redefines BookType (in LibraryBook.xsd) by extending it with a new element (Summary). Library.xsd (snippet, see example30)

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

45 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 45 Library.xsd (snippet, see example30) Because BookType has been redefined, Book's content now also includes Summary.

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

47 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 47 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 targetNamespace (just as it did when we d a no- namespace schema.

48 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 48 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> LibraryBook.xsd (see example31) Note that there is no targetNamespace!

49 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 49 Library.xsd (snippet, see example31) As soon as we redefine BookType.xsd it takes on the library targetNamespace.

50 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 50 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="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified" version="1.0">...

51 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 51 nil Content You can indicate in a schema that an element may be nil in the instance document. Empty content vs nil: –Empty: an element with an empty content is constrained to have no content. –nil: an instance document element may indicate no value is available by setting an attribute - xsi:nil - 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.

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

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

54 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 54 <Letter xmlns="http://www.letter.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.letter.org Letter.xsd"> Dear Sirs: This letter is to inform you that we are are finding your tool very useful. Letter.xml (see example36)

55 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 55 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.letter.org" xmlns="http://www.letter.org" elementFormDefault="qualified"> Letter.xsd (see example36)

56 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 56 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 base type for all elements which do not specify a type. –Example: This is the definition of the ur-type.

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

58 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 58 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 you can use the "anyType" No type specified so it defaults to ur-typeanyType is equivalent to ur-type

59 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 59 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. To date, all the schema-validators use the schemaLocation idiom.

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

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

62 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 62 Using ISBN as a Key When a book is published it has an ISBN, which is guaranteed to be unique. 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).

63 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 63 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> (see example32)

64 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 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. This is nice! We are using the content of a field as a key! (No longer limited to ID attributes for defining uniqueness.)

65 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 65 <BookStore xmlns="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org BookStore.xsd"> My Life and Times Paul McCartney 1998 94303-12021-43892 McMillin 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 (see example32) A schema-validator will verify that each Book has an ISBN element and that the values are all unique.

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

67 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 67 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.CostelloReunion.org" xmlns="http://www.CostelloReunion.org" elementFormDefault="qualified"> The key is the combination of the First and Last name. (See example33)

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

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

70 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 70 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> (see example 34) Note: ISBN is optional Require every ISBN be unique.

71 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 71 <BookStore xmlns="http://www.books.org/namespaces/BookStore" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org/namespaces/BookStore BookStore24.xsd"> My Life and Times Paul McCartney 1998 McMillin 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 (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!)

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

73 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 73 <Library xmlns="http://www.library.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.library.org AuthorSigningAtLibrary.xsd"> My Life and Times Paul McCartney 1998 94303-12021-43892 McMillin Publishing... Richard Bach Illusions The Adventures of a Reluctant Messiah 0-440-34319-4 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

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

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

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

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

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

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


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

Similar presentations


Ads by Google