Você está na página 1de 12

XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...

XML Schema
Abstract
DTDs are very useful if you want to describe the structure of a document. They have been used to do this for well over
a decade. Unfortunately, they come up short when it comes to the needs of XML application developers. Due to the
fact that XML is used, and will continue to be used, to create data-intensive applications, XML is not used to simply
markup a document--it's used to markup data.

DTD is CFG for document type. It lacks Data types, Inheritance, and Default values. Also, it is Non-XML.

XML Schemas express shared vocabularies and allow machines to carry out rules made by people. They provide a
means for defining the structure, content and semantics of XML documents. XML Schema is a language for defining
the structure of XML documents.

Introduction to XML Schema

A XML schema describes an XML markup language. Specifically it defines which elements and attributes are used in a
markup language, how they are ordered and nested, and what their data types are. The XML specification includes the
Document Type Definition (DTD), which can be used to describe XML markup languages and to validate XML
documents. While DTDs have proven very useful over the years, they are limited. The W3C created a new way to
describe markup languages called XML schema.

XML schema is an XML based alternative to DTD. An XML schema not only describes the structure of an XML
document, but also address data typing.

What is an XML Schema?


An XML schema is a predefined set of elements/attributes/values for defining "types", these are the the legal
building blocks of an XML document. A XML schema does the following:

define elements that can appear in a document

define attributes that can appear in a document

define which elements are child elements

defines the sequence in which the child elements can appear

defines the number of child elements

defines whether an element is empty or can include text

define default values for attributes

XML schema is an alternative and update to DTD. The main objectives of XML schema include:

1. XML format (and therefore accessibility to XML parsing tools)

2. extensibility, i.e., by allowing other schemas to be imported

3. finer control over data typing

4. support for XML namespaces

The following is an example of a comment element, comment.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2000/08/XMLSchema">

1 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...

<xsd:element name="comment" type="xsd:string"/>


</xsd:schema>

The following is an example of an instance, comment.xml:

<x xmlns="http://www.curt.org/comment.xsd">
<comment>This is a great comment!</comment>
</x>

An XML schema document can play the same role for an XML document as an external DTD. It is included into a
document to be validated using the XML schema model. To reference an XML schema document from an XML
document, add two XML Infoset attributes to the document element of the XML document:

1. The first attribute declares the namespace for XMLSchema-instance. The prefix is normally xsi. The value of the
attribute is fixed.

2. The second attribute specifies the location of the XML schema document. In the simplest case, there is no
namespace associated with this location. Therefore the value of the attribute is the file location relative to the
current XML file.

At the start of your schema you need to place a few lines of code, known as a Prolog, which defines the markup that
follows as a schema, and also defines what type of schema it is. The prolog comes immediately after the XML
declaration, which is the first line of code (after any comments).

The Xerces project provides an example of an XML document that refers to an XML schema document. The document
element of the XML document is personnel:

<?xml version="1.0" encoding="UTF-8"?>


<personal xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xsi:noNamespaceSchemaLocation='personal.xsd'>
...
</personnel>

What is in an XML Schema file?


The contents of an XML schema file are fully described in a DTD, which is an appendix of the first of two parts of the
XML schema standard, http://www.w3.org/TR/xmlschema-1/.

Like any XML document, an XML schema document contains one document element. Its name is <schema>. Like a
DTD, a schema can contain many different kinds of statements, but the most common ones are:

1. element declarations (similar to <!ELEMENT ...> declarations in a DTD)

2. type definitions (akin to parameter entities in a DTD)

The attribute list declarations, which are a common part of DTDs, can be in one of two places in an XML schema
document: embedded within an element declaration, or as a standalone group, which will be referenced within one or
more element declarations.

?XML schemas are the Successors of DTDs


