The formal rules for XML are a per document phenomenon. Rules are laid down in a document called a schema. As we have heard before the rules are the logical foundation for validation. No schema, no validity. A document may be well-formed but it can not valid without a schema.
Schemas serve two purposes:
Schemas are not omnipotent. The rules are enforced only at the time of validation, They are not part of a real time process.
There are three ways of defining rules for the XML documents. The Document Type Definition (DTD) language, which is native to the XML specification, is a schema language that is of relatively limited capability, but that also has other uses in XML aside from the expression of schemas. Two more expressive XML schema languages in widespread use are XML Schema (with a capital S) and RELAX NG. (This was quoted from Wikipedia). The following points out some examples. please refer to Wikipedia where the examples are presented.
A DTD defines the structure af an XML document. It names the root element and it's valid elements and their respective attributes. It defines the allowed order of elements, and the proper content of each element.
The DTD is the originally defined way of defining structure. It is written an a language not defined by XML.
Examples are deferred to the detailed sections below.
Due to lack of required expressiveness of the DTD, a new schema was defined. The XML Schema. ...
The XML Schema is itself an XML document. Take the following XML document
<?xml version="1.0" encoding="utf-8"?>
<Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="SimpleAddress.xsd">
<Recipient>Mr. Walter C. Brown</Recipient>
<House>49</House>
<Street>Featherstone Street</Street>
<Town>LONDON</Town>
<PostCode>EC1Y 8SY</PostCode>
<Country>UK</Country>
</Address>
The following schema, SimplaAddress.xsd
describes the rules:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="Recipient" type="xs:string" />
<xs:element name="House" type="xs:string" />
<xs:element name="Street" type="xs:string" />
<xs:element name="Town" type="xs:string" />
<xs:element name="County" type="xs:string" minOccurs="0" />
<xs:element name="PostCode" type="xs:string" />
<xs:element name="Country">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="FR" />
<xs:enumeration value="DE" />
<xs:enumeration value="ES" />
<xs:enumeration value="UK" />
<xs:enumeration value="US" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
RELAX NG is REgular LAnguage for XML Next Generation.
<book>
<page>This is page one.</page>
<page>This is page two.</page>
</book>
Describing schema for the above document:
<element name="book" xmlns="http://relaxng.org/ns/structure/1.0">
<oneOrMore>
<element name="page">
<text/>
</element>
</oneOrMore>
</element>