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

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

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

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 indicating 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 Publishing name- space.

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.publishing.org" xmlns="http://www.publishing.org" elementFormDefault="unqualified"> (see example 23) Notice that there is only one global element in the schema

17 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 17 <b:BookCatalogue xmlns:b="http://www.publishing.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.publishing.org BookCatalogue.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 - BookCatalogue. (see example 19)

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

20 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).

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

22 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 22 <cat:Catalogue xmlns:cat ="http://www.publishing.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.publishing.org BookCatalogue.xsd"> Staying Young Forever Karin Granstrom Jordan, M.D. December, 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 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

23 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 23 Illusions The Adventures of a Reluctant Messiah Richard Bach 1977 0-440-34319-4 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.

24 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

25 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!

26 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

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

28 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 28 Illusions The Adventures of a Reluctant Messiah Richard Bach 1977 0-440-34319-4 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.

29 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 29 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 Publication from being used as the content model of Listing, i.e.., this is not allowed

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

31 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

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

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

34 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 34 <Catalogue xmlns="http://www.publishing.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.publishing.org Catalogue.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 25) An XML Instance Document Conforming to Catalogue.xsd

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

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

37 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 37 <BookCatalogue xmlns="http://www.publishing.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.publishing.org BookCatalogue.xsd"> My Life and Times Paul McCartney 1998 94303-12021-43892 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.

38 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

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

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

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

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

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

44 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.”

45 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!

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

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

48 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!

49 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="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.publishing.org" xmlns="http://www.publishing.org" elementFormDefault="qualified" version="1.0">...

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

51 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:

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

53 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

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

55 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

56 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

57 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).

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

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

60 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 60 <BookCatalogue xmlns="http://www.publishing.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.publishing.org BookCatalogue.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 example 29) A schema-validator will verify that each Book has an ISBN element and that the values are all unique.

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

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

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

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

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

66 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 66 <BookCatalogue xmlns="http://www.publishing.org/namespaces/BookCatalogue" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.publishing.org/namespaces/BookCatalogue BookCatalogue24.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!)

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

68 Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 68 <Library xmlns="http://www.library.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.library.org AuthorSigningAtLibrary27.xsd">... Illusions The Adventures of a Reluctant Messiah Richard Bach 1977 0-440-34319-4 Dell Publishing Co.... 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 BookCatalogue. A key element A keyref element

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

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

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

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

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

74 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

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

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

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

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


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