1 / 97

Evolution of XML Schemas - A Guide for Beginners

Learn about the transition from DTDs to XML Schemas, highlighting enhanced datatypes, extensibility, object-oriented approach, and more. Follow a step-by-step example to convert a DTD to XML Schema notation for a simple Book Catalogue.

bbeecher
Download Presentation

Evolution of XML Schemas - A Guide for Beginners

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML Schemashttp://www.w3.org/TR/xmlschema-1/ http://www.w3.org/TR/xmlschema-2/ Roger L. Costello XML Technologies

  2. Thanks • Special thanks to the following people for their help in answering my endless barrage of questions: • Henry Thompson • Andrew Layman • Noah Mendelsohn • David Beech • Rick Jelliffe • Many thanks the following people who have carefully reviewed this tutorial and notified me of bugs, typos, etc. • Oliver Becker • Kit Lueder • David Wang

  3. Motivation • People are dissatisfied with DTDs • It's not XML • So, you write your XML document in one language and you specify the grammar of that document using another language (DTD) --> bad, inconsistent • Limited datatype capability • DTDs support a very limited capability for specifying datatypes. You can't, for example, say "I want this <elevation> element to be of datatype "int" • Desire a set of datatypes compatible with those found in databases • DTD supports 10 datatypes; XML Schemas supports 37+ datatypes

  4. Highlights of XML Schemas • XML Schemas are a tremendous advancement over DTDs: • Enhanced datatypes • 37+ versus 10 • Can create your own datatypes • Can define the lexical representation • Example "This element can contain strings of this form: ddd-dddd, where 'd' represents a 'digit'". • Written in XML • enables use of XML tools • Object-oriented • Can extend or restrict a type (derive new type definitions on the basis of old ones) • Can express sets - the child elements may occur in any order • Can specify element content as being unique (keys on content) and uniqueness within a region • Can define multiple elements with the same name but different content • Can define elements with null content • Can create equivalent elements - e.g., the "subway" element is equivalent to the "train" element.

  5. Problem • Convert BookCatalogue.dtd to the XML Schema notation • for this first example we will make a straight, one-to-one conversion, i.e., Title, Author, Date, ISBN, and Publisher will hold strings, just like is done in the DTD (next slide) • We will gradually modify the example to give more custom types to these elements

  6. BookCatalogue.dtd <!-- A book catalogue contains zero or more books --> <!ELEMENT BookCatalogue (Book)*> <!-- A Book has a Title, Author, Date, ISBN, and a Publisher --> <!ELEMENT Book (Title, Author, Date, ISBN, Publisher)> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (#PCDATA)> <!ELEMENT Date (#PCDATA)> <!ELEMENT ISBN (#PCDATA)> <!ELEMENT Publisher (#PCDATA)>

  7. <?xml version="1.0"?> <!DOCTYPE schema SYSTEM "xml-schema.dtd"[ <!ATTLIST schema xmlns:cat CDATA #IMPLIED> ]> <schema xmlns="http://www.w3.org/1999/XMLSchema" targetNamespace="http://www.somewhere.org/BookCatalogue" xmlns:cat="http://www.somewhere.org/BookCatalogue"> <element name="BookCatalogue"> <annotation> <info>A book catalogue contains zero or more books</info> </annotation> <type> <element ref="cat:Book" minOccurs="0" maxOccurs="*"/> </type> </element> <element name="Book"> <annotation> <info>A Book has a Title, Author, Date, ISBN, and a Publisher</info> </annotation> <type> <element ref="cat:Title"/> <element ref="cat:Author"/> <element ref="cat:Date"/> <element ref="cat:ISBN"/> <element ref="cat:Publisher"/> </type> </element> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </schema> (explanations on succeeding pages) BookCatalogue1.xsd xsd = Xml-Schema Definition

  8. <?xml version="1.0"?> <!DOCTYPE schema SYSTEM "xml-schema.dtd"[ <!ATTLIST schema xmlns:cat CDATA #IMPLIED> ]> <schema xmlns="http://www.w3.org/1999/XMLSchema" targetNamespace="http://www.somewhere.org/BookCatalogue" xmlns:cat="http://www.somewhere.org/BookCatalogue"> <element name="BookCatalogue"> <annotation> <info>A book catalogue contains zero or more books</info> </annotation> <type> <element ref="cat:Book" minOccurs="0" maxOccurs="*"/> </type> </element> <element name="Book"> <annotation> <info>A Book has a Title, Author, Date, ISBN, and a Publisher</info> </annotation> <type> <element ref="cat:Title"/> <element ref="cat:Author"/> <element ref="cat:Date"/> <element ref="cat:ISBN"/> <element ref="cat:Publisher"/> </type> </element> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </schema> This (default) namespace declaration says that all these elements come from this namespace (the XML Schema namespace). By virtue of the fact that they are associated with an element that comes from the XML Schema namespace, these attributes also come from the XML Schema namespace. (Recall that a default namespace doesn’t apply to attributes.) BookCatalogue1.xsd

  9. <?xml version="1.0"?> <!DOCTYPE schema SYSTEM "xml-schema.dtd"[ <!ATTLIST schema xmlns:cat CDATA #IMPLIED> ]> <schema xmlns="http://www.w3.org/1999/XMLSchema" targetNamespace="http://www.somewhere.org/BookCatalogue" xmlns:cat="http://www.somewhere.org/BookCatalogue"> <element name="BookCatalogue"> <annotation> <info>A book catalogue contains zero or more books</info> </annotation> <type> <element ref="cat:Book" minOccurs="0" maxOccurs="*"/> </type> </element> <element name="Book"> <annotation> <info>A Book has a Title, Author, Date, ISBN, and a Publisher</info> </annotation> <type> <element ref="cat:Title"/> <element ref="cat:Author"/> <element ref="cat:Date"/> <element ref="cat:ISBN"/> <element ref="cat:Publisher"/> </type> </element> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </schema> Says that the elements and types defined in this schema are in this namespace. This will be used in instance documents to indicate that the elements contained in the instance document come from this namespace. BookCatalogue1.xsd

  10. <?xml version="1.0"?> <!DOCTYPE schema SYSTEM "xml-schema.dtd"[ <!ATTLIST schema xmlns:cat CDATA #IMPLIED> ]> <schema xmlns="http://www.w3.org/1999/XMLSchema" targetNamespace="http://www.somewhere.org/BookCatalogue" xmlns:cat="http://www.somewhere.org/BookCatalogue"> <element name="BookCatalogue"> <annotation> <info>A book catalogue contains zero or more books</info> </annotation> <type> <element ref="cat:Book" minOccurs="0" maxOccurs="*"/> </type> </element> <element name="Book"> <annotation> <info>A Book has a Title, Author, Date, ISBN, and a Publisher</info> </annotation> <type> <element ref="cat:Title"/> <element ref="cat:Author"/> <element ref="cat:Date"/> <element ref="cat:ISBN"/> <element ref="cat:Publisher"/> </type> </element> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </schema> Here we are referencing a Book element. Where is that Book element defined? In what namespace? The cat: prefix indicates what namespace this element is defined in. cat: has been set to be the same as the targetNamespace. BookCatalogue1.xsd

  11. xmlns:cat • The schema element has an attribute xmlns:cat. The cat: namespace prefix, as we noted, is used for one element referencing another element within the schema. • Clearly, this attribute will be schema-dependent, e.g., if I write a schema for cars I might have xmlns:car. • Obviously, there is no way for xml-schema.dtd to account for all the possibilities. Thus, in the schema we supplement xml-schema.dtd with the internal subset declaration: <!DOCTYPE schema SYSTEM "xml-schema.dtd"[ <!ATTLIST schema xmlns:cat CDATA #IMPLIED> ]>

  12. <?xml version="1.0"?> <!DOCTYPE schema SYSTEM "xml-schema.dtd"[ <!ATTLIST schema xmlns:cat CDATA #IMPLIED> ]> <schema xmlns="http://www.w3.org/1999/XMLSchema" targetNamespace="http://www.somewhere.org/BookCatalogue" xmlns:cat="http://www.somewhere.org/BookCatalogue"> <element name="BookCatalogue"> <annotation> <info>A book catalogue contains zero or more books</info> </annotation> <type> <element ref="cat:Book" minOccurs="0" maxOccurs="*"/> </type> </element> <element name="Book"> <annotation> <info>A Book has a Title, Author, Date, ISBN, and a Publisher</info> </annotation> <type> <element ref="cat:Title"/> <element ref="cat:Author"/> <element ref="cat:Date"/> <element ref="cat:ISBN"/> <element ref="cat:Publisher"/> </type> </element> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </schema> The annotation/ info elements are optional. Their content is intended for human consumption, i.e., it's a comment. BookCatalogue1.xsd

  13. Referencing a schema in an XML instance document <?xml version="1.0"?> <BookCatalogue xmlns ="http://www.somewhere.org/BookCatalogue" xmlns:xsi="http://www.w3.org/1999/XMLSchema/instance" xsi:schemaLocation="http://www.somewhere.org/BookCatalogue http://www.somewhere.org/BookCatalogue/BookCatalogue1.xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>July, 1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> ... </BookCatalogue> In the BookCatalogue element (the root element) I declare that the schemaLocation attribute comes from the XML Schema Instance namespace (xsi). The value of schemaLocation is a pair of values - a namespace and the URI to a schema. When the XML parser processes this XML document it will use the schemaLocation pair of values to determine the XML Schema that it conforms to. It will retrieve the schema at the URI specified in schemaLocation (in this example, BookCatalogue.xsd) and then it will open up this schema document to confirm that its targetNamespace value matches the namespace value shown in schemaLocation. In this case it does. I declare (using a default namespace) that all the elements in this XML instance document comes from the same namespace.

  14. Referencing a schema in an XML instance document targetNamespace="A" schemaLocation="A A/BookCatalogue.xsd" BookCatalogue.xsd BookCatalogue.xml - uses elements from namespace A - defines elements for namespace A

  15. 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) • For this tutorial I will assume that we are using an XML Parser which uses the schemaLocation idiom.

  16. Note multiple levels of checking BookCatalogue.xml BookCatalogue1.xsd xml-schema.dtd Does the xml document conform to the rules laid out in the xml-schema? Is the xml-schema a valid xml document, i.e., does it conform to the rules laid out in the xml-schema DTD? Do Lab1

  17. Alternate Schema • On the following slide is an alternate (equivalent) way of representing the schema shown previously.

  18. <?xml version="1.0"?> <!DOCTYPE schema SYSTEM "xml-schema.dtd"[ <!ATTLIST schema xmlns:cat CDATA #IMPLIED> ]> <schema xmlns="http://www.w3.org/1999/XMLSchema" targetNamespace="http://www.somewhere.org/BookCatalogue" xmlns:cat="http://www.somewhere.org/BookCatalogue"> <element name="BookCatalogue"> <type> <element name="Book" minOccurs="0" maxOccurs="*"> <type> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </type> </element> </type> </element> </schema> BookCatalogue2.xsd

  19. <?xml version="1.0"?> <!DOCTYPE schema SYSTEM "xml-schema.dtd"[ <!ATTLIST schema xmlns:cat CDATA #IMPLIED> ]> <schema xmlns="http://www.w3.org/1999/XMLSchema" targetNamespace="http://www.somewhere.org/BookCatalogue" xmlns:cat="http://www.somewhere.org/BookCatalogue"> <element name="BookCatalogue"> <type> <element name="Book" minOccurs="0" maxOccurs="*"> <type> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </type> </element> </type> </element> </schema> Anonymous type (no name) Do Lab 2 BookCatalogue2.xsd

  20. Named Types • The following slide shows an alternate (equivalent) schema which uses named types.

  21. <?xml version="1.0"?> <!DOCTYPE schema SYSTEM "xml-schema.dtd"[ <!ATTLIST schema xmlns:cat CDATA #IMPLIED> ]> <schema xmlns="http://www.w3.org/1999/XMLSchema" targetNamespace="http://www.somewhere.org/BookCatalogue" xmlns:cat="http://www.somewhere.org/BookCatalogue"> <element name="BookCatalogue"> <type> <element name="Book" minOccurs="0" maxOccurs="*" type="cat:CardCatalogueEntry"/> </type> </element> <type name="CardCatalogueEntry"> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </type> </schema> BookCatalogue3.xsd

  22. Problem • Defining the Date element to be of type string is unsatisfactory (it allows any string value to be input as the content of the Date element, including non-date strings). We would like to constrain the allowable content that Date can have. Modify the XML-Schema to restrict the content of the Date element to just date values. • Similarly, constrain the content of the ISBN element to content of this form: ddddd-ddddd-ddddd or d-ddd-ddddd-d or d-dd-dddddd-d, where 'd' stands for 'digit'

  23. <?xml version="1.0"?> <!DOCTYPE schema SYSTEM "xml-schema.dtd"[ <!ATTLIST schema xmlns:cat CDATA #IMPLIED> ]> <schema xmlns="http://www.w3.org/1999/XMLSchema" targetNamespace="http://www.somewhere.org/BookCatalogue" xmlns:cat="http://www.somewhere.org/BookCatalogue"> <datatype name="ISBNType" source="string"> <pattern value="\d{5}-\d{5}-\d{5}"/> <pattern value="\d{1}-\d{3}-\d{5}-\d{1}"/> <pattern value="\d{1}-\d{2}-\d{6}-\d{1}"/> </datatype> <element name="BookCatalogue"> <type> <element name="Book" minOccurs="0" maxOccurs="*"> <type> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="date"/> <element name="ISBN" type="cat:ISBNType"/> <element name="Publisher" type="string"/> </type> </element> </type> </element> </schema> BookCatalogue4.xsd

  24. <datatype name="ISBNtype" source="string"> <pattern value="\d{5}-\d{5}-\d{5}"/> <pattern value="\d{1}-\d{3}-\d{5}-\d{1}"/> <pattern value="\d{1}-\d{2}-\d{6}-\d{1}"/> </datatype> "Elements of this datatype will have string content. The string must conform to one of the three patterns listed. - The first pattern is: 5 digits followed by a dash followed by 5 digits followed by another dash followed by 5 more digits. - The second pattern is: 1 digit followed by a dash followed by 3 digits followed by another dash followed by 5 digits followed by another dash followed by 1 more digit. - The third pattern is: 1 digit followed by a dash followed by 2 digits followed by another dash followed by 6 digits followed by another dash followed by 1 more digit." These patterns are specified using Regular Expressions. In a few slides we will see more of the Regular Expression syntax.

  25. Primitive Datatypes string boolean float double decimal timeInstant timeDuration recurringInstant binary uri Atomic, built-in "Hello World" {true, false} 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN 7.08 1999-12-04T20:12:00.000 1Y2M3DT10H30M12.3S same format as timeInstant 010010111001 http://www.somewhere.org Built-in Datatypes Note: 'T' is the date/time separator INF = infinity NAN = not-a-number

  26. Generated datatypes language NMTOKEN NMTOKENS Name NCName QNAME ID IDREF IDREFS ENTITY ENTITIES NOTATION integer non-negative-integer positive-integer non-positive-integer negative-integer date time Subtype of primitive datatype any valid xml:lang value, e.g., EN, FR, ... "house" "house barn yard" "hello-there" part (no namespace qualifier) book:part Token, must be unique Token which matches an ID List of IDREF 456 zero to infinity one to infinity negative infinity to zero negative infinity to negative one 1999-05-21 13:20:00.000 Built-in Datatypes (cont.) Do Lab 3

  27. Creating your own Datatypes • We can create new datatypes from existing datatypes (called source or basetype) by specifying values for one or more of the optional facets • Example. The string primitive datatype has nine optional facets - pattern, enumeration, length, maxlength, maxInclusive, maxExclusive, minlength, minInclusive and minExclusive.

  28. Example <datatype name="TelephoneNumber" source="string"> <length value="8"/> <pattern value="\d{3}-\d{4}"/> </datatype> This creates a new datatype called 'TelephoneNumber'. Elements of this type can hold string values, but the string length must be exactly 8 characters long and the string must follow the pattern: ddd-dddd, where 'd' represents a 'digit'.

  29. Facets of the Integer Datatype • Facets: • maxInclusive • maxExclusive • minInclusive • minExclusive

  30. Example <datatype name= "EarthSurfaceElevation" source="integer"> <minInclusive value="-1246"/> <maxInclusive value="29028"/> </datatype> This creates a new datatype called 'EarthSurfaceElevation'. Elements of this type can hold an integer. However, the integer must have a value between -1246 and 29028, inclusive.

  31. General Form of Datatype/Facet Usage <datatype name= "name" source= "source"> <facet value= "value"/> <facet value= "value"/> ... </datatype> Facets: - minInclusive - maxInclusive - minExclusive - maxExclusive - length - minlength - maxlength - pattern - enumeration ... • Sources: • - string • - boolean • - float • - double • - decimal • - timeInstant • - timeDuration • - recurringInstant • - binary • - uri • ... Do Lab 4

  32. Regular Expressions • Recall that the string datatype has a pattern facet. The value of a pattern facet is a regular expression. Below are some examples of regular expressions: Regular Expression - Chapter \d - a*b - [xyz]b - a?b - a+b - [a-c]x Example - Chapter 1 - b, ab, aab, aaab, … - xb, yb, zb - b, ab - ab, aab, aaab, … - ax, bx, cx

  33. Regular Expression [a-c]x [-ac]x [ac-]x [^0-9]x \Dx Chapter\s\d (ho){2} there (ho\s){2} there .abc (a|b)+x Example ax, bx, cx -x, ax, cx ax, cx, -x any non-digit char followed by x any non-digit char followed by x Chapter followed by a blank followed by a digit hoho there ho ho there any char followed by abc ax, bx, aax, bbx, abx, bax,... Regular Expressions (cont.)

  34. a{1,3}x a{2,}x \w\s\w ax, aax, aaax aax, aaax, aaaax, … word (alphanumeric plus dash) followed by a space followed by a word Regular Expressions (cont.)

  35. Example R.E. [1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5] 0 to 99 200 to 249 250 to 255 100 to 199 This regular expression restricts a string to have values between 0 and 255. … Such a R.E. might be useful in describing an IP address ...

  36. IP Datatype Definition <datatype name="IP" source="string"> <pattern value="(([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3} ([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"> <annotation> <info> Datatype for representing IP addresses. Examples, 129.83.64.255, 64.128.2.71, etc. This datatype restricts each field of the IP address to have a value between zero and 255, i.e., [0-255].[0-255].[0-255].[0-255] Note: in the value attribute (above) the regular expression has been split over two lines. This is for readability purposes only. In practice the R.E. would all be on one line. </info> </annotation> </pattern> </datatype>

  37. Regular Expression Parser • Want to test your skill in writing regular expressions? Go to: http://www.xfront.org/xml-schema/ • Dan Potter has created a nice tool which allows you to enter a regular expression and then enter a string. The parser will then determine if your string conforms to your regular expression. Do Lab 5

  38. Derived Types • We can do a form of subclassing the type definitions. We call this "derived types" • derive by extension: extend the parent type with more elements • derive by restriction: constrain the parent type by constraining some of the elements to have a more restricted range of values, or a more restricted number of occurrences.

  39. <?xml version="1.0"?> <!DOCTYPE schema SYSTEM "xml-schema.dtd"[ <!ATTLIST schema xmlns:cat CDATA #IMPLIED> ]> <schema xmlns="http://www.w3.org/1999/XMLSchema" targetNamespace="http://www.somewhere.org/BookCatalogue" xmlns:cat="http://www.somewhere.org/BookCatalogue"> <type name="Publication"> <element name="Title" type="string" maxOccurs="*"/> <element name="Author" type="string" maxOccurs="*"/> <element name="Date" type="date"/> </type> <type name="Book" source="cat:Publication" derivedBy="extension"> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </type> <element name="BookCatalogue"> <type> <element name="CatalogueEntry" minOccurs="0" maxOccurs="*" type="cat:Book"/> </type> </element> </schema> BookCatalogue5.xsd

  40. <type name="Publication"> <element name="Title" type="string" maxOccurs="*"/> <element name="Author" type="string" maxOccurs="*"/> <element name="Date" type="date"/> </type> <type name="Book" source="cat:Publication" derivedBy="extension"> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </type> Elements of type Book will have 5 child elements - Title, Author, Date, ISBN, and Publisher.

  41. Using Derived Types • If we declare an element to be of type Publication then in the XML instance document that element's content can be either a Publication or a Book (since Book is a Publication).

  42. <?xml version="1.0"?> <!DOCTYPE schema SYSTEM "xml-schema.dtd"[ <!ATTLIST schema xmlns:cat CDATA #IMPLIED> ]> <schema xmlns="http://www.w3.org/1999/XMLSchema" targetNamespace="http://www.somewhere.org/Catalogue" xmlns:cat="http://www.somewhere.org/Catalogue"> <type name="Publication"> <element name="Title" type="string" maxOccurs="*"/> <element name="Author" type="string" maxOccurs="*"/> <element name="Date" type="date"/> </type> <type name="Book" source="cat:Publication" derivedBy="extension"> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </type> <element name="Catalogue"> <type> <element name="CatalogueEntry" minOccurs="0" maxOccurs="*" type="cat:Publication"/> </type> </element> </schema> BookCatalogue6.xsd

  43. <?xml version="1.0"?> <Catalogue xmlns ="http://www.somewhere.org/Catalogue" xmlns:xsi="http://www.w3.org/1999/XMLSchema/instance" xsi:schemaLocation="http://www.somewhere.org/Catalogue http://www.somewhere.org/Catalogue/BookCatalogue6.xsd"> <CatalogueEntry> <Title>Staying Young Forever</Title> <Author>Karin Granstrom Jordan, M.D.</Author> <Date>December, 1999</Date> </CatalogueEntry> <CatalogueEntry xsi:type="Book"> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </CatalogueEntry> <CatalogueEntry xsi:type="Book"> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0-06-064831-7</ISBN> <Publisher>Harper &amp; Row</Publisher> </CatalogueEntry> </Catalogue> BookCatalogue2.xml

  44. <CatalogueEntry xsi:type="Book"> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </CatalogueEntry> "The CatalogueEntry element is declared to be of type Publication. Book is derived from Publication. Therefore, Book is a Publication. Thus, the content of CatalogueEntry 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."

  45. Why is xsi:type Needed? • Why, in an instance document, do we need to indicate the derived type being used? • Suppose that there are several types derived (by extension) from Publication, and some of the extended element declarations are optional. In the instance document, if xsi:type was not used, it might get very difficult for an XML Parser to determine which derived type is being used. Do Lab 6

  46. Derive by Restriction <type name="Publication"> <element name="Title" type="string" maxOccurs="*"/> <element name="Author" type="string" maxOccurs="*"/> <element name="Date" type="date"/> </type> <type name= "SingleAuthorPublication" source="cat:Publication" derivedBy= "restriction"> <restrictions> <element name="Author" type="string" maxOccurs="1"/> </restrictions> </type> Elements of type SingleAuthorPublication will have 3 child elements - Title, Author, and Date. There must be exactly one Author element.

  47. Restricting Derivations • Sometimes we may want to create a type and disallow all derivations of it, or just disallow extensions of it, or restrictions of it. <type name="Publication" final="#all" …> This type cannot be extended nor restricted <type name="Publication" final="restriction" …> This type cannot be restricted <type name="Publication" final="extended" …> This type cannot be extended

  48. exact Types • If you define a type to be exact, then other types may derive from it. However, in the instance document derived types may not be used in its stead.

  49. <type name="Publication"> <element name="Title" type="string" maxOccurs="*"/> <element name="Author" type="string" maxOccurs="*"/> <element name="Date" type="date"/> </type> <type name="Book" source="cat:Publication" derivedBy="extension"> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </type> <element name="Catalogue"> <type> <element name="CatalogueEntry" minOccurs="0" maxOccurs="*" type="cat:Publication"/> </type> </element> Schema: This permits Publication elements, as well as types derived from Publication to be used as a child of Catalogue, e.g., Book <CatalogueEntry xsi:type="Book"> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </CatalogueEntry> Instance doc:

  50. <type name="Publication" exact="extension"> <element name="Title" type="string" maxOccurs="*"/> <element name="Author" type="string" maxOccurs="*"/> <element name="Date" type="date"/> </type> <type name="Book" source="cat:Publication" derivedBy="extension"> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </type> <element name="Catalogue"> <type> <element name="CatalogueEntry" minOccurs="0" maxOccurs="*" type="cat:Publication"/> </type> </element> Schema: This prohibits the use of types derived by extension to be used as children of CatalogueEntry, e.g., this is not allowed <CatalogueEntry xsi:type="Book"> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </CatalogueEntry> Instance doc:

More Related