Presentation is loading. Please wait.

Presentation is loading. Please wait.

McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Schemas Ellen Pearlman Eileen Mullin Programming the Web Using XML.

Similar presentations


Presentation on theme: "McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Schemas Ellen Pearlman Eileen Mullin Programming the Web Using XML."— Presentation transcript:

1 McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Schemas Ellen Pearlman Eileen Mullin Programming the Web Using XML

2 5-2 Learning Objectives 1.Understand the difference between a DTD and a XML Schema 2.Learn about Schema Namespaces 3.Differentiate between SimpleType Schema data 4.Manipulate ComplexType Schema data 5.Work with child elements 6.Set MinOccur and MaxOccur instances 7.Work with choices in Schemas

3 5-3 The Origins of XML Schema The W3C had already realized a variety of limitations inherent in DTDs when XML 1.0 was accepted in February 1998. The problem with DTDs is that their syntax is not particularly flexible. Documents follow one syntax and DTDs follow another. This led to the W3C creating a Schema Recommendation to set the framework of a new modeling definition, called the XML Schema Definition Language (XSD).

4 5-4 What is XML Schema? XML Schema is already constructed as well-formed XML, and edits correctly with most XML tools providing consistent validation models through sets of XML instances. It is especially handy in defining datatypes and patterns. It allows much more sophisticated descriptions and more importantly, shared markup vocabularies. It does this by creating a structure that will either accept or reject specific XML data before performing a function on that data.

5 5-5 Some Problems With DTDs The most difficult aspect of a DTD is that it employs its own syntax, which is not the same as XML syntax. DTDs can’t describe the actual data contained in elements. They can designate that it is indeed character data, or parsed character data, but they can’t say if it should be formatted or structured in a specific format. DTDs have limited ability to deal with namespaces. DTDs are not context sensitive and cannot filter information based on what is inside of it.

6 5-6 Example Schema: Types of Speed

7 5-7 Namespaces Namespaces uniquely identify attribute and element names. Schemas actually employ not one, but two namespaces. The first is the XML Schema namespace itself and the second is a target namespace for the tags that are created in any instance of a particular schema. An example of this might be as follows: xmlns:xs="http://www.w3.org/2001/XMLSchema"

8 5-8 Developing Schemas Namespaces are particularly important in schemas because they are set up to process data and documents from different sources. The names that a schema defines are commonly called the “target” namespace. However, the syntax of a namespace can be confusing. If a definition in a schema refers to other namespaces, they are called “source” namespaces. Every schema has one target and potentially numerous source namespaces.

9 5-9 Simple and Complex Element Types There are two element types in a schema. –The first is simple. A simple element is precisely that, it has absolutely no attributes or elements for content. –The second kind is the complex type. A complex type contains element and attribute declarations. This differs from a DTD in that a DTD has content that is a string within the element declaration, and has attributes declared separately in a list of attributes.

10 5-10 Thinking About Validation An XML document that uses a DTD has to declare a DTD declarative statement that points to the DTD and where it resides. It does this so the statement can be validated against the DTD. This makes it difficult to validate a DTD from a third party because you have to go and locate their DTD, wherever it might reside, to validate it.

11 5-11 Thinking About Validation (2) With the schema specification there is a lot more room to maneuver. Different documents can be affiliated with different schemas. The instance document has to point to the schema. The way to do this is to attach the schema to a particular document, which can be done by applying the attribute xsi:noNamespaceSchemaLocation and attach it to the document’s root element.

12 5-12 Categories of Simple Types There are 44 built-in simple types in the W3C XML Schema language. They are divided into seven basic groups: –Binary –Boolean –Numeric –String –Time –URI reference –XML

13 5-13 Numeric Data Types and Their Subcategories Numeric data types are divided into three basic categories: –Integer and floating point –Finite size numbers (like those in Java and C) –Signed and unsigned numbers

14 5-14 Making Your Own SimpleType It is possible to make up your own simple type. You do it by restricting the type that exists already (for example xsd:string) with an xsd:restriction element. The child element of xsd:restriction has an attribute that specifies what type it comes from.

15 5-15 Complex Schema Example: Book Catalog <BOOK xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="book.xsd"> XML Bart Simpson Lisa Simpson McGrawHill 300 2003 Marge Simpson $50.00 book.xsd

16 5-16 BookType Schema diagram in XML Spy

17 5-17 MinOccurs and MaxOccurs Attributes The minOccurs and maxOccurs attributes set how many times and how many variations of an element can exist.

18 5-18 Enhanced Grid View in XML Spy of Min/Max Occurrences for BOOKTYPE

19 5-19 Name Child Elements in XML Spy

20 5-20 Complex Types Defined in XML SPY Hierarchy View

21 5-21 Grouping In some instances, the ordering in which elements and their attributes appear is not critical. There are three group designations within the W3C XML Schema language that tell if and how the order of individual elements should be handled. They are: –xsd:all –xsd:choice –xsd:sequence

22 5-22 Making A Choice DTDs allow one to choose an element by using the pipe (|) symbol. Schemas use the term xsd:choice. When xsd:choice is used then only one of the referenced elements must appear in the document.

23 5-23 xsd:choice Example <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0"/> <xsd:element name="ILLUSTRATOR" type="xsd:string" maxOccurs="unbounded"/>

24 5-24 Importing Elements Schemas, unlike DTDs, allow the importing of elements with the xsd:import element. This is done through the xsd:import’s schemaLocation attribute. That attribute informs the XML processor where to find the correct schema to import. The namespace attribute in such a statement tells which elements and attributes the schema declares.

25 5-25 The End


Download ppt "McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Schemas Ellen Pearlman Eileen Mullin Programming the Web Using XML."

Similar presentations


Ads by Google