A significant difference between schemas and DTDs is that schemas define many basic data types: string, boolean,
float, double, decimal, timeDuration, recurringDuration, binary, and uri. Each of these so-called primitive data types
has distinctive lexical representation and other characteristics which delimit their possible values. By typing the data
enclosed in an XML document, a schema makes the document computable in ways not possible with the simple
(mostly string-based) data types that are present in DTDs. XML schema was originally proposed by Microsoft, but is
now a W3C recommendation.

2 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...

We think that very soon XML schemas will be used in Web applications as a replacement for DTDs. Here are the
reasons why:

1. XML schemas are easier to learn than DTD

2. XML schemas are extensible to future additions

3. XML schemas are richer and more useful than DTDs

4. XML schemas are written in XML

5. XML schemas support data types

6. XML schemas support namespaces

XML Schema Instance Document


An XML schema instance document is an XML document that conforms to a particular schema.

Neither instances nor schemas need to exist as documents. They could be any of:

Streams of bytes sent between applications

Fields in a database record

Collections of XML Infoset "Information Items"

Schema Elements and Subelements


Each schema has a schema element and a variety of subelements

Subelements determine appearance of elements and their content in instance documents

Types of subelements are element, complexType, and simpleType

Elements begin with xsd: to associate them with XML schema namespace through declaration.

<xsd:schema xmlns:xsd="http://www.w3.org/2000/08/XMLSchema">

where the xsd prefix identifies elements as part of the XML schema language

Resources--XML Schema specification:


http://www.w3.org/TR/xmlschema-0/Primer

http://www.w3.org/TR/xmlschema-1/Structures

http://www.w3.org/TR/xmlschema-2/Datatypes

XML Schema Data Types

A schema describes the structure of an XML document in terms of complex types and simple types. Complex types
describe how elements are organized and nested. Simple types are the primitive data types contained by elements
and attributes.

One of the benefits of using schema is the ability to control data types for elements and attributes. By allowing
developers to dictate the type of data an element or attribute can contain, schema are much more suited for XML
application development.

3 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...

Simple Types
The XML schema sepcification defines many stardard smiple types, called built-in types. These built-in types are the
standard building blocks of an XML schema document. Simple types cannot be broken down into constituent parts.
In other words, a siimple element type will not contain other elements, it will contain only data. Attributes must be
Simple Types. Simple type elements cannot themselves have attributes or contain other elements.

The following are the examples of the simple types:

<xsd:element name="state" type="xsd:string" />


<xsd:element name="zip" type="xsd:decimal" />
<xsd:attribute name="country" type="xsd:NMTOKEN"
use="fixed" value="US"/>

You can derive new simple types types from existing types by restricting the type to a subset of its normal values. An
xsd:simpleType element defines the restricted type. The name attribute of xsd:simpleType assigns a name to the
new type. An xsd:restriction child element specifies what type is being restricted via its base attribute. Facet
children of xsd:restriction specify the constraints on the type. For example, this xsd:simpleType element defines a
independentYear as any year from 1776 on:

<xsd:simpleType name="independentYear">
<xsd:restriction base="xsd:gYear">
<xsd:minInclusive value="1776"/>
</xsd:restriction>
</xsd:simpleType>

Then you declare the year element like this:

<xsd:element type="independentYear" />

There is another more detailed example later under "create your own data type".

Complex Types
A schema may declare complex types, which define how elements that contains other elements are organized.
Complex types allow elements in their content and may carry attributes. For example, the USAddress schema type
defines a US postal address, which contains name, street, city, state and zip:

<xsd:complexType name="USAddress">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="street" type="xsd:string" />
<xsxd:element name="city" type="xsd:string" />
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"
use="fixed" value="US"/>
</xsd:complexType>

As in the above USAddress example, most complexType declarations in schemas contains a sequence element that
lists one or more elements definitions. The sequence element describes which elements are nested in the
complexType, the type of each nested elements, and the order of the nested elements.

In a complexType, the number of times of an element occurs in an XML document is controlled by the maxOccurs
and minOccurs attributes. For example, in the above USAddress definition, the street element can occur at least one
time and no more than two times:

<xsd:complexType name="USAddress">

