Programming the Web Using XML Instructor’s Manual-Chapter 5

Chapter 5

Schemas

Learning Objectives

In this lesson, the students will learn about the XML Schemas. At the end of this lesson, the students should be able to:

  • Differentiate between a DTD and an XML Schema.
  • Understand the concept of a schema.
  • Understand the concept of developing schema.
  • Identify the elements and attributes in a schema.

TIME ESTIMATE FOR MODULE: 2 HOURS, 30 MINUTES

This lesson deals with the fundamentals of developing schemas. Apart from the limitations of a DTD, this lesson also discusses the various elements and attributes that are used in creating a schema.

Class Preparation

The topic is practical based. You need to load the examples given in lesson 5 onto the computer and explain these examples to the students as and when required. Ensure that every topic/technology is explained to the students.

General Teaching Tips

All the topics in this lesson are equally important. It is recommended that you allot more time on the topics related to elements and attributes used in XML Schema. You need to ensure that every table given in this lesson is explained.

Key Terms

Binary types — The data types that can be included for certain types of data, and are supported with just two encodings, usually used for digital signatures.

Complex element — Complex element is one of the types of element in a schema. It contains element and attribute declarations.

Content Model constraint — Content Model is a type of constraint that defines the actual element that can be used, thus setting down the basic language of that particular schema.

Datatype constraint — Datatype constraint defines the types of units of data that the schema will allow and consider valid.

Facets — Limiting the datatype is done by adding “facets” or restrictions, which requires it to match a defined pattern.

Fundamental facet — A Fundamental facet defines a value space.

Namespaces — Namespaces uniquely identify attribute and element names.

Non-Fundamental facet or Constraining facet — A Non-Fundamental facet or Constraining Facet puts constraints on the allowed value.

Numeric types — The data type that represent numeric data, which are very useful in making high-end scientific calculations and financial calculations.

Schema — The word schema in a database terminology derives from a Greek word that means form or shape within the XML context. A schema refers a model that defines how elements, attributes, and text can be arranged.

Simple element — A simple element has no attributes or elements for content.

String types — A string type is a data type that contains only character data.

Target namespace — Target namespace is the namespace for tags that are created at any instance of a particular schema.

XML Schema Definition Language (XSD) — XSD gives a description of how to describe elements in an XML document so that each piece of information within a document conforms to the actual description of the element in which the piece of information is to be placed.

XML Schema namespace — XML Schema namespace is the first namespace of the schema that uniquely identifies attribute and element names.

Lecture Outline

I.Differentiate Between a DTD and an XML Schema

A.DTDs were the original way to decide the structure of information within an XML document and language.

B.The XML Schema Definition Language (XSD) very specifically gives a description on how to describe elements in an XML document.

C.In the XML Schema Definition Language, each piece of information within a document conforms to the actual description of the element in which the piece of information is to be placed.

D.XSD is a representation of the multi relationships between the elements and attributes in an XML document.

E.XSD is written in XML so that it does not need a secondary processing by a parser.

F.Drawbacks of a DTD.

1.DTD syntax is not flexible.

2.DTD employs its own syntax, which is not the same as xml syntax.

3.DTDs cannot describe the actual data contained in elements.

4.DTDs can identify character data or parsed character data, but cannot identify if it should be formatted or structured in a specific way.

5.DTD has a limited ability to deal with namespace.

6.DTDs are not context sensitive.

7.DTDs look for three things.

a.A basic text string.

b.The basic text string mixed in with other child elements.

c.A set of child elements.

Teaching Tip

The school alumni association list example given in this lesson can be explained to the students to throw more light on DTD’s drawbacks.

G.XML Schema (also called Xschema).

1.It is constructed as well-formed XML.

2.It provides consistent validation models through sets of xml instances.

3.It is handy in defining datatypes and patterns.

4.It allows sophisticated descriptions.

5.It shares markup vocabularies.

6.It has the ability to define hierarchical nested elements and various options and constraints on those elements.

7.It has the ability to define a range for either an element or an attribute.

II.Introduction to Schema

A.The word schema in database technology derives from a Greek word that means form or shape.

B.The actual term “schema” derives from database terminology, and within that context describes the actual structure of data in relational tables throughout a relational database.

C.It does not need a secondary processing by a parser.

D.In XML, a schema refers to a model that describes how elements, attributes, and text can be arranged.

E.Both, DTDs and Schema perform validation against data. However, DTDs are not capable of complying with C++ and Java classes, which employ object-oriented technology, also referred to as instance documents. Schemas can comply.

