xml s chema b asics sd2520 databases using xml and jquery chapter 12 tech.edu/itt/clikslogin

31
XML SCHEMA BASICS SD2520 Databases using XML and Jquery Chapter 12 http://www.distance- education.itt-tech.edu/itt/ clikslogin

Upload: erin-dean

Post on 22-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

XML SCHEMA BASICS

SD2520Databases using XML and JqueryChapter 12http://www.distance-education.itt-tech.edu/itt/clikslogin

Page 2: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

XML SCHEMA

The specifications for XML include the syntax for a schema language: A way to define what a valid XML document

could contain. This language, known as a DTD (Document

Type Definition). It enables users to define elements,

attributes, and their relationships for any application of XML needed.

Page 3: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

NEW SCHEMA LANGUAGE In 2001, the W3C developed a new schema language to address many of the shortcomings of DTD. This schema language was named XML Schema,

which is admittedly confusing because DTDs are a type of XML schema (lower case “s”).

Even still, it’s most often called XML Schema, though it is occasionally called XML Schema Definition (XSD).

Version 1.1 of the language is known as XML Schema Definition Language (XSDL).

For this book, we use the name XML Schema, which is still the most widely recognized name of them all.

Page 4: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

XML SCHEMA XML Schema, written in XML itself, is deeper and

more powerful than a DTD. A few examples of its strength include its system

of data types that let you specify when an element should contain one of these: an integer or a period of time or a string.

It also lets you define both local and global elements, allowing two elements to have different definitions, sharing the same name.

XML Schema gives you much more control over the contents of an XML document, and will likely supplant DTDs as the most widely accepted schema language in the near future.

Page 5: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

WORKING WITH XML SCHEMA

An XML Schema specifies the structure of valid XML documents (Figure 12-1) by defining: a set of elements their relationships to each other the attributes that they can contain.

In a DTD, all XML elements are defined using one element type.

In XML Schema, an XML element can be defined as either a simple type or a complex type.

A simple type is an XML element that only contains text

A complex type is an XML element that contains child elements and/or attributes.

Page 6: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

FIGURE 12-1 Here is one of the first XML documents used in the book.

FIGURE 12-2 These elements are defined using XML Schema’s built-in simple data types.

The name and location elements must be strings, and the height element must be an integer.

Page 7: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

SIMPLE VS. COMPLEX TYPES

Where simple type elements describe the text of an XML document, complex type elements describe its structure.

Page 8: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

SIMPLE VS. COMPLEX TYPES

There are four kinds of complex type elements: those that contain child

elements; those that contain both child

elements and text; those that contain only text; those that are empty.

Page 9: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

REMEMBER

One of the benefits of XML Schema is that it uses XML syntax, which you probably already know by now

Because an XML Schema is an XML document, it must: begin with an XML

declaration have one root

element be well-formed, just

like all other XML documents

Page 10: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

NOTE

The W3C is working towards the completion of XML Schema Version 1.1.

It is being developed to fix bugs and make improvements where possible, while maintaining the same basic scope as (and remaining mostly compatible with) version 1.0.

You can find up-to-date information at the W3C’s XML Schema Working Group site at: www.w3.org/XML/Schema.

Page 11: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

BEGINNING A SCHEMA – PAGE 209 An XML Schema is a text-only document, and begins with a

standard XML declaration. It is customarily saved with an .xsd extension, and its root

element must be schema. To begin an XML Schema:1. At the top of your document, type <?xml version=”1.0”?

>. 2. Type <xs:schema to define the root element. The xs: is a

namespace prefix (see Tip on namespaces below).3. Then, type mlns:xs=”http://www.3.org/2001/XMLSchema”

to declare the XML Schema namespace (xmlns). This also declares that the elements and data types that are part of this namespace should be prefixed with xs: as you saw in Step 2 above.

4. Type > to complete the root element’s tag. 5. Leave a few empty lines for your XML Schema’s rules. 6. Finally, type </xs:schema> to complete the root element,

and the XML Schema document itself (Figure 12-5).

Page 12: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

AN XML SCHEMA

FIGURE 12-5 An XML Schema is

an XML document itself.

The root element must be schema, and its namespace is declared to be the W3C’s XML Schema namespace.

Page 13: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

TIPS A namespace is a “space” in