4 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...

<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="street" type="xsd:string" />
minOccurs="1" maxOccurs="2" / >
<xsxd:element name="city" type="xsd:string" />
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"
use="fixed" value="US"/>
</xsd:complexType>

Note that the default value for both maxOccurs and minOccurs is "1". So if there attributes are omitted, the element
must be present exactly once. If an element is optional, set the minOccurs to "0". If an element can occur any
number of times, set minOccurs="0" and maxOccurs="unbounded". In other words:

Must occur - minOccurs="1" maxOccurs="1"

Optional - minOccurs="0" maxOccurs="1"

Kleene closure - minOccurs="0" maxOccurs="unbounded"

Besides the sequence element, the all element is also used in the ComplexType definition. Unlike the sequence
element, which defines the exact order of child elements, the XML schema all element allows the elements in it to
appear in any order. Each element in an all group may occur once or not at all; no other multiplicity is allowed. In
other words, in the all group, the minOccurs is either "0" or "1", the default is "1", the maxOccurs is always "1".

Note: Only single elements may be used in an all group, it can't include other groupings like sequence or all. The
following is the USAddress example that is defined using the all element:

<?xml version=1.0 encoding="UTF-8" ?>


<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:address="http://www.postaddress.com/address"
targetNamespace="http://www.postaddress.com/address"
...
<complexType name="USAddress">
<all>
<element name="name" type="string" />
<element name="street1" type="string" />
<element name="street2" type="string" minOccurs="0" />
<element name="apt" type="string" minOccurs="0"/>
<element name="city" type="string" />
<element name="state" type="string"/>
<element name="zip" type="decimal" />
</all>
</xsd:complexType>
...
<schema>

Note: that the names of XML schema types are case-sensitive. When an element declares that it is of a particular
type, it must specify both the namespace and the name of that type exactly as the type declares them.

When do you use the complexType element and the


simpleType element?
Use the complexType element when you want to define child elements and/or attributes of an element

Use the simpleType element when you want to create a new type that is a refinement of a built-in type (string,

5 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...

date, gYear, etc)

Create Your Own Data Types


A new datatype can be defined from an existing datatype (called the "base" type) by specifying values for one or more
of the optional facets for the base type. For example,the string primitive datatype has six optional facets: length,
minLength, maxLength, pattern, enumeration, whitespace (legal values: preserve, replace, collapse)

The followin is an example of Creating a New Datatype by Specifying Facet Values:

? This creates a new data type called "TelephoneNumber" ?

<xsd:simpleType name="TelephoneNumber">

?Elements of this type can hold string values?

??? <xsd:restriction base="xsd:string">

? But the string length must be exactly 8 characters long and?

??????? <xsd:length value="8"/>

? The string must follow the pattern: ddd-dddd, where 'd' represents a 'digit'?

??????? <xsd:pattern value="\d{3}-\d{4}"/>

??? </xsd:restriction>

</xsd:simpleType>??????

Obviously, in this example the regular expression makes the length facet redundant

The following example is creates a new type called US-Flag-Colors. An element declared to be of this type must have
either the value red, or white, or blue:

<xsd:simpleType name="US-Flag-Colors">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="red"/>
<xsd:enumeration value="white"/>
<xsd:enumeration value="blue"/>
</xsd:restriction>
</xsd:simpleType>

General Form of Creating a New Datatype by Specifying Facet Values

6 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...

Creating a simpleType from another simpleType

We can create a simpleType using one of the built-in datatypes as our base type. However, we can create a
simpleType that uses another simpleType as the base.

<xsd:simpleType name= "EarthSurfaceElevation">


<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="-1290"/>
<xsd:maxInclusive value="29035"/>
</xsd:restriction>
</xsd:simpleType>

The BostonAreaSurfaceElevation simpleType uses EarthSurfaceElevation as its base type.

<xsd:simpleType name= "BostonAreaSurfaceElevation">