F.The models are described by their constraints that are of two types.

1.Those for content models – They define the actual elements that can be used.

2.Those for datatypes – They only define the type of units of data that the schema will allow and consider valid.

III.Developing Schemas

A.Namespaces uniquely identify attribute and element names.

1.Namespaces are important in schemas because they are set up to process data and documents from different sources.

2.Schemas employ two namespaces.

a.The first is the XML Schema namespace itself.

b.The second is a target namespace for the tags that are created in any instance of a particular schema.

3.Every schema has one target and potentially numerous source namespaces.

4.The names that a schema defines are commonly called the “target” namespace.

5.Source namespaces are the other namespaces to which the schema definition is referenced.

6.A schema can point to a namespace-qualified element using the xsi:schemaLocation attribute, also placed within the root element.

IV.Elements and Attributes

A.Simple element types.

1.A simple element is precisely that, it has absolutely no attributes or elements for content.

2.The text could be limited, and a range could be specified. This makes it good for instances where limited input is desired, such as when using a date, an integer, or a decimal value.

3.Limiting the data type is done by adding “facets” or restrictions, which require it to match a defined pattern.

4.There are 44 built-in simple types in the W3C XML Schema language that are divided into seven basic groups.

a.Binary

b.Boolean

c.Numeric

d.String

e.Time

f.URI reference

g.XML

5.Numeric data types are divided into three basic categories.

a.Integer and floating point.

b.Finite size numbers (like those in Java and C).

c.Signed and unsigned numbers.

Teaching Tip

The students can refer Table 5.1 in the lesson that shows the Schema numeric types.

6.Most numeric types are already well known to programmers, and the xml schema definition has had a lot of input from programmers to make all of these specific definitions.

7.The numeric types are very useful in making high-end scientific calculations and financial calculations.

8.The String type is quite common and takes a sequence of Unicode characters, irrespective of the length, which is the same as xml elements and attributes.

Teaching Tip

Students can refer Table 5.3 and 5.4 to enumerate the Schema XML Data Types and Schema String Types.

9.Binary types can be included for certain types of data, and are supported with just two encodings, usually used for digital signatures.

10.Simple elements are allowed to be either fixed or revert to the default value.

a.The default value automatically assigns to an element when nothing else is indicated.

11.There are two types of facets – fundamental and non-fundamental.

a.A fundamental facet defines a value space.

b.A non-fundamental or constraining facet puts a constraint on the allowed value.

12.The facets and their restrictions are:

a.enumeration – A list of acceptable values.

b.fractionDigits – The maximum number of decimal places equal to or greater than zero.

c.length – The exact number of either characters or list items, equal to or greater than zero.

d.maxExclusive – Tells the upper limit of one value compared to another, and it must be less.

e.maxInclusive - Tells the upper limit of one value compared to another, and it must be less or equal to.

f.maxLength – The maximum numbers of characters or list items allowed, equal to or greater than zero.

g.minExclusive – Specifies the lower range for numeric values, and must be less than the one stated.

h.minInclusive – Specifies the lower range for numeric values, and must be greater than or equal to zero.

i.minLength – The minimum number of characters or list items acceptable, equal to, or greater than zero.

j.pattern – The exact sequence of characters.

k.totalDigits – The exact number of digits allowed, greater than zero.

l.whiteSpace - Defines how white space (carriage returns, line feeds, spaces, and tabs) are dealt with.

13.It is possible to make up a simple type.

a.It is done by restricting the type that exists already (for example, xsd:string) with an xsd:restriction element.

b.The child element of xsd:restriction has an attribute that specifies what type it comes from.

c.Each xsd:restriction element has one or often more child elements that represent facets.

d.Then the xsd:simpleType element can combine each existing type using an xsd:union child element.

B.Complex Types of elements in a schema.

Discussion Point

Ask the students to compare simple and complex types. The discussion will make the students thorough with the idea of the different types of elements.

1.Complex schemas can contain both, attributes and child elements.

2.There is only one top-level element, a child element of the root element xsd:schema.

3.The name attribute of the element complexType allows for the name type or any other type.

C.Grouping

1.There are three group designations within the W3C XML Schema language.

a.xsd:all, in which an element in the group must occur for the most once, but the order is not important.

b.xsd:choice, in which any one or more elements from a group should appear, and a certain subsection of group elements should appear in a certain order.

c.xsd:sequence, in which each element in the group should appear only once in the exact order specified.

D.Making a choice in a schema.

1.Schemas use the term xsd:choice in which only one of the referenced elements appears in the document.

