Download presentation
Presentation is loading. Please wait.
Published byGrant Casey Modified over 9 years ago
1
XML 2nd EDITION Tutorial 4 Working With Schemas
2
XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents. The XML document containing the content is called the instance document.
3
XP Comparing Schemas and DTDs This figure compares schemas and DTDs
4
XP Schema Vocabularies There is no single schema form. Several schema “vocabularies” have been developed in the XML language. Support for a particular schema depends on the XML parser being used for validation.
5
XP Schema Vocabularies This figure shows a few schema vocabularies
6
XP Starting A Schema File A schema is always placed in a separate XML document that is referenced by the instance document.
7
XP Example Schema
8
XP Elements And Attributes Of The Patients Document This figure shows the elements and attributes of the patients.xml document
9
XP Schema Types XML Schema recognize two categories of types: – Simple type contains only a single value Value of an attribute Or the textual content of an element – Complex type Contains a structure
10
XP Schema Types This figure shows types of elements
11
XP Simple Type Elements Use the following syntax to declare a simple type element in XML Schema: name = the name of the element in the instance document type = the data type of the element. Example: Unlike DTD, schema is an XML language
12
XP Understanding Data Types XML Schema supports two data types – A built-in data type is part of the XML Schema specifications and is available to all XML Schema authors. – A user-derived data type is created by the XML Schema author for specific data values in the instance document.
13
XP Declaring An Attribute An attribute is another example of a simple type. The syntax to define an attribute is name = the name of the attribute type = the data type default = the attribute’s default value fixed = a fixed value for the attribute For examle:
14
XP Associating Attributes And Elements The basic structure for defining a complex type with XML Schema is declarations name = the name of the element declarations = schema commands specific to the type of complex element being defined.
15
XP Associating Attributes And Elements Four complex type elements that usually appear in an instance document are the following: – The element is an empty element and contains only attributes. – The element contains textual content and attributes but no child elements. – The element contains child elements but not attributes. – The element contains both child elements and attributes.
16
XP Empty Elements And Attributes The code to declare the attributes of an empty element is attributes attributes = the set of declarations that define the attributes associated with the element
17
XP Empty Elements And Attributes For example: Describes the following empty element:
18
XP Simple Content And Attributes If an element is not empty and contains textual content (but no child elements), the structure of the complex type element is slightly different. attributes
19
XP Simple Content And Attributes For example: defines 0.81
20
XP Specifying The Use Of An Attribute An attribute may or may not be required with a particular element. To indicate whether an attribute is required, you add the use attribute to the element declaration or reference. The use attribute has the following values: – required—The attribute must always appear with the element – optional—(default) The use of the attribute is optional with the element – prohibited—The attribute cannot be used with the element For example, the previous attribute declaration is modified as:
21
XP Referencing An Element Or Attribute Rather than nesting the attribute declaration within the element, you can create a reference to it. The code to create a reference to an element or attribute declaration is where elemName = the name used in an element declaration attName = the name used in an attribute declaration
22
XP Referencing An Element Or Attribute ref=“scale” use=“required” />
23
XP Working With Child Elements Another kind of complex type element contains child elements, but no attributes. To define these child elements, use the code structure elements where elements =the list of simple type element declarations for each child element compositor = defines how the child elements are organized.
24
XP Using Compositors XML Schema supports the following compositors: – sequence defines a specific order for the child elements – choice allows any one of the child elements to appear in the instance document – all allows any of the child elements to appear in any order in the instance document; however, they must appear either only once or not all.
25
XP Using Compositors Must be in sequence
26
XP Using Compositors Family may contain Father and/or Mother in no particular order
27
XP Specifying The Occurrences Of An Item
28
XP Working With Child Elements And Attributes The code for a complex type element that contains both attributes and child elements is elements attributes
29
XP Child Elements And Attributes Example
30
XP Specifying Mixed Content Patient Cynthia Davis was enrolled in the Tamoxifen Study on 8/15/2003. can be declared in the schema file using the following complex type: element contains both text and child elements
31
XP Applying A Schema To attach a schema to the document, you must do the following: – Declare a namespace for XML Schema in the instance document. – Indicate the location of the schema file. Example: xmlns:xsi=http://www.w3.org/2001/XMLSchema- instance xsi=SchemaLocation=“pschema.xsd”> Commonly used for XML Schema instances Schema instance namespace
32
XP Understanding Data Types A primitive data type, also called a base type, is one of 19 fundamental data types not defined in terms of other types. A derived data type is a collection of 25 data types that the XML Schema developers created based on the 19 primitive types.
33
XP Understanding Data Types This figure shows the 44 built-in data types Page XML 168 Premitive types Derived Types Dates & times
34
XP Understanding Data Types This figure shows a partial description of XML string data types
35
XP Using Data Types Examples:
36
XP Understanding Data Types This figure shows a partial description of XML numeric data types
37
XP Understanding Data Types This figure shows a partial description of XML date and time data types
38
XP Deriving New Data Types Three components are involved in deriving new data types: – Value space: the set of values that correspond to the data type. – Lexical space: the set of textual representations of the value space. – Facets: the properties of the data type that distinguish one data type from another. Text string length, range of allowable values,
39
XP User Derived Data New data types fall into three categories: – List: a list of values where each list is derived from a base type. – Union: the combination of two or more data types. – Restriction: a limit placed on the facet of a base type.
40
XP List List data type is a list of values separated by white space To create a list data type: To use the data type: 15.1 15.8. 20.0 9.3 7.1 5.2
41
XP Deriving A Restricted Data Type The most common way to derive a new data type is to restrict the properties of a base type. XML Schema provides twelve constraining facets for this purpose.
42
XP Constraining Facets This figure shows the 12 constraining facets
43
XP Constraining Facets Example Constrains the data type to be greater than or equal to the value
44
XP The Patterns Facet A pattern can be created with a formatted text string called a regular expression or regex. To apply a regular expression in a data type, you use the code Where regex is a regular expression pattern.
45
XP Regular Expressions Figure 4-30 page 184 shows character types, for example – \d a digit from 0 to 9 – \D non digit character – Etc. Figure 4-31 page 185 shows character sets, for example – [chars] Match any character in the chars list – [a-z] Match any lower case letter – [0-9] match any digit form 0-9 – Etc.
46
XP Pattern Quantifiers This figure shows pattern quantifiers
47
XP Example Regular Expressions <xs:pattern value=“[A-Z]{0,10}*” /}
48
XP Working With Named Types Schema authors can create customized complex types. – Advantage: reuse the complex structure in several places in the document. For example: Use
49
XP Named Model Groups A named model group is a collection, or group, of elements. Syntax elements Where name is the name of the model group, and elements is a collection of element declarations
50
XP Named Model Groups Example Definition Use >
51
XP Working With Named Attribute Groups Attributes can be grouped into collections called named attribute groups. This is particularly useful for attributes that you want to use with several different elements in a schema. The syntax for a named attribute group is attributes Where name is the name of the attribute group and attributes is a collection of attributes assigned to the group.
52
XP Working With Named Attribute Groups Example: curt Hurley
53
XP Scope An item has global scope = it is a child of a root schema element – Can be referenced and reused anywhere in the schema An item has local scope = it is nested within another element – Can be referenced and reused only within the item in which it is declared
54
XP Structuring A Schema Schemas can be structured in several ways: Flat catalog (also called salami slice) – All declarations are made globally Russian doll – Has only one global element – Everything else is nested inside of that element Venetian Blind Design – Similar to flat catalog, except – Creates types and references within a single global element
55
XP Structuring A Schema One schema design is a Flat Catalog Design. – Sometimes called salami slice design In this design, all element declarations are made globally. The structure of the instance document is created by referencing the global element declarations.
56
XP Flat Catalog Design This figure shows a Flat Catalog design
57
XP Structuring A Schema Schemas can be structured in a number of ways. One structure is called a Russian Doll design. This design involves sets of nested declarations. While this design makes it easy to associate the schema with the instance document, it can be confusing and difficult to maintain.
58
XP Russian Doll Design This figure shows a Russian Doll design
59
XP VENETIAN BLIND DESIGN A Venetian blind design Compromise between flat catalogs and Russian dolls – Element and attribute groups and named types are declared globally (and can be reused throughout the schema) – Elements and attributes for the instance document are local and nested
60
XP Venetian Blind Design
61
XP Comparing Schema Designs This figure compares the three schema designs
62
XP Placing A Schema In A Namespace: Targeting A Namespace To associate a schema with a namespace – declare the namespace prefix:xmlns="uri" – make that namespace the target of the schema. targetNamespace="uri" Where prefix = prefix of the XML Schema namespace uri = URI of the target namespace
63
XP Placing A Schema In A Namespace: Targeting A Namespace Example (Figure 4-40): <xs:schema xmlns:xs=http://www.ww3.org/2001/XMLSchema xmlns=“http:/uhosp.edu/patients/na” targetNamespace=“http://uhosp.edu/patients/na> <xs:element name=“patient” type=“pType” Minoccurs = “1” maxoccurs=“unbounded” /> … Declare the namespace Make the new namespace the target of the schema
64
XP Applying A Schema To A Document With A Namespace To apply a schema to a document with a namespace, add the following attributes to the instance document’s root element: xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xsi:schemaLocation="uri schema" Where uri is the URI of the namespace and schema is the location and name of the schema file. All global elements and attributes from the schema must be qualified in the instance document.
65
XP Applying A Schema To A Document With A Namespace <patients xmlns:xsi=http://www.W3.org/200/XMLSchema- instance xmlns=“http://uhosp.edu/patients/ns xsi:schemaLocation=http://uhosp.edu/patients/ns patvb.xsd> <patient patID = “MR890-041-02” … ….
66
XP Including And Importing Schemas To include a schema from the same namespace, add the following element as a child of the schema element: Where schema is the name and location of the schema file. To import a schema from a different namespace, use the syntax Where uri is the URI of the imported schema’s namespace and schema is the name and location of the schema file.
67
XP Referencing Objects From Other Schemas Once a schema is imported, any objects it contains with global scope can be referenced To reference an object from an imported schema, you must declare the namespace of the imported schema in the schema element You can then reference the object using the ref attribute or the type attribute for customized simple and complex types
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.