<xsd:restriction base="EarthSurfaceElevation">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="120"/>
</xsd:restriction>
</xsd:simpleType>

Local References to Globals


Local elements can reference global elements by name, as shown in the following example:

<xsd:schema xmlns:xsd="http://www.w3.org/2000/08/XMLSchema">
<xsd:element name="person" type="PersonType"/>
<xsd:element name="comment" type="xsd:string"/>
<xsd:complexType name="PersonType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element ref="comment" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

References to global attributes are also possible

7 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...

Inheritance and Extensions


Base types can be inherited and extended. For example:

<complexType name="Address">
<sequence>
<element name="name" type="string"/>
<element name="street" type="string"/>
<element name="city" type="string"/>
</sequence>
</complexType>
<complexType name="USAddress">
<complexContent>
<extension base="ipo:Address">
<sequence>
<element name="state" type="ipo:USState"/>
<element name="zip" type="positiveInteger"/>
</sequence>
</extension>
</complexContent>
</complexType>

Overriding is possible via restriction only.

Declarations
Element Declarations
Elements declarations were mentioned in the Data types section. Those elements are declared as children of a
complex type. For example:

<xsd:complexType name="USAddress">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="street" type="xsd:string" />
<xsxd:element name="city" type="xsd:string" />
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"
use="fixed" value="US"/>
</xsd:complexType>

These elements in the example can only be referred to inside the complex type USAddress. In addition to declaraing
elements inside a complex type, a schema may also declare global elements, which XML documents can refer to
directly. Global elements are declared as direct children of the schema. In the following example, both the person and
the comment elements are global:

<xsd:schema xmlns:xsd="http://www.w3.org/2000/08/XMLSchema">
<xsd:element name="person" type="PersonType"/>
<xsd:element name="comment" type="xsd:string"/>
<xsd:complexType name="PersonType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element ref="comment" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

Use the following attributes to declare an element:


"name", "type" defined occurance of a element

8 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...

"ref" references existing element declared elsewhere in the schema


"minOccurs", "maxOccurs" control number of appearances

The following are all legal examples of element declarations:

?? <xsd:element?? name="billTo"??? type="USAddress" />

?? <xsd:element?? ref="comment"??? minOccurs="0"/>

?? <xsd:element?? name="items"???? type="Items" />

Note: An element declaration can have a type attribute, or a complexType child element, but it cannot have both a
type attribute and a complexType child element. The following example is not allowed:

<xsd:element name="A" type="foo">


<xsd:complexType
......
</xsd:complexType>
</xsd:element

Attribute Declarations
Attributes declarations, like element declarations, are expressed as elements in an XML schema. The following
example shows attributes declarations:

<xsd:complexType name="USAddress" >


<xsd:sequence>
<xsd:element name="state" type="xsd:string" />
<xsd:element name="zip" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN" use="fixed" value="US"/>
</xsd:complexType>

To define an attribute:

"use" indicates whether the attribute is required or optional (fixed or default)


"value" provides any value that is called for

Note: An attribute may also have occurrence constraints, but they are different from those of elements. Instead of
maxOccurs and minOccurs, attribute types declare the use occurrence constraint, which may be "required",
"optional", or "prohibited", indicating that the attribute must, may, or may not be used, respectively. The default is
"optional". An attribute may also be declared as "fixed": A fixed value is assigned to the attribute no matter what
value appears in the XML instance document.

An attribute may also have a default value, to the assigned if no value is explicitly declared in the XML document. But
the default attribute can be used only when the use attribute is "optional". It doesn't make sense when the use is
"required" or "prohibited". For if it is "required", the attribute must appear in the XML document. If it is "prohibited",
the attribute is not allowed in the XML document.

Sequences and Choices


Sequences define an ordered group of elements, while Choices define a group of mutually exclusive elements or
compositors. For example:

<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>

9 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...

<xsd:choice>
<xsd:group ref="shipAndBill" />
<xsd:element name="singleUSAddress" type=" USAddress" />
</xsd:choice>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items" type="Items" />
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date" />
</xsd:complexType>

