How to use XML Schemas in XML document

You can create any markup language by using XML due to XML standard flexibility.  This flexibility also raises a few problems.  You will not be sure that developers around the world using your XML format are following the rules. The solution is to create a formal document that states the rules of your custom markup language, which is called a schema. These rules won’t include syntactical details (such as the requirement to use angle brackets or properly nest tags) because these requirements are already part of the basic XML standard. Instead, the schema document will list the logical rules that pertain to your type of data.

An XSD, or schema, includes the following:

– Document vocabulary: This determines what element and attribute names are used in your XML documents.

– Document structure: This determines where tags can be placed and can include rules specifying that certain tags must be placed before, after, or inside others. You can also specify how many times an element can occur.

– Supported data types: This allows you to specify whether data is ordinary text or must be able to be interpreted as numeric data, date information, and so on.

– Allowed data ranges: This allows you to set constraints that restrict numbers to certain ranges, limit text to a certain length, force regular expression pattern matching, or allow only a small set of specified values.

Schema documents are written using an XML syntax with specific element names. All the XSD elements are placed in the http://www.w3.org/2001/XMLSchema namespace. Often, this namespace uses the prefix xsd: or xs:, as in the following example.

 

<?xml version=”1.0″?>
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”

targetNamespace=”urn:books”

xmlns:bks=”urn:books”>

 

<xsd:element name=”Books” type=”bks:BooksForm”/>

<xsd:complexType name=”BooksForm”>

<xsd:sequence>

<xsd:element name=”Book”

type=”bks:BookForm”

minOccurs=”0″

maxOccurs=”unbounded”/>

</xsd:sequence>

</xsd:complexType>

 

<xsd:complexType name=”BookForm”>

<xsd:sequence>

<xsd:element name=”ISBN-13″   type=”xsd:string”/>

<xsd:element name=”Title”    type=”xsd:string”/>

<xsd:element name=”Author”   type=”xsd:string”/>

<xsd:element name=”Price”    type=”xsd:decimal” />

<xsd:element name=”Available” type=”xsd:boolean” />

</xsd:sequence>

</xsd:complexType>

</xsd:schema>

 

Notes:

By examining the BooksList.xsd schema, you can learn the next important points:

– Schema documents use their own form of XML markup. In the example, you can see that all the elements are placed in the http://www.w3.org/2001/XMLSchema namespace using the xs namespace prefix.

– Every schema document starts with a root <schema> element.

– The schema document must specify the namespace of the documents it can validate. It specifies this detail with the targetNamespace attribute on the root <schema> element.

– The elements inside the <schema> element describe the structure of the target document. The <element> element represents an element, while the <attribute> element represents an attribute. To find out what the name of an element or attribute is, look at the name attribute. For example, you can tell quite easily that the first <element> has the name BooksList. This indicates that the first element in the validated document must be <BooksList>.

–  If an element can contain other elements or has attributes, it’s considered a complex type. Complex types are represented in a schema by the <complexType> element. The simplest complex type is a sequence, which is represented in a schema by the <sequence> element. It requires that elements are always in the same order—the order that’s set in the schema document.

– When defining elements, you can define the maximum number of times an element can appear (using the maxOccurs attribute) and the minimum number of times it must occur (using the minOccurs attribute). If you leave out these details, the default value of both is 1, which means that every element must appear exactly once in the target document. Use a maxOccurs value of unbounded if you want to allow an unlimited list. For example, this allows there to be an unlimited number of <Book> elements in the BooksList catalog. However, the <Price> element must occur exactly once in each <Book>.

– When defining an attribute, you can use the use attribute with a value of required to make that attribute mandatory.

– When defining elements and attributes, you can specify the data type using the type attribute. The XSD standard defines 44 data types that map closely to the basic data types in .NET, including the double, int, and string data types used in this example.