Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML Schema Basics SD2520 Databases using XML and Jquery Chapter 12

Similar presentations


Presentation on theme: "XML Schema Basics SD2520 Databases using XML and Jquery Chapter 12"— Presentation transcript:

1 XML Schema Basics SD2520 Databases using XML and Jquery Chapter 12
tech.edu/itt/clikslogin

2 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.

3 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.

4 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.

5 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.

6 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.

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

8 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.

9 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

10 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:

11 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: At the top of your document, type <?xml version=”1.0”?>. Type <xs:schema to define the root element. The xs: is a namespace prefix (see Tip on namespaces below). Then, type mlns:xs=” 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. Type > to complete the root element’s tag. Leave a few empty lines for your XML Schema’s rules. Finally, type </xs:schema> to complete the root element, and the XML Schema document itself (Figure 12-5).

12 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.

13 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).

14 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: Inside the definition of the root element of your XML document, type xmlns:xsi=“ XMLSchema”. This allows you to define the location of your XML Schema in the next two steps. Type xsi:noNamespaceSchemaLocation= 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).

15 location of the XML Schema file
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: There are other validators listed at: 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. 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

16 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.

17 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).

18 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.

19 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.

20 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

21 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.

22 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.

23 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 " 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.

24 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.

25 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.)

26 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.

27 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).

28 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.

29 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).

30

31 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.


Download ppt "XML Schema Basics SD2520 Databases using XML and Jquery Chapter 12"

Similar presentations


Ads by Google