1 Tutorial 14 Validating Documents with Schemas Exploring the XML Schema Vocabulary
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 2 Introducing XML Schemas A schema is an XML document that contains validation rules for an XML vocabulary. The XML document containing the content is called the instance document.
Comparing Schemas and DTDs New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 3
4 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.
Schema Vocabularies New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 5
6 Starting a Schema File A schema is always placed in a separate XML document that is referenced by the instance document. The root element in any XML Schema document is the schema element. content
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 7 Understanding Simple and Complex Types XML Schema recognize two categories of element types: simple and complex. A simple type contains a single value. A complex type contains two or more values or elements placed within a defined structure.
Understanding Simple and Complex Types New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 8
Simple and Complex Types in the patients.xml Document New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 9
10 Defining a Simple Type Element Use the following syntax to declare a simple type element in XML Schema: Here, name is the name of the element in the instance document and type is the data type of the element. If a namespace prefix is used with the XML Schema namespace, any XML Schema tags must be qualified with the namespace prefix.
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 11 Understanding Data Types XML Schema supports two data types: built-in and user- derived. 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.
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 12 Defining an Attribute An attribute is another example of a simple type. The syntax to define an attribute is Where name is the name of the attribute, type is the data type, default is the attribute’s default value, and fixed is a fixed value for the attribute.
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 13 Defining a Complex Type Element The basic structure for defining a complex type element with XML Schema is declarations where name is the name of the element and declarations are declarations that declare the type of content contained within the element.
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 14 Defining a Complex Type Element 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 no attributes. –The element contains both child elements and attributes.
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 15 Defining an Element Containing Only Attributes The code to declare the attributes of an empty element is attributes where name is the name of the element in the instance document and attributes is the set of simple type elements that define the attributes associated with the element
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 16 Defining an Element Containing Attributes and Basic Text 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
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 17 Referencing an Element or Attribute Definition XML Schema allows for a great deal of flexibility in designing complex types. 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 is the name used in an element declaration and attName is the name used in an attribute declaration
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 18 Defining an Element with Nested Children Another kind of complex type element contains nested child elements, but no attributes. To define these child elements, use the code structure elements where name is the name of the element, compositor defines how the child elements are organized, and elements is a list of the nested child elements
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 19 Defining an Element with Nested Children 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.
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 20 Defining an Element Containing Nested Elements and Attributes The code for a complex type element that contains both child elements and attributes is elements attributes
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 21 Specifying Mixed Content When the mixed attribute is set to the value “true,” XML Schema assumes that the element contains both text and child elements. The structure of the child elements can then be defined with the conventional method. For example, the XML 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:
Indicating Required Attributes An attribute may or may not be required with a particular element element content where name is the name of the element containing the attribute, element content is XML Schema code that defines the content and structure of the element, properties is XML Schema code that defines the data type and properties of the attribute, and use has one of the following three values (required, optional, or prohibited) New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 22
Specifying the Number of Child Elements To specify the number of times an element appears in the instance document, you can apply the minOccurs and maxOccurs attributes to the element definition where the minOccurs value defines the minimum times the element can occur and the maxOccurs value defines the maximum number of times the element can occur New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 23
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 24 Applying a Schema to an Instance Document 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. To declare the XML Schema namespace in the instance document, you add the following attribute to the document’s root element: xmlns:xsi="
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 25 Applying a Schema to an Instance Document If there is no namespace for the contents of the instance document, add the following attribute to the root element: xsi:noNamespaceSchemaLocation="schema"
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 26 Validating with Built-in 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 data type that is developed from one of these base types.
Validating with Built-in Data Types New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 27
String Data Types New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 28
Numeric Data Types New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 29
Date and Time Data Types New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 30
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 31 Deriving Customized 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.
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 32 Deriving Customized Data Types 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.
Constraining Facets New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 33
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 34 Deriving Data Types Using Regular Expressions A regular expression is a text string that defines a character pattern To apply a regular expression in a data type, you use the code Where regex is a regular expression pattern.
Regular Expression Quantifiers New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 35
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 36 Working with Named Types XML Schema also allows schema authors to create customized complex types. The advantage of creating a complex type is that the complex structure can be reused in the document. For example, the following code declares an element named client containing the complex content of two child elements named firstName and lastName:
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 37 Named Model Groups A named model group is a collection, or group, of elements. The syntax for creating a model group is elements where name is the name of the model group, and elements is a collection of element declarations
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 38 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.
Designing a Schema One schema design is a Flat Catalog Design. In this design, all element declarations are made globally. The structure of the instance document is created by referencing the global element declarations. New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 39
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 40 Flat Catalog Design
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 41 Russian Doll Design 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.
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 42 Russian Doll Design
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 43 Venetian Blind Design A Venetian blind design is similar to a flat catalog, except that instead of declaring elements and attributes globally, it creates named types and references those types within a single global element In this layout, the only globally declared element is the patients element; all other elements and attributes are placed within element or attribute groups or, in the case of the performance element, within a named complex type
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 44 Venetian Blind Design
Comparison of Schema Designs New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 45
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 46 Associating a Schema with a Namespace To associate a schema with a namespace, you first declare the namespace and then make that namespace the target of the schema. To do this, you add the following attributes to the schema’s root element: <xs:schema xmlns:xs=" xmlns="prefix:uri" targetNamespace="uri">... where prefix is the prefix of the namespace and uri is the URI of the namespace
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 47 Validating a Combined Document
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 48 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.
New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 49 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