<xsd:group name="shipAndBill">
<xsd:sequence>
<xsd:element name="shipTo" type="USAddress" />
<xsd:element name="billTo" type="USAddress" />
</xsd:sequence>
</xsd:group>

Definitions vs. Declarations


Definitions create new simple and complex types, for example:

<xsd:complexType name="USAddress" >


<xsd:sequence>
<xsd:element name="name"type="xsd:string" />
<xsd:element name="street" type="xsd:string" />
<xsd:element name="city"? type="xsd:string" />
<xsd:element name="state"type="xsd:string" />
<xsd:element name="zip"type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"
use="fixed" value="US"/>
</xsd:complexType>

Declarations are not themselves types

Enable elements and attributes

<xsd:complexType name="USAddress" >


<xsd:sequence>
<xsd:element name="name"type="xsd:string" />
<xsd:element name="street" type="xsd:string" />
<xsd:element name="city"type="xsd:string" />
<xsd:element name="state"type="xsd:string" />
<xsd:element name="zip"type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"use="fixed" value="US"/>
</xsd:complexType>

The advantage of defining USAddress 's element declarations and wrapping them in a named type is that now this
type can be reused by other elements.

Please note that:

<xsd:element name="A" type="foo"/>


<xsd:complexType name="foo">
<xsd:sequence>
<xsd:element name="B" .../>
<xsd:element name="C" .../>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

10 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...

is equivalent to:

<xsd:element name="A">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="B" .../>
<xsd:element name="C" .../>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

An XML Schema Example


An XML Schema Example
The following is the XML schema that defines the shipOrder:

<xsd:schema xmlns:xsd="http://www.w3.org/1999/XMLSchema">

<xsd:element name="shipOrder" type="order"/>

<xsd:complexType name="order">

<xsd:element name="shipTo" type="shipAddress"/>

<xsd:element name="items" type="cdItems"/>

</xsd:complexType>

<xsd:complexType name="shipAddress">

<xsd:element name="name" type="xsd:string"/>

<xsd:element name="street" type="xsd:string"/>

<xsd:element name="address" type="xsd:string"/>

<xsd:element name="country" type="xsd:string"/>

</xsd:complexType>

<xsd:complexType name="cdItems">

<xsd:element name="item" type="cdItem"/>

</xsd:complexType>

<xsd:complexType name="cdItem">

<xsd:element name="title" type="xsd:string"/>

<xsd:element name="quantity"

type="xsd:positiveInteger"/>

<xsd:element name="price" type="xsd:decimal"/>

</xsd:complexType>

</xsd:schema>

The XML schema above defines the element <shipOrder> to be of the type order.

The order is a complex type element consisting of the elements <shipTo> and <items>.

The <shipTo> element is of the type shipAddress - a complex type element consisting of the elements <name>,
<street>, <address>, and <country>.

The <items> element is of the type cdItems - a complex type element consisting of <item> elements.

11 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...

The <item> element is of the type cdItem - a complex type element consisting of <title>, <quantity>, and <price>
elements.

The <title> element is a normal element of the type string.

An XML Shipping Order Example


The following is an XML document defined by the above XML schema:

<?xml version="1.0"?>

<shipOrder>

<shipTo>

<name>Tove Svendson</name>

<street>Ragnhildvei 2</street>

<address>4000 Stavanger</address>

<country>Norway</country>

</shipTo>

<items>

<item>

<title>Empire Burlesque</title>

<quantity>1</quantity>

<price>10.90</price>

</item>

<item>

<title>Hide your heart</title>

<quantity>1</quantity>

<price>9.90</price>

</item>

</items>

</shipOrder>

The XML document consists of a root element <shipOrder>, with two child elements <shipTo> and <items>.

The <items> element contains <item> elements. An <item> element contains <title>,<quantity>, and <price>
elements.

12 of 12 08/12/2008 22:43

Você também pode gostar