1 / 20

Uniqueness in XML Schemas

Learn how XML Schema enhances uniqueness by defining element content, non-ID attributes, and combinations as unique, with keys setting specific requirements for elements or attributes.

frazierd
Download Presentation

Uniqueness in XML Schemas

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. Uniqueness in XML Schemas Roger L. Costello XML Technologies Course

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

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

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

  5. <?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> (see example32)

  6. <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. This is nice! We are using the content of a field as a key! (No longer limited to ID attributes for defining uniqueness.)

  7. <?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. (see example32)

  8. 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. See next example.

  9. <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.CostelloReunion.org" xmlns="http://www.CostelloReunion.org" xmlns:reunion="http://www.CostelloReunion.org" elementFormDefault="qualified"> <xsd:element name="Y2KFamilyReunion"> <xsd:complexType> <xsd:sequence> <xsd:element name="Participants" > <xsd:complexType> <xsd:sequence> <xsd:element name="Name" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="First" type="xsd:string"/> <xsd:element name="Last" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:key name="PK"> <xsd:selector xpath="reunion:Participants/reunion:Name"/> <xsd:field xpath="reunion:First"/> <xsd:field xpath="reunion:Last"/> </xsd:key> </xsd:element> </xsd:schema> The key is the combination of the First and Last name. (See example33)

  10. <?xml version="1.0"?> <Y2KFamilyReunion xmlns="http://www.CostelloReunion.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.CostelloReunion.org Y2KFamilyReunion.xsd"> <Participants> <Name><First>Peter</First><Last>Brown</Last></Name> <Name><First>Peter</First><Last>Costello</Last></Name> </Participants> </Y2KFamilyReunion> A schema-validator will verify that each First name/Last name combination is unique.

  11. Must namespace-qualify xPath expressions (when elementFormDefault="qualified") Note that we namespace-qualified the xPath references: <xsd:key name="PK"> <xsd:selector xpath="bk:Book"/> <xsd:field xpath="bk:ISBN"/> </xsd:key> This is required, even though the targetNamespace is the default namespace. This is an xPath requirement. Note that if the schema had instead set elementFormDefault="unqualified" then the xPath expressions would not be namespace-qualified.

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

  13. <?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. (see example 34)

  14. <?xml version="1.0"?> <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"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <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 which has an ISBN element has a unique value (note that the first Book does not have an ISBN. That's perfectly valid!) (see example 31)

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

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

  17. <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 (snippet, see example35)

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

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

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

More Related