1 / 47

XML Schema – Part 2

XML Schema – Part 2. More on Schema Types & Derivation Abstact types & type substitution Uniqueness & Keys Additional schema mechanisms - include & import - open content Comparison with DTD & other tech. Content Types.

dustin-wise
Download Presentation

XML Schema – Part 2

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 Schema – Part 2 • More on SchemaTypes & Derivation • Abstact types & type substitution • Uniqueness & Keys • Additional schema mechanisms - include & import - open content • Comparison with DTD & other tech.

  2. Content Types • The type hierarchy first branches into two groups: simple types and complex types. • Complex types are divided into two groups: those with simple content and those with complex content. • While both forms of complex type allow attributes, only those with complex content allow child elements; those with simple content only allow character content.

  3. <xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author"type="xsd:string"maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:complexType > <xsd:complexType name="BookPublication"> <xsd:complexContent> <xsd:extension base="Publication"> <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType > <xsd:element name="Book" type="BookPublication" maxOccurs="unbounded"/>

  4. Content Type • As we saw there are four kinds of derivation: restriction, extension, list, and union. • All types derive, directly or indirectly, from the root type. The root type is anyType. • The default syntax for complex types is complex content that restricts anyType.

  5. Schema Types

  6. anyType • The anyType is the base type for all types which do not specify a value for the base attribute. It is the base type for all elements which do not specify a type. • Example: <xsd:element name="foo"/> <xsd:complexType name="xsd: anyType" mixed="true"> <xsd:sequence> <xsd:any minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> <xsd:anyAttribute/> </xsd:complexType> This is the definition of the anyType.

  7. Complex Type • <xsd:element name="book"> • <xsd:complexType> • <xsd:sequence> • <xsd:element name="title" type="xsd:string"/> • <xsd:element name=“author" type="xsd:date“/> • </xsd:sequence> • </xsd:complexType> • </xsd:element> • <xsd:element name="book"> • <complexContent> • <restriction base="anyType"> • <xsd:complexType> • <xsd:sequence> • <xsd:element name="title" type="xsd:string"/> • <xsd:element name=“author" type="xsd:date“/> • </xsd:sequence> • </xsd:complexType> • </restriction> • </complexContent> • </xsd:element>

  8. Empty Element Your first inclination might be to associate the empty element with a simple type. But that won't work since simple types allow data content. So it must be a complex type. The, ask yourself the next question. Will it allow element children? No. We need a <complexType> with <simpleContent>, right? Wrong. Complex types with simple content also allow data content, and we want an empty element. That leaves us with <complexType> with <complexContent>, which ensures that there will not be any data content in the element. But we don't want child elements, either, and a complex type with complex content allows child elements. The key is that it doesn't require them. Simply leave the content model out of the type definition: • <complexType name=“emptyElement"> • <complexContent> • <restriction base="anyType"> • </restriction> • </complexContent> • </complexType>

  9. Empty Element <!ELEMENT image EMPTY> <!ATTLIST image href CDATA #REQUIRED> DTD: • <xsd:element name="image"> • <xsd:complexType> • <xsd:attribute name="href" • type="xsd:anyURI" use="required"/> • </xsd:complexType> • </xsd:element> Schema: Instance doc (snippet): <image href="http://www.xfront.com/InSubway.gif"/>

  10. Type Substitutability • As we saw earlier, substitutionGroup gives us "element substitution", i.e., the ability to substitute one element for another. Now we will see how to achieve "type substitution", i.e., the ability to substitute an element’s content with another content. • Here’s the principle of type substitutability: A base type can be substituted by any derived type. • Example. Suppose that BookType is derived from PublicationType. If we declare an element, Publication, to be of type PublicationType (the base type) then in the instance document Publication's content can be either a PublicationType or a BookType.

  11. <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="unqualified"> <xsd:complexType name="PublicationType"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:year"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="BookType"> <xsd:complexContent> <xsd:extension base="PublicationType"> <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Publication" maxOccurs="unbounded" type="PublicationType"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> PublicationType is the base type BookType extends PublicationType Publication is of type PublicationType (the base type)

  12. <?xml version="1.0"?> <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"> <Publication> <Title>Staying Young Forever</Title> <Author>Karin Granstrom Jordan, M.D.</Author> <Date>1999</Date> </Publication> <Publication xsi:type="bk:BookType"> <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> </Publication> <Publication xsi:type="bk:BookType"> <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> </Publication> </bk:BookStore> This Publication’s content model is PublicationType This Publication’s content model is BookType This Publication’s content model is BookType BookStore.xml

  13. Abstract Elements • The head element must be global. • Same type as the head or derived within substitution group • Used in place of the head element. • Generic head element, • Shouldn't be used directly, but in one of its derived forms. • Declare head as abstract. • Analogous to abstract classes in O/O. • <xs:element name="name-elt" type="xs:string" abstract="true"/> • <xs:element name="name" type="xs:string" substitutionGroup="name-elt"/> <xs:element name="surname" type="xs:string" substitutionGroup="name-elt"/> • This example defines name-elt as an abstract element that should be replaced either by name or surname everywhere it is referenced.

  14. Abstract complexType • You can declare a complexType to be abstract • Example.<xsd:complexType name="PublicationType" abstract="true"/> • An abstract complexType is a template/placeholder type: • If an element is declared to be a type that is abstract then in an XML instance document the content model of that element may not be that of the abstract type. • Example. An element declared to be of type PublicationType (shown above) may not have that type’s content model. • However, complexType’s that are derived from the abstract type may substitute for the abstract type.

  15. Note that PublicationType is declared abstract. <xsd:complexType name="PublicationType" abstract="true"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:year"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="BookType"> <xsd:complexContent> <xsd:extension base="PublicationType"> <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="SingleAuthorPublication"> <xsd:complexContent> <xsd:restriction base="PublicationType"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:year"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent> </xsd:complexType> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded" type="PublicationType"/> </xsd:sequence> </xsd:complexType> </xsd:element> Book derives from PublicationType. By default abstract="false". Thus, this type can substitute for the PublicationType.

  16. <?xml version="1.0"?> <BookStore xmlns="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org BookStore.xsd"> <Book xsi:type="BookType"> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> <Book xsi:type="SingleAuthorPublication"> <Title>FooManchu</Title> <Author>Don Keyote</Author> <Date>1951</Date> </Book> </BookStore> The content model of each <Book> element must be from a type that derives from PublicationType. In the schema there are two such types - BookType and SingleAuthorPublication.

  17. Review of Abstract Elements and Abstract complexTypes • If you declare an element to be abstract • - -> Use element substitution for the abstract element (as provided by substitutionGroup) • If you declare a complexType to be abstract • - -> Use type substitution for the abstract type (as provided by type derivation)

  18. Uniqueness & Keys • DTDs provide the ID attribute datatype for uniqueness (i.e., an ID value must be unique throughout the entire document, and the XML parser enforces this). • XML Schema has much enhanced uniqueness capabilities: • enables you to define element content to be unique. • enables you to define non-ID attributes to be unique. • enables you to define a combination of element content and attributes to be unique. • enables you to distinguish between unique versus key. • enables you to declare the range of the document over which something is unique

  19. unique vs key • Key: an element or attribute (or combination thereof) which is defined to be a key must: • always be present (minOccurs must be greater than zero) • be non-nillable (i.e., nillable="false") • be unique • Key implies unique, but unique does not imply key

  20. <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" xmlns:bk="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:key name="PK"> <xsd:selector xpath="bk:Book"/> <xsd:field xpath="bk:ISBN"/> </xsd:key> </xsd:element> </xsd:schema>

  21. <xsd:element name="BookStore"> ... <xsd:key name="PK"> <xsd:selector xpath="bk:Book"/> <xsd:field xpath="bk:ISBN"/> </xsd:key> </xsd:element> "Within <BookStore> we define a key, called PK. Select each <Book>, and within each <Book> the ISBN element is a key." In other words, within <BookStore> each <Book> must have an <ISBN> and it must be unique.

  22. <?xml version="1.0"?> <BookStore xmlns="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org BookStore.xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>1-56592-235-2</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> <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> </Book> <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> </Book> </BookStore> A schema-validator will verify that each Book has an ISBN element and that the values are all unique.

  23. Notes about <key> • It must be nested within an <element> • It must come at the end of <element> (after the content model, and attribute declarations) • Use the <selector> element as a child of <key> to select a set of elements for which the key applies. • Use the <field> element as a child of <key> to identify the element or attribute that is to be the key • There can be multiple <field> elements.

  24. unique • The <unique> element is used exactly like the <key> element is used. It has a <selector> and one or more <field> elements, just like <key> has. • The only difference is that the schema validator will simply validate that, whenever present, the values are unique.

  25. <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" xmlns:bk="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string" minOccurs="0"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:unique name="UNIQ"> <xsd:selector xpath="bk:Book"/> <xsd:field xpath="bk:ISBN"/> </xsd:unique> </xsd:element> </xsd:schema> Note: ISBN is optional Require every ISBN be unique.

  26. Referencing a key • Recall that by declaring an element of type IDREF then that element must reference an ID attribute, and an XML Parser will verify that the IDREF value corresponds to a legitimate ID value. • Similarly, you can define a keyref which asserts, "the value of this element must match the value of an element referred to by this".

  27. <?xml version="1.0"?> <Library xmlns="http://www.library.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.library.org AuthorSigningAtLibrary.xsd"> <Books> <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> </Book> ... </Books> <GuestAuthors> <Author> <Name>Richard Bach</Name> <BookForSigning> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <ISBN>0-440-34319-4</ISBN> </BookForSigning> </Author> </GuestAuthors> </Library> A key element 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 keyref element

  28. <xsd:element name="Library"> <xsd:complexType> <xsd:sequence> <xsd:element name="Books"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element ref="GuestAuthors"/> </xsd:sequence> </xsd:complexType> <xsd:key name="PK"> <xsd:selector xpath="bk:Books/bk:Book"/> <xsd:field xpath="bk:ISBN"/> </xsd:key> <xsd:keyref name="isbnRef" refer="PK"> <xsd:selector xpath="bk:GuestAuthors/bk:Author/bk:BookForSigning"/> <xsd:field xpath="bk:ISBN"/> </xsd:keyref> </xsd:element> AuthorSigningAtLibrary.xsd

  29. <xsd:key name="PK"> <xsd:selector xpath="bk:BookStore/bk:Book"/> <xsd:field xpath="bk:ISBN"/> </xsd:key> This tells the schema-validator to validate that every Book (in BookStore) has an ISBN, and that ISBN must be unique. <xsd:keyref name="isbnRef" refer="PK"> <xsd:selector xpath="bk:GuestAuthors/bk:Author/bk:BookForSigning"/> <xsd:field xpath="bk:ISBN"/> </xsd:keyref> This tells the schema-validator that the ISBN of the Book that the Author is signing must refer to one of the ISBN elements in the collection defined by the PK key.

  30. Specifying scope of uniqueness in XML Schemas • The key/keyref/unique elements may be placed anywhere in your schema (that is, at the bottom of any element declaration) • Where you place them determines the scope of the uniqueness • Example. We may desire to have uniqueness in a localized region of instance documents. Thus, we would use key/keyref/unique within the element for that region.

  31. Additional schema mechanisms include & import open content

  32. include & import • xsd:include • similar to a copy and paste • overriding the definitions of the included schema isn’t allowed. • <xsd:include schemaLocation="character.xsd"/>

  33. Assembling a Schema from Multiple Schema Documents • The include element allows you to access components in other schemas • All the schemas you include must have the same namespace as your schema (i.e., the schema that is doing the include) • The net effect of include is as though you had typed all the definitions directly into the containing schema LibraryEmployee.xsd LibraryBook.xsd <xsd:schema …> <xsd:include schemaLocation="LibraryBook.xsd"/> <xsd:include schemaLocation="LibraryEmployee.xsd"/> … </xsd:schema> Library.xsd

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

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

  36. <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.camera.org" xmlns:nikon="http://www.nikon.com" xmlns:olympus="http://www.olympus.com" xmlns:pentax="http://www.pentax.com" elementFormDefault="qualified"> <xsd:import namespace="http://www.nikon.com" schemaLocation="Nikon.xsd"/> <xsd:import namespace="http://www.olympus.com" schemaLocation="Olympus.xsd"/> <xsd:import namespace="http://www.pentax.com" schemaLocation="Pentax.xsd"/> <xsd:element name="camera"> <xsd:complexType> <xsd:sequence> <xsd:element name="body" type="nikon:body_type"/> <xsd:element name="lens" type="olympus:lens_type"/> <xsd:element name="manual_adapter" type="pentax:manual_adapter_type"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:schema> These import elements give us access to the components in these other schemas. Here I am using the body_type that is defined in the Nikon namespace

  37. Extensible Instance Documents • The <any> element enables instance document authors to create instance documents containing elements above and beyond what was specified by the schema. The instance documents are said to be extensible. Contrast this schema with previous schemas where the content of all our elements were always fixed and static. • We are empowering the instance document author with the ability to define what data makes sense to him/her!

  38. Open Content • Definition: an open content schema is one that allows instance documents to contain additional elements beyond what is declared in the schema. This is achieved by using the <any> and <anyAttribute> elements in the schema. • Sprinkling <any> and <anyAttribute> elements liberally throughout your schema will yield benefits in terms of how evolvable your schema is.

  39. anyAttribute • The <anyAttribute> element enables the instance document author to extend his/her document with attributes not specified by the schema. <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> <xsd:any minOccurs="0"/> </xsd:sequence> <xsd:anyAttribute/> </xsd:complexType> </xsd:element> Now an instance document author can add any number of attributes onto a <Book> element (as well as extend the element content).

  40. DTD vs Schema • Enhanced datatypes • 44+ versus 10 • Can create your own datatypes • Written in the same syntax as instance documents • Better maintainence and readability • Object-oriented - can extend or restrict a type • Modular - include & import • Extensibility • An open content schema by using the <any> and <anyAttribute> elements.

  41. DTD vs Schema • Can specify element content as being unique (keys on content) and uniqueness within a region • Can define elements with nil content • Can define substitutable elements • Can express sets, i.e., can define the child elements to occur in any order

  42. Not “All Powerful” • XML Schemas is very powerful • However, it is not "all powerful". There are many constraints that it cannot express. Here are some examples: • Ensure that the value of the aircraft <Elevation> element is greater than the value of the obstacle <Height> element. • Ensure that: • if the value of the attribute, mode, is "air", then the value of the element, <Transportation>, is either airplane or hot-air balloon • if mode="water" then <Transportation> is either boat or hovercraft • if mode="ground" then <Transportation> is either car or bicycle. • Ensure that the value of the <PaymentReceived> is equal to the value of <PaymentDue>, where these elements are in separate documents! • To check all our constraints we will need to supplement XML Schemas with another tool.

  43. Two Approaches to Extending XML Schemas • XSLT/XPath • The first approach is to supplement the XSD document with a stylesheet • Schematron • The second approach is to embed the additional constraints within <appinfo> elements in the XSD document. Then, a tool (Schematron) will extract and process those constraints.

  44. Schematron • The Schematron differs in basic concept from other schema languages in that it not based on grammars but on finding tree patterns in the parsed document. This approach allows many kinds of structures to be represented which are inconvenient and difficult in grammar-based schema languages. • XSLT/XPath based • W3C Schemas are conservative: everything not permitted is forbidden. • Schematron is liberal: everything not forbidden is permitted. • No data typing; validation only • Handles unordered structures very well • Handles descendant constraints very well • Almost self-documenting

  45. Element - conditional definition For instance, the following Schematron schema states that if the element E has the attribute one, then it must have the second attribute two as well: <rule context='E'> <report test='(@one) or not(@one and @two)'> E cannot have attribute 'one‘ alone. </report> </rule>

  46. Schema Validators • Command Line Only • XSV by Henry Thompson • ftp://ftp.cogsci.ed.ac.uk/pub/XSV/XSV12.EXE • Has a Programmatic API • xerces by Apache • http://www.apache.org/xerces-j/index.html • IBM Schema Quality Checker (Note: this tool is only used to check your schema. It cannot be used to validate an instance document against a schema.) • http://www.alphaworks.ibm.com/tech/xmlsqc • MSXML4.0 • http://www.microsoft.com • GUI Oriented • XML Spy • http://www.xmlspy.com • Turbo XML • http://www.extensibility.com

  47. Sources • http://www.w3.org/XML/Schema • http://www.xfront.com/ • http://www.xml.com/

More Related