which names reside. Names that are part of one

namespace are not the same as names that are part of another namespace (even if they are spelled the exact same way).

The W3C created a namespace which contains all XML Schema elements and data types. This namespace is declared in Step 3 above.

Once declared, in order to indicate that a particular element or data type should be considered part of the W3C’s XML Schema namespace, it must start with the xs: namespace prefix (Figure 12-6).

Page 14: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

ASSOCIATING AN XML SCHEMA WITH AN XML DOCUMENT

To validate an XML document against an XML Schema, you must specify the location of the XML Schema in the XML document itself.

To declare the XML Schema and its location: 1. Inside the definition of the root element of your

XML document, type xmlns:xsi=“http://www.w3.org/2001/ XMLSchema”. This allows you to define the location of your XML Schema in the next two steps.

2. Type xsi:noNamespaceSchemaLocation=3. Finally, type “xsd.uri”, where xsd.uri is the

location of the XML Schema file against which you want to validate your XML file (Figure 12-7).

Page 15: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

LOCATION OF THE XML SCHEMA FILE

FIGURE 12-7 In order to associate an XML Schema with an XML document, you must declare the XML Schema document location within the root element of the XML document

Most XML parsers can validate XML documents against a declared XML Schema (Figure 12-8). If the XML parser you are using cannot, there are validators available online that can. The W3C has a validator at: www.w3.org/2001/03/webdata/xsv.

There are other validators listed at: www.w3.org/XML/Schema. The “xsd.uri” in Step 3 above can refer to a file on the Internet, local area network, or your local computer. Step 1 above declares the XML Schema Instance namespace which includes the xsi:noNamespaceSchemaLocation attribute used in Step 2, along with a few other namespace attributes.

Page 16: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

XML SCHEMA ASSOCIATION

FIGURE 12-8 Here, I’ve added the

XML Schema association to the XML document from Figure 12-1.

Then, when checked, this XML document is valid against the XML Schema in Figure 12-6.

Page 17: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

ANNOTATING SCHEMAS

Since an XML Schema is an XML document, you can include standard XML comments in your XML Schema documents (see page 16 ).

In addition to these standard comments, XML Schema offers the ability to add more structured comments to your document.

XML comments are readable by people, they are ignored by parsers, and often not passed through during XML transformations.

XML Schema comments (annotations, as they are called), can be parsed and processed, because they are elements themselves (Figure 12-9).

Page 18: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

TO ANNOTATE XML SCHEMAS:

1. Type <xs:annotation>.2. Next, type <xs:documentation> to begin the comment. 3. Type the comment. 4. Type </xs:documentation> to complete the comment. 5. Finally, type </xs:annotation> to complete the annotation.

FIGURE 12-9 An annotation helps you document the XML Schema. It can facilitate future revisions.

Page 19: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

XSD (XML SCHEMA DEFINITION)

XSD is a Recommendation of the World Wide Web Consortium (W3C), specifies how to formally describe the elements in an Extensible Markup Language (XML) document.

It can be used by programmers to verify each piece of item content in a document.

They can check it adheres to the description of the element it is placed in.

http://en.wikipedia.org/wiki/XML_Schema_(W3C)

Page 20: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

XSD: EXPRESSING A SET OF RULES

Like all XML schema languages, XSD can be used to express a set of rules to which an XML document must conform in order to be considered 'valid' according to that schema.

However, unlike most other schema languages, XSD was also designed with the intent that determination of a document's validity would produce a collection of information adhering to specific data types.

Such a post-validation infoset can be useful in the development of XML document processing software.

The schema language's dependence on specific data types has provoked criticism

http://en.wikipedia.org/wiki/XML_Schema_(W3C)

Page 21: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

SCHEMAS AND SCHEMA DOCUMENTS

A schema is an abstract collection of metadata, consisting of a set of schema components: chiefly element and attribute declarations and complex and simple type definitions.

These components are usually created by processing a collection of schema documents, which contain the source language definitions of these components.

In popular usage, however, a schema document is often referred to as a schema.

http://en.wikipedia.org/wiki/XML_Schema_(W3C)

Page 22: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

SCHEMA DOCUMENTS: NAMESPACE

Schema documents are organized by namespace: all the named schema components belong to a target namespace, and the target namespace is a property of the schema document as a whole.

