Escolar Documentos
Profissional Documentos
Cultura Documentos
CHAPTER 3
Introduction
In the previous chapter we learnt DTD that is Traditional way of validating an XML document, which
were inherited from SGML. Over many times people have complained to the W3C about the
complexity of DTDs and have asked for something simpler. W3C for the above complaint assigned a
committee to work on the problem, and came up with a solution, which is more complex than DTDs
called XML Schemas. On the other hand XML Schemas are also far more powerful than DTDs ever-
were.
A Schema is a set of rules for constraining the structure and articulating the information set of
XML documents.
The name attribute establishes the name of the schema. The Xmlns attribute is very
important in that it establishes the namespace for the schema. This attribute must be set to
urn: schemas-microsoft-com: xml -data in order to use Microsoft's XML schema
implementation.
<Schema name="myschema" xmlns="urn:schemas-microsoft-com: xml -data">
<!--schema content goes here-->
</ Schema >
NOTE
Namespace are used in XML documents to guarantee uniqueness among element and attribute
names associated with a given XML vocabulary. Namespaces take the form of URLs, which are
often the familiar URLs, used to identify resources on the Web.
In addition to specifying the namespace for the schema, usually it is also necessary to specify the
namespace for XML schema data types. The data type namespace is typically assigned to the
xmlns:dt attribute and is set to urn: schemas-microsoft-com:datatypes. You must set this
namespace in order to use any of the XML schema datatypes, such as date, time, int and float.
The schema element can contain child elements of type AttributeType, ElementType and
Description. The AttributeType and ElementType elements define attribute types and element
types.
order The order of the child elements and groups contain within the element
The following are the examples of element types defined using the ElementType element:
Notice that the name and type elements are first declared using the ElementType element, and
then are identified within the content model of the session element using the element.
The type attribute is used to specify the type of the element. The value assigned to the type
attribute must be the name of an element type already declared in the schema.
The minoccurs and maxoccurs attributes are used to establish the number of times an element
can occur within a group or element type. Both attributes have default values of 1 in the XML-Data
note, which means that an element must occur exactly one by default.
The relationship between the minoccurs and maxoccurs Attributes and the number of times an
Element or Group can occur
Note
The table applies to the group element, because groups also have minOccurs and
maxOccurs attributes that serve the same purpose.
The following is an example of the element used to declare element instances within an element
type:
<ElementType name="location" content="textOnly"/>
<ElementType name="comments" content="textOnly"/>
<ElementType name="session" model="closed" content="eltOnly" order="seq">
<element type="location" minOccurs="1" maxOccurs="1"/>
<element type="comments" minOccurs="0" maxOccurs="1"/>
</ElementType>
order The order of the child elements contained within the group
minoccurs The minimum number of times the group must occur
maxOccurs The maximum number of times the group must occur
The order attribute works exactly like its counterpart in the ElementType element. The following
are acceptable values for this attribute:
The minOccurs and maxOccurs attributes play the exact same role in the group element as they
did in the element, which is constraining the number of times the group can Occur.
The name attributes specifies the name of the attribute type and is a required attribute. This name
must be unique among attributes within a given scope. The dt:type attribute specifies the data
type of the attribute.
The dt:values attribute is used to specify a list of possible values for enumerated attributes. This
attribute is applicable only when dt:type is set to enumeration. The list of enumerated attribute
values is specified as a single string with spaces between each possible value.
The following is an example of an enumerated attribute definition:
In this example, the available values that can be assigned to the type attribute are running,
cycling and swimming. Any value other than one of these three will be considered an error during
validation.
The default Attribute of the AttributeType element is used to establish the default value for the
attribute type. The following is an example of establishing the default value of an attribute:
The required attribute is basically a flag that is used to specify whether the attribute type is
required of the element in which it is defined. Acceptable values for the required attribute are yes
and no, which indicate the requirement of the attribute type.
The Attribute Element
The attribute element is used to declare an instance of an attribute for an element type. The
attribute element includes three attributes for describing additional information about an attribute
instance:
The type attribute is used to specify the type of the attribute. The value assigned to the type
attribute must be the name of an attribute type already declared in the schema. The type attribute
is what ties attribute instances to their associated attribute types. The default and required
attribute serve the same purposes as their equivalents in the AttributeType element, and they will
supercede the equalent attributes if they are set in the attribute type.
The following is an example of the attribute element used to declare attribute instances within an
element type.
In this example, the type and date attributes are first declared using the AttributeType element
and then associated with an element type using the attribute element.
Notice that the default value of the type attribute is set in the attribute element instead of the
AttributeType element.
Note
There is no constraint on the order of attributes within an element, but there can be no
more than one attribute of a given name per element.
The description Element
The last element used in XML Schema documents is the description element, which simply
provides a means of placing a text description within a schema. The description element is a text
only element that is designed for documentation purposes. You can use description element in
any way you choose to provide documentation about an XML Schema construct. The following is
an example of how you might add documentation to element type:
Example
<Schema name="Myschema" xmlns="urn:schema-microsoft-com:xml-data"
xmlns:dt="urn:schema-microsoft-com:datatypes">
-
-
-
</Schema>
The whole point of declaring the XML Schema data type namespace is so you can use the data
types it supports. The following is a list of these data types, which go far beyond the limited data
types supported in XML 1.0:
The following are the primitive data types available for use in XML Schema:
Employees.xml
<?xml version="1.0"?>
<employees xmlns="x-schema:employees.xml">
<employee>
<eid id="A100">A100</eid>
<ename>Surya</ename>
<sal>50000.00</sal>
<desig>CEO</desig>
<phno>3751135</phno>
<email>suryaactive@hotmail.com</email>
</employee>
<employee>
<eid id="A101">A101</eid>
<ename>Rajesh</ename>
<sal>30000.00</sal>
<desig>Director</desig>
<phno>3751238</phno>
<email>insbrajesh@rediffmail.com</email>
</employee>
</employees>
empSchema.xml
Note
Microsoft Schema extension is .xml, whereas W3C Schema file extension is .xsd
<?xml version="1.0"?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-
com:datatypes">
<!--
Above is a Microsoft namespace for Schemas data & datatypes
xmlns=>XML Namespace
urn=> Uniform Resource Namespace
dt=> datatype
-->
</Schema>
How do you associate a schema with this document as far as Internet Explorer is concerned?
You do so by specifying a default namespace attribute in the root element, and prefacing the
name of the schema file with x-schema: like this:
<?xml version="1.0"?>
<programming_team xmlns="x-schema:schema1.xml">
<programmer>Fred Samson</programmer>
<programmer>Edward</programmer>
</programming_team>
Here, I'm naming the schema file schema1.xml (IE Schema does not insist on any special
extension for schema file)
Creating Schema file you can name the schema using the name attribute in Schema.
One of the advantages of using schemas is that they allow you to specify the actual data types
that you want to use, but those data types weren't fully fleshed out at the time Microsoft decided
to implement schemas, so Microsoft implemented its own. To create a schema for Internet
Explorer, you set up a default namespace