XML Schema - Defining Tags of XML Documents

Schema is a W3C Recommendation, and can be implemented without fear of it changing.

The goal of XML schema

Schemas provide the sets of rules that define structure, content and semantic of XML documents.
This is a replacement, more complete of doctype.

What is XML schema made of?

A schema is an XML document that define rules of other XML documents. It is a list of rules and a rule defines:
- sub-elements,
- type of data.
An XML tag is defined by an "ElementType" structure, that lists embedded sub-elements.
The header of the schema must specify a name space.

How to use XML schema?

Example of XML document and its schema. You need for a validating program to parse the XML document with the schema.

Example of XML document:
<cars xmlns="x-schema:schema.xml">
    <car> 
        <power>2500</power>
    <car> 
</cars> 
The corresponding schema:
<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">                  
    <ElementType name='cars' content='eltOnly'>
        <element type='car' />
    </ElementType>
    <ElementType name='car' content='eltOnly'>
        <element type='name' dt:type='string' />
        <element type='power' dt:type='decimal' />                    
    </ElementType> 
</Schema> 

Explanations:

<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">

Declares the name space and the datatypes.

<ElementType name='cars' content='eltOnly'>

Declares types, conditions and child elements of the element "cars".

 <element type='car' />

This is equivalent to <element type='car'> </element>, and this defines which elements are childs of the element "cars".
We can add other childs as:
<element type='van' />
<element type='truck' />
and so ones...

 <ElementType name='car' content='eltOnly'>

Declares a sub-element and its child elements: name, power....

<element type='name' dt:type='string' />
<element type='power' dt:type='decimal' /> 

Declares two elements and the type of the content, that are either text or number.

XML Schema and DTD

DTD has the same role as XML Schema, but with drawbacks:

Links

© 2006-2021 Xul.fr