Download presentation
Presentation is loading. Please wait.
Published byBeverley Davis Modified over 9 years ago
1
1 XML Schemas
2
2 Useful Links Schema tutorial links: http://www.w3schools.com/schema/default.asp http://www.xfront.com/ http://www.w3.org/TR/xmlschema-0/
3
3 Why Schemas –Enhanced datatypes 44+ versus 10 Can create your own datatypes –Example: "This is a new type based on the string type and elements of this type must follow this pattern: ddd- dddd, where 'd' represents a digit". –Written in the same syntax as instance documents: less syntax to remember –Can extend or restrict a type (derive new type definitions on the basis of old ones) –Can express sets, i.e., can define the child elements to 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 substitutable elements - e.g., the "Book" element is substitutable for the "Publication" element.
4
4 Schemas and DTDs One difference between XML Schemas and DTDs is that the XML Schema vocabulary is associated with a name (namespace). Likewise, the new vocabulary that you define must be associated with a name (namespace). With DTDs neither set of vocabulary is associated with a name (namespace).
5
5 Some XML Parsers with Schema Support A comparison of some XML parsers: –http://webreference.com/xml/column22/2.htmlhttp://webreference.com/xml/column22/2.html
6
6 Example – BookCatalog.dtd
7
7 An Example Of A Schema Definition BookCatalog.xsd
8
8 BookCatalog.xml <BookCatalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="BookCatalog.xsd"> Billions Of Stars Susan Boggs 1983 1-5555-555-2 Anderson-Wells Adventures Of Freddie the Frog John Smith 1977 0-444-4444-4 Kidder Publishing Co.
9
9 Schema namespace 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
10
10 Using namespaces Example – plants.xsd <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyDefs.com" xmlns="http://www.MyDefs.com" elementFormDefault="qualified">
11
11 Example – plants.xml <plants xmlns="http://www.MyDefs.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xsi:schemaLocation=“plants.xsd"> Daisy perennial
12
12 Example – BookCatalog.xsd <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyDefs.org" xmlns:bk="http://www.MyDefs.org" elementFormDefault="qualified"> … …
13
13 BookCatalog.xml <BookCatalog xmlns="http://www.MyDefs.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.MyDefs.org BookCatalog.xsd"> Billions Of Stars Susan Boggs 1983 1-5555-555-2 Anderson-Wells Adventures Of Freddie the Frog John Smith 1977 0-444-4444-4 Kidder Publishing Co.
14
14 Simple vs Complex Types A schema document can be divided into content types: Simple and complex types. Simple types: can contain only text. There are several simple built-in types – String, Boolean, Float, Double, ID, IDREF, Entity etc. –Derived data types: The built-in data types can be customized to control the content. Complex types: can contain elements that can contain other elements or attributes. Local vs global declarations: Schema components, elements, attributes, named simple and complex types, declared under xsd:schema element are considered globally declared. A global declaration has to be explicitly referenced. New elements can be declared within the context of a complex type, in which case they are considered locally declared.
15
15 Simple Types: exampleA.xsd
16
16 exampleA.xml Billions Of Stars 2000-01-02 1983 true
17
17 Deriving From Simple Types A new custom type can be derived a simple type. Example: -- refers to a simple type Patterns are specified using regular expressions (supports regular expressions from perl)
18
18 Example – Example3.xsd and Example3.xml Billions Of Stars 2-12345-678-9
19
19 Giving a choice of acceptable values
20
20 Defining Complex Types Elements that can contain other elements or attributes. There are four kinds of elements of complex type: –Elements that contain other elements and attributes, but no text. –Elements that are empty – possibly contain attributes but no other elements or text. –Elements of mixed content that can contain a combination of elements, attributes or text. –Elements that contain only text.
21
21 Example - Elements containing elements only ….
22
22 Example – Elements containing elements only
23
23 Referring to a group 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.
24
24 Using all Billions Of Stars Susan Boggs 1983 1-5555-555-2 Anderson- Wells Adventures Of Freddie the Frog John Smith 1977 0-444-4444-4 Kidder Publishing Co.
25
25 Attributes Attribute declarations: –An attribute is declared with. –An attribute can be defined outside of a complex type and referenced in complex type. –Attribute types can be defined using primitive types. –Default, fixed, optional, required and prohibited can be used to set the values for attributes. –Example: –
26
26 <!ATTLIST Book Category (autobiography | non-fiction | fiction) #REQUIRED InStock (true | false) "false" Reviewer CDATA " ">
27
27
28
28 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
29
29 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
30
30 Using ISBN as a Key In the next example, we will use a Book's ISBN element as a key.
31
31 Example <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyDefs.com" xmlns="http://www.myDefs.com" xmlns:bk="http://www.MyDefs.com" elementFormDefault="qualified"> define a key, called PK. Select each, and within each the ISBN element is a key.“
32
32 BookCatalog.xml <BookCatalog xmlns="http://www.MyDefs.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.MyDefs.com BookCatalog.xsd"> Billions Of Stars Susan Boggs 1983 1-5555-555-2 Anderson-Wells Adventures Of Freddie the Frog John Smith 1977 0-444-4444-4 Kidder Publishing Co.
33
33 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.
34
34 Example – meeting.xsd <!-- The key is the combination of the First and Last name.
35
35 Example-meeting.xml <meeting xmlns="http://www.MyDefs.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xsi:schemaLocation="http://www.MyDefs.com Meeting.xsd"> John Smith Mary Jones
36
36 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.
37
37 Example <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyDefs.com" xmlns="http://www.myDefs.com" xmlns:bk="http://www.MyDefs.com" elementFormDefault="qualified"> …. <!– ISBN are unique
38
38 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.
39
39 Example – keyRef.xsd
40
40 Example – keyRef.xml
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.