2.The xsd:choice element is allows minOccurs and maxOccurs attributes that let a user define the number of selections that can be made from a choice.

F.Importing elements.

Insider Information

Within a schema, elements from external namespaces can be imported. A schema can use the elements imported from multiple namespaces. However, before importing the elements, they must be globally declared within the external schema. Importing from other schemas enables types from different target namespaces to be used together.

1.Schemas allow elements to be imported with the xsd:import element by attaching to a different namespace derived from a different schema document.

2.Schemas also work with XLink.

3.This is done through the xsd:import’s schemaLocation attribute, which informs the XML processor where to find the correct schema to import.

4.When the schema gets imported, its attributes can be added to an xsd:compleType by assigning it an xsd:attribute child that has a ref attribute pointing to the attribute to be attached.

Student Exercise

Create a Complex Type element

This exercise will enable the students to create a complex Type element. Declare PRODUCT as a complex type called prdt. The data type prdt should contain other simple type elements, PRODUCTNAME, DESCRIPTION, PRICE, and QUANTITY. The sequence in which these elements must appear inside the PRODUCT element is specified using the sequence element.

Answer

<xsd: Complex Type name=”prdt”>

<xsd: sequence>

<xsd: element name=”PRODUCTNAME” type=”xsd:string”/>

<xsd: element name=”DESCRIPTION” type=”xsd:string”/>

<xsd: element name=”PRICE” type=”xsd:positiveInteger”/>

<xsd: element name=”QUANTITY” type=”xsd:nonNegativeInteger”/>

</xsd: sequence>

</xsd: complex Type>

For More Information

  • Elements and attributes in XML schema:
  • Simple type elements in a schema:
  • XML Namespaces:

Chapter Summary

  • DTDs employ its own syntax, which is not the same as xml syntax. DTDs cannot describe the actual data contained in elements.
  • DTDs only work for three things – a basic text string, the basic text string mixed in with other child elements, and a set of child elements.
  • Schema consists of two element types – Simple and Complex.
  • A simple type element is an element that has only text and does not have either attributes or child elements.
  • A complex type contains element and attribute declarations.
  • Schemas employ two namespaces.
  • The first namespace employed in a schema is the XML Schema namespace itself and the second is a target namespace.
  • Binary, Boolean, Numeric, String, Time, URI reference, and XML are examples of simple type elements.
  • Numeric data types are divided into three basic categories. They are Integer and floating point, Finite size numbers, and Signed and unsigned numbers.
  • Simple elements are allowed to be either fixed or revert to the default value.
  • The default value automatically assigns to an element when nothing else is indicated.
  • There are two types of facets, a fundamental facet that defines a value space, and a non-fundamental or constraining facet that puts a constraint on the allowed value.
  • It is possible to make simple data types.
  • Complex schemas can contain both, attributes and child elements.
  • There are three group designations within the W3C XML Schema language. They are xsd:all, xsd:choice, and xsd:sequence.
  • Schemas use the term xsd:choice. When xsd:choice is used, then only one of the referenced elements appears in the document.
  • Schemas allow elements to be imported with the xsd:import element.

Assessment Quiz

The following quiz will help you gauge the level of understanding of your students.

Questions

1.What is a target namespace?

2.What is a fundamental facet?

3.A ______or constraining facet puts a constraint on the allowed value.

4.______can contain both, attributes and child elements.

5.______uniquely identifies attribute and element names.

6.A (n)____ element has no attributes or elements for content.

A.simple

B.complex

C.schema

D.user-defined

7.A ____ type contains element and attribute declarations.

A.simple

B.xml

C.complex

D.parent

8.In ____, each element in a group must occur for the most once, but the order is not important.

A.xsd:sequence

B.xsd:all

C.xsd:choice

D.xsd:string

9.In xsd:sequence, each element should appear only once in the exact order specified.

A.True

B.False

10.The xsd:float consists of 64-bit floating-point number.

A.True

B.False

Answers

1.A target namespace is one of the namespaces employed by schema for the tags that are created in any instance of a particular schema.

2.A fundamental facet is a facet that defines a value space.

3.A non-fundamental or constraining facet puts a constraint on the allowed value.

4.Complex schemas can contain both, attributes and child elements.

5.Namespace uniquely identifies attribute and element names.

6.A. A simple element has no attributes or elements for content.

7.C. A complex type contains element and attribute declarations.

8.B. In xsd:all, each element in a group must occur for the most once, but the order is not important.

9.True – In xsd:sequence, each element should appear only once in the exact order specified.

10.False – The xsd:float consists of 32-bit floating-point number.

1