A schema document may include other schema documents for the same namespace, and may import schema documents for a different namespace.

http://en.wikipedia.org/wiki/XML_Schema_(W3C)

Page 23: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

ASSESMENT! When an instance document is validated against a

schema (a process known as assessment) The schema to be used for validation can either be

supplied as a parameter to the validation engine, or it can be referenced directly from the instance document using:

Two special attributes: xsi:schemaLocation xsi:noNamespaceSchemaLocation. (The latter mechanism requires the client invoking validation to

trust the document sufficiently to know that it is being validated against the correct schema. "xsi" is the conventional prefix for the namespace "http://www.w3.org/2001/XMLSchema-instance".)

XML Schema Documents usually have the filename extension ".xsd". A unique Internet Media Type is not yet registered for XSDs, so "application/xml" or "text/xml" should be used, as per RFC 3023.

Page 24: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

MAIN SCHEMA COMPONENTS

Element declarations, which define properties of elements.

These include the element name and target namespace.

Elements may have integrity constraints: uniqueness constraints determining that particular values must be unique within the subtree rooted at an element, and referential constraints determining that values must match the identifier of some other element.

Element declarations may be global or local, allowing the same name to be used for unrelated elements in different parts of an instance document.

Page 25: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

MAIN SCHEMA COMPONENTS

Attribute declarations, which define properties of attributes.

The properties include the attribute name and target namespace.

The attribute type constrains the values that the attribute may take.

An attribute declaration may also include a default value or a fixed value (which is then the only value the attribute may take.)

Page 26: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

MAIN SCHEMA COMPONENTS

Simple and complex types. These are described in the following section.

Model group and attribute group definitions. These are essentially macros: named groups of elements and attributes that can be reused in many different type definitions.

An attribute use represents the relationship of a complex type and an attribute declaration, and indicates whether the attribute is mandatory or optional when it is used in that type.

Page 27: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

MAIN SCHEMA COMPONENTS An element particle similarly represents

the relationship of a complex type and an element declaration, and indicates the minimum and maximum number of times the element may appear in the content.

As well as element particles, content models can include model group particles, which act like non-terminals in a grammar: they define the choice and repetition units within the sequence of permitted elements.

The wildcard particles are allowed, which permit a set of different elements (perhaps any element provided it is in a certain namespace).

Page 28: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

MAIN SCHEMA TYPES - COMPLEX Complex types describe the permitted content of an

element, including its element and text children and its attributes.

A complex type definition consists of a set of attribute uses and a content model.

Varieties of content model include element-only content, in which no text may appear (other than whitespace, or text enclosed by a child element); simple content, in which text is allowed but child elements are not; empty content, in which neither text nor child elements are allowed; and mixed content, which permits both elements and text to appear.

A complex type can be derived from another complex type by restriction (disallowing some elements, attributes, or values that the base type permits) or by extension (allowing additional attributes and elements to appear).

In XSD 1.1, a complex type may be constrained by assertions —X-Path 2.0 expressions evaluated against the content that must evaluate to true.

Page 29: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

MAIN SCHEMA TYPES - SIMPLE Simple types (also called data types) constrain the

textual values that may appear in an element or attribute.

This is one of the more significant ways in which XML Schema differs from DTDs. For example, an attribute might be constrained to hold only a valid date or a decimal number.

XSD provides a set of 19 primitive data types (anyURI, base64Binary, boolean, date, dateTime, decimal, double, duration, float, hexBinary, gDay, gMonth, gMonthDay, gYear, gYearMonth, NOTATION, QName, string, and time).

It allows new data types to be constructed from these primitives by three mechanisms: restriction (reducing the set of permitted values), list (allowing a sequence of values), and union (allowing a choice of values from several types).

Page 30: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin
Page 31: XML S CHEMA B ASICS SD2520 Databases using XML and Jquery Chapter 12  tech.edu/itt/clikslogin

CHAPTER 12 TIPS Whether you use XML comments, XML

Schema annotations (or both), commenting your XML Schema makes it much easier to work with your documents.

You can create annotations anywhere in the XML Schema, after the root element.

They can be placed: just after the xs:schema root element (to

comment on the entire schema), or just after individual element definitions (to give

more information about them), or both.