Download presentation
Presentation is loading. Please wait.
Published byFrancine Heath Modified over 9 years ago
1
1 XML Schemas
2
2 Topics What are Schemas? NameSpaces Elements Attributes Data Types Derivations Keys
3
3 Describing Metadata using Schemas XML Schema Strategy - two documents are used : –a schema document specifies the properties (metadata) for a class of resources (objects); –each instance document provides specific values for the properties.
4
4 XML Schema: Specifies the Properties for a Class of Resources "For the class of Book resources, we identify five properties - Title, Author, Date, ISBN, and Publisher"
5
5 XML Instance Document: Specifies Values for the Properties Cosmos Sagan 1977 0-111-34319-4 Star Publishers Co. An XML document with a specific instance of a Book
6
6 XML Schema Vocabulary Schema element: All XML schemas have schema as the root element. XML Schema namespace: The elements and datatypes that are used to construct schemas, like, schema, element, complexType, sequence and string come from the http://…/XMLSchema namespace
7
7 Using namespaces Plants.xsd <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyDefs.com" xmlns="http://www.MyDefs.com" elementFormDefault="qualified"> Plants.xml <plants xmlns="http://www.MyDefs.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=“Plants.xsd"> Daisy perennial
8
8 Element Definitions Elements are defined using element construct. A complex type can be defined for an element, where a complex type usually contains a set of element and/or attribute declarations and element references. Example: </xsd:sequence < /xsd:element
9
9 Example … …. An element can be declared and we can refer to that element declaration. The default value for minOccurs is "1" The default value for maxOccurs is "1"
10
10 Element Declarations Using Anonymous Types An element can be declared and we can refer to that element declaration. Alternatively, an element declaration can be declared in line. Method 1: Method 2 - Inlining ….
11
11 Element Declarations Using Named Types <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified">
12
12 Please note that: is equivalent to: Element A references the complexType foo. Element A has the complexType definition inlined in the element declaration.
13
13 Simple vs Complex Types When do you use the complexType element and when do you use the simpleType element? –Use the complexType element when you want to define child elements and/or attributes of an element –Use the simpleType element when you want to create a new type that is a refinement of a built-in type (string, date, gYear, etc)
14
14 group Element The group element enables you to group together element declarations. –If we want to reuse a set of elements in several content –type definitions, we can use a model group to define a set of elements that can be repeated. They act rather like parameter entities in DTDs. –A model group consists of element declarations, wildcards and other model groups. –The minimum values for min and maxOccurences = 1. Note: the group element is just for grouping together element declarations, no attribute declarations allowed! Group definitions must be global Cannot inline the group definition.
15
15 Grouping Elements
16
16 Grouping Elements
17
17 Expressing Alternates DTD: Schema : Use choice (an exclusive-or) to contain only one element – either annual or perennial.
18
18 Plants.xsd Using sequence and choice ---------------------------------------------------------------------------------- Daisy Start early
19
19 fixed/default Element Values When you declare an element you can give it a fixed or default value. –Then, in the instance document, you can leave the element empty. … 0 or equivalently: … red or equivalently:
20
20 Expressing Any Order Create an element with a set of elements that are allowed to occur in any order. The children of cannot be part of any other element group. <xsd:element name="ISBN" type="xsd:string"/
21
21 type Attribute or complexType Child Element, but not Both! An element declaration can have a type attribute, or a complexType child element, but it cannot have both a type attribute and a complexType child element. …
22
22 Summary of Declaring Elements (two ways to do it) A simple type (e.g., xsd:string) or the name of a complexType (e.g., BookPublication) … 1 2 A nonnegative integer A nonnegative integer or "unbounded" Note: minOccurs and maxOccurs can only be used in nested (local) element declarations.
23
23 Terminology: Declaration vs Definition In a schema: –You declare elements and attributes. Schema components that are declared are those that have a representation in an XML instance document. –You define components that are used just within the schema document(s). Schema components that are defined are those that have no representation in an XML instance document. Declarations: - element declarations - attribute declarations Definitions: - type (simple, complex) definitions - attribute group definitions - model group definitions
24
24 Terminology: Global versus Local Global element declarations, global type definitions: –These are element declarations/type definitions that are immediate children of Local element declarations, local type definitions: –These are element declarations/type definitions that are nested within other elements/types.
25
25 Global type definition Global element declaration Local element declarations Local type definition <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified">
26
26 Global vs Local …? So what if an element or type is global or local. What practical impact does it have? –Answer: only global elements/types can be referenced (i.e., reused). Thus, if an element/type is local then it is effectively invisible to the rest of the schema (and to other schemas).
27
27 Data Types Data types are the basic building blocks. –Primitive Data types: Are built-in types. –String, Boolean, Float, Double, ID, IDREF, Entity etc. –Cannot contain child elements or attributes. –Derived Data types: Defined in terms of existing types. For example, CDATA type is derived from the String type. –May have attributes, elements or mixed content. –Built-in Derived types for XML Schema: Some of the commonly used derived data types are built into XML Schema –Examples: CDATA, token, name, integer, long, short, byte, date etc.
28
28 Aspects Of Data types All XML datatypes are comprised of three parts: –A value space –A lexical space –A set of facets
29
29 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 BookCatalog schema to restrict the content of the Date element to just date values (actually, year values. See next two slides). Similarly, constrain the content of the ISBN element to content of this form: d-ddddd-ddd-d or d-ddd-ddddd-d or d-dd-dddddd-d, where 'd' stands for 'digit'
30
30 The date Datatype A built-in datatype (i.e., schema validators know about this datatype) This datatype is used to represent a specific day (year-month-day) Elements declared to be of type date must follow this form: CCYY- MM-DD Example: 2003-05-31 represents May 31, 2003 Elements declared to be of type gYear must follow this form: CCYY
31
31 RegularExpressions Using Regular Expressions and patterns, the string type can be restricted. Example: OR
32
32 Built-in Datatypes Primitive Datatypes –string –boolean –decimal –float –double –duration –dateTime –time –date –gYearMonth –gYear –gMonthDay Atomic, built-in –"Hello World" –{true, false} –7.08 – 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN – P1Y2M3DT10H30M12.3S – format: CCYY-MM-DDThh-mm-ss – format: hh:mm:ss.sss – format: CCYY-MM-DD – format: CCYY-MM – format: CCYY – format: --MM-DD Note: 'T' is the date/time separator INF = infinity NAN = not-a-number
33
33 Built-in Datatypes (cont.) Primitive Datatypes –gDay –gMonth –hexBinary –base64Binary –anyURI –QName –NOTATION Atomic, built-in – format: ---DD (note the 3 dashes) – format: --MM-- –a hex string –a base64 string –http://www.xfront.com –a namespace qualified name –a NOTATION from the XML spec
34
34 Built-in Datatypes (cont.) Derived types –normalizedString –token –language –IDREFS –ENTITIES –NMTOKEN –NMTOKENS –Name –NCName –ID –IDREF –ENTITY –integer –nonPositiveInteger Subtype of primitive datatype – A string without tabs, line feeds, or carriage returns – String w/o tabs, l/f, leading/trailing spaces, consecutive spaces –any valid xml:lang value, e.g., EN, FR,... –must be used only with attributes –part (no namespace qualifier) –must be used only with attributes –456 –negative infinity to 0
35
35 Built-in Datatypes (cont.) Derived types –negativeInteger –long –int –short –byte –nonNegativeInteger –unsignedLong –unsignedInt –unsignedShort –unsignedByte –positiveInteger Subtype of primitive datatype – negative infinity to -1 – -9223372036854775808 to 9223372036854775808 – -2147483648 to 2147483647 – -32768 to 32767 – -127 to 128 – 0 to infinity – 0 to 18446744073709551615 – 0 to 4294967295 – 0 to 65535 –0 to 255 –1 to infinity Note: the following types can only be used with attributes (which we will discuss later): ID, IDREF, IDREFS, NMTOKEN, NMTOKENS, ENTITY, and ENTITIES.
36
36 Creating your own Datatypes A new datatype can be defined from an existing datatype (called the "base" type) by specifying values for one or more of the optional facets for the base type. Example. The string primitive datatype has six optional facets: –length –minLength –maxLength –pattern –enumeration –whitespace (legal values: preserve, replace, collapse)
37
37 Example of Creating a New Datatype by Specifying Facet Values 1. This creates a new datatype called 'TelephoneNumber'. 2. Elements of this type can hold string values, 3. But the string length must be exactly 8 characters long and 4. The string must follow the pattern: ddd-dddd, where 'd' represents a 'digit'. (Obviously, in this example the regular expression makes the length facet redundant.) 1 2 3 4
38
38 Another Example This creates a new type called US-Flag-Colors. An element declared to be of this type must have either the value red, or white, or blue.
39
39 Facets of the integer Datatype The integer datatype has 8 optional facets: –totalDigits –pattern –whitespace –enumeration –maxInclusive –maxExclusive –minInclusive –minExclusive
40
40 Example This creates a new datatype called 'EarthSurfaceElevation'. Elements declared to be of this type can hold an integer. However, the integer is restricted to have a value between -1290 and 29035, inclusive.
41
41 General Form of Creating a New Datatype by Specifying Facet Values … Facets: - length - minlength - maxlength - pattern - enumeration - minInclusive - maxInclusive - minExclusive - maxExclusive... Sources: - string - boolean - number - float - double - duration - dateTime - time... See DatatypeFacets.html for a mapping of datatypes to their facets.
42
42 Multiple Facets - "and" them together, or "or" them together? An element declared to be of type TelephoneNumber must be a string of length=8 and the string must follow the pattern: 3 digits, dash, 4 digits. An element declared to be of type US-Flag-Color must be a string with a value of either red, or white, or blue. Patterns, enumerations => "or" them together All other facets => "and" them together
43
43 Creating a simpleType from another simpleType Thus far we have created a simpleType using one of the built-in datatypes as our base type. However, we can create a simpleType that uses another simpleType as the base. See next slide.
44
44 This simpleType uses EarthSurfaceElevation as its base type.
45
45 Fixing a Facet Value Sometimes when we define a simpleType we want to require that one (or more) facet have an unchanging value. That is, we want to make the facet a constant. simpleTypes which derive from this simpleType may not change this facet.
46
46 Error! Cannot change the value of a fixed facet!
47
47 Element Containing a User-Defined Simple Type Example. Create a schema element declaration for an elevation element. Declare the elevation element to be an integer with a range -1290 to 29035 5240 Here's one way of declaring the elevation element:
48
48 Element Containing a User-Defined Simple Type (cont.) Here's an alternative method for declaring elevation: The simpleType definition is defined inline, it is an anonymous simpleType definition. The disadvantage of this approach is that this simpleType may not be reused by other elements.
49
49 Summary of Declaring Elements (three ways to do it) … 1 2 … 3
50
50 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 - 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
51
51 Regular Expressions (cont.) 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 – Chapter followed by a blank followed by a digit –hoho there – any (one) char followed by abc –ax, bx, aax, bbx, abx, bax,...
52
52 Regular Expressions (cont.) a{1,3}x a{2,}x \w\s\w ax, aax, aaax aax, aaax, aaaax, … word character (alphanumeric plus dash) followed by a space followed by a word character [a-zA-Z-[Ol]]* A string comprised of any lower and upper case letters, except "O" and "l" \. The period "." (Without the backward slash the period means "any character")
53
53 Regular Expressions (cont.) \n \r \t \\ \| \- \^ \? \* \+ \{ \} \( \) \[ \] linefeed carriage return tab The backward slash \ The vertical bar | The hyphen - The caret ^ The question mark ? The asterisk * The plus sign + The open curly brace { The close curly brace } The open paren ( The close paren ) The open square bracket [ The close square bracket ]
54
54 Regular Expressions \p{L} \p{Lu} \p{Ll} \p{N} \p{Nd} \p{P} \p{Sc} A letter, from any language An uppercase letter, from any language A lowercase letter, from any language A number - Roman, fractions, etc A digit from any language A punctuation symbol A currency sign, from any language "currency sign from any language, followed by one or more digits from any language, optionally followed by a period and two digits from any language" $45.99 ¥300
55
55 Example R.E. [1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5] 0 to 99 100 to 199 200 to 249250 to 255 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...
56
56 Derived Types We can do a form of subclassing complexType definitions. We call this "derived types" –derive by extension: extend the parent complexType with more elements –derive by restriction: create a type which is a subset of the base type. There are two ways to subset the elements: redefine a base type element to have a restricted range of values, or redefine a base type element to have a more restricted number of occurrences.
57
57 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> Note that BookPublication extends the Publication type, i.e., we are doing Derive by Extension
58
58 Elements declared to be of type BookPublication will have 5 child elements - Title, Author, Date, ISBN, and Publisher. Note that the elements in the derived type are appended to the elements in the base type.
59
59 Derive by Restriction Elements of type SingleAuthorPublication will have 3 child elements - Title, Author, and Date. However, there must be exactly one Author element. Note that in the restriction type you must repeat all the declarations from the base type (except when the base type has an element with minOccurs="0" and the subtype wishes to delete it. See next slide).
60
60 Deleting an element in the base type Note that in this subtype we have eliminated the Author element, i.e., the subtype is just comprised of an unbounded number of Title elements followed by a single Date element. If the base type has an element with minOccurs="0", and the subtype wishes to not have that element, then it can simply leave it out.
61
61 Derive by Restriction (cont.) You might (legitimately) ask: –why do I have to repeat all the declarations from the base type? Why can't I simply show the delta (i.e., show those declarations that are changed)? –What's the advantage of doing derived by restriction if I have to repeat everything? I'm certainly not saving on typing. Answer: –Even though you have to retype everything in the base type there are advantages to explicitly associating a type with a base type. Later we will see that an element’s content model may be substituted by the content model of derived types. Thus, the content of an element that has been declared to be of type Publication can be substituted with a SingleAuthorPublication content since SingleAuthorPublication derives from Publication. We will discuss this type substitutability in detail later.
62
62 Prohibiting Derivations Sometimes we may want to create a type and disallow all derivations of it, or just disallow extension derivations, or disallow restriction derivations. –Rationale: "For example, I may create a complexType and make it publicly available for others to use. However, I don't want them to extend it with their proprietary extensions or subset it to remove, say, copyright information." (Jon Cleaver) Publication cannot be extended nor restricted Publication cannot be restricted Publication cannot be extended
63
63 Element Substitution Oftentimes in daily conversation there are several ways to express something. –In Boston we use the words "T" and "subway" interchangeably. For example, "we took the T into town", or "we took the subway into town". Thus, "T" and "subway" are substitutable. Which one is used may depend upon what part of the state you live in, what mood you're in, or any number of factors. We would like to be able to express this substitutability in XML Schemas. –That is, we would like to be able to declare in a schema an element called "subway", an element called "T", and state that "T"may be substituted for "subway". Instance documents can then use either or, depending on their preference.
64
64 substitutionGroup We can define a group of substitutable elements (called a substitutionGroup) by declaring an element (called the head) and then declaring other elements which state that they are substitutable for the head element. subway is the head element T is substitutable for subway So what's the big deal? - Anywhere a head element can be used in an instance document, any member of the substitutionGroup can be substituted!
65
65 Red Line Schema: Instance doc: Red Line Alternative instance doc (substitute T for subway): This example shows the element being substituted with the element.
66
66 International Clients We can use substitutionGroups to create elements customized for our international clients. On the next slide is shown a Spanish version of the element.
67
67 Red Line Schema: Instance doc: Linea Roja Alternative instance doc (customized for our Spanish clients):
68
68 Notes about using substitutionGroup The elements that are declared to be in the substitution group (e.g., subway and T) must be declared as global elements If the type of a substitutionGroup element is the same as the head element then you can omit it (the type) –In our Subway example we could have omitted the type attribute in the declaration of the T element since it is the same as Subway’s type (xsd:string).
69
69 Notes about using substitutionGroup (cont.) The type of every element in the substitutionGroup must be the same as, or derived from, the type of the head element. This type must be the same as "xxx" or, it must be derived from "xxx".
70
70 Element Substitution with Derived Types
71
71 BookType and MagazineType Derive from PublicationType PublicationType BookTypeMagazineType In order for Book and Magazine to be in a substitutionGroup with Publication, their type (BookType and MagazineType, respectively) must be the same as, or derived from Publication's type (PublicationType)
72
72
73
73 Illusions The Adventures of a Reluctant Messiah Richard Bach 1977 0-440-34319-4 Dell Publishing Co. Natural Health 1999 The First and Last Freedom J. Krishnamurti 1954 0-06-064831-7 Harper & Row can contain any element in the substitutionGroup with Publication!
74
74 Blocking Element Substitution An element may wish to block other elements from substituting with it. This is achieved by adding a block attribute.
75
75 Red Line Schema: Instance doc: Red Line Not allowed!
76
76 One more Note about substitutionGroup 1. Transitive: if element A can substitute for element B, and element B can substitute for element C, then element A can substitute for element C. A --> B --> C then A --> C 2. Non-symmetric: if element A can substitute for element B, it is not the case that element B can substitute for element A.
77
77 Attributes On the next slide is a version of the BookCatalog DTD that uses attributes. Then, on the following slide I show how this is implemented using XML Schemas.
78
78 <!ATTLIST Book Category (autobiography | non-fiction | fiction) #REQUIRED InStock (true | false) "false" Reviewer CDATA " "> BookStore.dtd
79
79 InStock (true | false) "false" Reviewer CDATA " " Category (autobiography | non-fiction | fiction) #REQUIRED
80
80 "Instance documents are required to have the Category attribute (as indicated by use="required"). The value of Category must be either autobiography, non-fiction, or fiction (as specified by the enumeration facets)." Note: attributes can only have simpleTypes (i.e., attributes cannot have child elements).
81
81 Summary of Declaring Attributes (two ways to do it) required optional prohibited Do not use the "use" attribute if you use either default or fixed. xsd:string xsd:integer xsd:boolean... … 1 2
82
82 use --> use it only with Local Attribute Declarations The "use" attribute only makes sense in the context of an element declaration. Example: "for each Book element, the Category attribute is required". When declaring a global attribute do not specify a "use"
83
83 … … Local attribute declaration. Use the "use" attribute here. Global attribute declaration. Must NOT have a "use" ("use" only makes sense in the context of an element)
84
84 Inlining Attributes On the next slide is another way of expressing the last example - the attributes are inlined within the Book declaration rather than being separately defined in an attributeGroup.
85
85
86
86 Notes about Attributes The attribute declarations always come last, after the element declarations. The attributes are always with respect to the element that they are defined (nested) within. … "bar and boo are attributes of foo"
87
87 These attributes apply to the element they are nested within (Book) That is, Book has three attributes - Category, InStock, and Reviewer.
88
88 Element with Simple Content and Attributes Example. Consider this: 5440 The elevation element has these two constraints: - it has a simple (integer) content - it has an attribute called units How do we declare elevation? (see next slide)
89
89 1. elevation contains an attribute. - therefore, we must use 2. However, elevation does not contain child elements (which is what we generally use to indicate). Instead, elevation contains simpleContent. 3. We wish to extend the simpleContent (an integer)... 4. with an attribute. 1 2 3 4
90
90 elevation - use Stronger Datatype In the declaration for elevation we allowed it to hold any integer. Further, we allowed the units attribute to hold any string. Let's restrict elevation to hold an integer with a range 0 - 12,000 and let's restrict units to hold either the string "feet" or the string "meters"
91
91
92
92 Summary of Declaring Elements 1. Element with Simple Content. Declaring an element using a built-in type: Declaring an element using a user-defined simpleType: An alternative formulation of the above flag example is to inline the simpleType definition:
93
93 Summary of Declaring Elements (cont.) 2. Element Contains Child Elements Defining the child elements inline: An alternate formulation of the above Person example is to create a named complexType and then use that type:
94
94 Summary of Declaring Elements (cont.) 3. Element Contains a complexType that is an Extension of another complexType
95
95 Summary of Declaring Elements (cont.) 4. Element Contains a complexType that is a Restriction of another complexType
96
96 Summary of Declaring Elements (concluded) 5. Element Contains Simple Content and Attributes Example. Large, green, sour
97
97 Creating Lists There are times when you will want an element to contain a list of values, e.g., "The contents of the Numbers element is a list of numbers". Example: For a document containing a Lottery drawing we might have 12 49 37 99 20 67 How do we declare the element Numbers... (1) To contain a list of integers, and (2) Each integer is restricted to be between 1 and 99, and (3) The total number of integers in the list is exactly six.
98
98 <LotteryDrawings xmlns="http://www.lottery.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.lottery.org Lottery.xsd"> July 1 21 3 67 8 90 12 July 8 55 31 4 57 98 22 July 15 70 77 19 35 44 11 Lottery.xml
99
99 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.lottery.org" xmlns="http://www.lottery.org" elementFormDefault="qualified"> Lottery.xsd
100
100 LotteryNumbers --> Need Stronger Datatyping The list in the previous schema has two problems: –It allows to contain an arbitrarily long list –The numbers in the list may be any positiveInteger We need to: –Restrict the list to length value="6" –Restrict the numbers to maxInclusive value="99"
101
101 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.lottery.org" xmlns="http://www.lottery.org" elementFormDefault="qualified"> Lottery.xsd
102
102 NumbersList is a list where the type of each item is OneToNintyNine. LotteryNumbers restricts NumbersList to a length of six (i.e., an element declared to be of type LotteryNumbers must hold a list of numbers, between 1 and 99, and the length of the list must be exactly six).
103
103 Alternatively, This is read as: "We are creating a new type called LotteryNumbers. It is a restriction. At this point we can either use the base attribute or a simpleType child element to indicate the type that we are restricting (you cannot use both the base attribute and the simpleType child element). We want to restrict the type that is a list of OneToNintyNine. We will restrict that type to a length of 6."
104
104 Notes about the list type You cannot create a list of lists –i.e., you cannot create a list type from another list type. You cannot create a list of complexTypes –i.e., lists only apply to simpleTypes In the instance document, you must separate each item in a list with white space (blank space, tab, or carriage return) The only facets that you may use with a list type are: –length: use this to specify the length of the list –minLength: use this to specify the minimum length of the list –maxLength: use this to specify the maximum length of the list –enumeration: use this to specify the values that the list may have –pattern: use this to specify the values that the list may have
105
105 Summary of Declaring simpleTypes 1. simpleType that uses a built-in base type: 2. simpleType that uses another simpleType as the base type:
106
106 Summary of Declaring simpleTypes 3. simpleType that declares a list type: where the datatype OneToNintyNine is declared as: 4. An alternate form of the above, where the list's datatype is specified using an inlined simpleType:
107
107 Summary of Declaring simpleTypes 5. simpleType that declares a union type: where the datatype UnboundedType is declared as: 6. An alternate form of the above, where the datatype UnboundedType is specified using an inline simpleType:
108
108 any Element The element enables the instance document author to extend his/her document with elements not specified by the schema. Now an instance document author can optionally extend (after ) the content of elements with any element.
109
109 KEYS and Unique
110
110 IDs and IDREFs Library.xsd
111
111 Library.xml 1234567891 Being a dog is a full time job Charles-M.-Schultz SPARKY
112
112 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
113
113 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 not-false. –be unique Key implies unique, but unique does not imply key
114
114 Using ISBN as a Key In the next example, we will use a Book's ISBN element as a key.
115
115 BookStore.xsd <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">
116
116 BookStore.xml <BookStore xmlns="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org BookStore.xsd"> My Life and Times Paul McCartney 9999 Illusions The Adventures of a Reluctant Messiah Richard Bach 4567 The First and Last Freedom J. Krishnamurti 12999
117
117 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.
118
118 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.
119
119 BookStore.xsd
120
120 Referencing a key with a key ref Similar to ID/IDREFS, you can define a keyref to link an element with a key. 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.
121
121 Example Refer to AuthorSigningAtLibrary.xsd and AuthorSigningAtLibrary.xml
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.