Creating your first XML schema is a learning process. A schema is a diagram of an XML document. It allows the designer to establish rules and validation criteria for elements. As with XML, the best way to write a schema is by developing well-formed sections. Schemas use basic XML syntax. If you can write an XML document, you can write a schema to accentuate it. Stepping through the process will help you learn how to write your first XML schema.
Start at the Beginning
A schema is an XML document. That means it follows the same basic rules as a standard XML page. XML code begins with a declaration statement.
<?xml version="1.0" ?>
All XML documents must have a root element. In a schema, the root element is static and identifies the page as a schema.
The root element begins with a namespace. A namespace is a container that qualifies the elements. Think of it a label that groups all elements together, a way of branding the parts of the group to point out that they are part of something larger. The namespace comes before the root element
In an XML schema, the root element is "schema" and the namespace is "xs". This makes the opening tag for a schema:
If a company develops a brand name, it has essentially established a namespace for a collection of products. That company might create a page that lists the products that fall under their namespace. In XML, this list is a Uniform Resource Identifier (URI). The URI is the web address that provides information on the namespace to keep it from being confused with namespaces developed for other purposes. This way a namespace titled "tools" and associated with "http://www.mytools.com" is different than the "tools" namespace for "http://www.histools.com".
When creating the namespace 'xs' for an XML schema, write the URI as an attribute for the first element in that namespace or in this case, the root element.
This bit of code establishes the namespace "xs," and lists the element name 'schema'. It groups all things "xs" with the URI identifier from WC3 (web address). The code xmlns states, "I am creating this namespace to be associated with this URI."
A good practice when developing XML is to write a closing tag at the same time as you write the opening. This way you never forget to close an element.
The starting point for this schema looks like this:
<?xml version="1.0" ?>
Understanding Some Important Terms
So far, stepping through the code has uncovered a number of critical terms. Let"s go through them one by one.
A schema is an XML document that serves as a blueprint to qualify and define a separate XML page.
The declaration statement introduces the document as XML. It tells the parser what to expect when it processes the information. It is always the first line of any XML page.
The root element is the beginning container of information in an XML document, the box that holds all the other elements for the page. Schemas have static root elements. This means all schemas start with the same root.
A namespace is a label that identifies an element as part of a group. When a parser sees a namespace, it assigns it to the group. The processor knows this namespace is associated with a group defined by a URI.
Uniform Resource Identifier (URI)
For the purposes of a schema, a URI is a web address assigned to define namespaces.
Right now, this schema is an empty container. In the next article, we will fill in the empty space. For more information on XML schemas, look at: What is an XML Schema?