Home

XML Schemas

XML Schemas

Definition

We know how to create XML elements and how to add values to them. Some values appear as regular strings. Some appear as numbers, and so on. In reality, we did not define what type each value was.

In fact, we could have a child node whose value appeared as a string while the same child node in the element would appear as a number. An XML schema is a document that specifies the type of value that each element of the corresponding XML document uses. This means that an XML schema must provide at least two pieces of information: the XML document that the schema refers to and the list of elements defined by the schema.

Introduction to Creating an XML Schema

Like an XML file, an XML schema is a text-based document made of tags. You can start the document with a document type definition (DTD) that including encoding:

<?xml version="1.0" encoding="utf-8"?>

After that line, you will create the elements of the document. The first characteristic of an XML schema document is that it must specify a namespace. The namespace is defined in a document that specifies the types of values used in the document. As seen in our introduction to namespaces, you must decide about the prefix you will use in the various tags of the document. By convention, most people use either xs, which stands of XML Schema, or xsd, which stands for XML Schema Definition. Here is an example that uses xs:

<?xml version="1.0" encoding="utf-8"?>
<xs>

As mentioned in our review of namespaces, the prefix must be followed by a colon and a name for the tag. In a typical XML schema, the first tag is named schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema>

As mentioned already, you must specify the namespace you will use. As mentioned in our study of namespaces, you create an attribute what uses xmlns as its prefix followed by a column and the prefix previously defined:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs>

You must assign the URL of the namespace to that attribute.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="">

After creating the document, you should (must) save it and give it a name. An XML schema file has the extension .xsd.

Built-In Schemas

As mentioned already, the primary role of an XML schema is to define the types of values used in an XML document. There are various types of values that can be used in an XML document. Instead of defining your own types of values, many people and companies have already created XML schemas that you can simply use in your project. We refer to these as built-in XML schemas.

Among others, both the W3C consortium and Microsoft have already created XML schemas you can use. To see a list of built-in XML schemas, if you are working in Microsoft Visual Studio, first create or open an XML file. On the main menu, click XML -> Schemas... This would open the XML Schemas dialog box

Buil-In XML Schemas

Buil-In XML Schemas

Buil-In XML Schemas

To use any of these schemas, assign it to your xmlns:Prefix attribute. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

You can use one of the built-in schemas of the XML Schemas dialog box. You can also use more than namespace. To do this, create one schema tag and add each desired namespace. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
	   xmlns:xs="http://www.w3.org/XML/1998/namespace"
	   xmlns:xs="http://schemas.microsoft.com/xsd/catalog">

Notice that, in the XML Schemas dialog box, you can add a new one by clicking Add, locating, and then selecting the file.

Introduction to Creating a Tag in an XML Schema

Consider the following XML document:

<?xml version="1.0" encoding="utf-8"?>
<video />

As mentioned already, to create its corresponding XML schema, start a text-based document that contains a DTD and a tag that specifies the foundational namespace. Of course, this tag must be closed. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
</xs:schema>

Between the start and the end tags, you must specify the necessary tag. Since the schema is tied to the XML tag, it must include a tag for each of the XML elements. Each tag must start with the prefix, a column, and the keyword element. This would be done as follows:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element ... >
</xs:schema>

To indicate what element the tag is referring to, you use name="" and, in the quotes, insert the name of the element from the XML document. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="video">
</xs:schema>

As seen in our introductions to XML, you must close the tag and you have two options:

Characteristics of Tags of an XML Schema

Simple Types

In programming languages, a data type is a word or a group of words that indicates the amount of computer memory necessary to store a certain value or a certain kind of value. A value can be considered as a character, a group of characters (also called a string), a number, a date, etc. In XML, these are referred to as simple types. Consider the following XML document:

<?xml version="1.0" encoding="utf-8" ?>
<video>The Distinguished Gentleman</video>

Notice that it contains only one element, that element has a value, and it does not have any child node. One of the rules of simple types is that their correspond node cannot have a child element. In the same way, an element of simple type cannot have an attribute.

Instead of defining your own simple types, you can use a built-in schema that already has the definitions you will need and you can simply use its data types. To specify the data type used by an element, in the XML schema and in the start-tag of the element, create an attribute whose name is type. Assign a string that contains the name of the prefix, followed by a colon, and followed by the data type. This would be done as follows:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="video" type="xs:DataType">
  </xs:element>
</xs:schema>

As mentioned already, a data type is primarily represented by a name but it may also have some internal characteristics that express how the data type functions. A number is referred to as signed if it can be represented with a leading sign as + or -, which means the number can be positive (from 0 up) or negative (from 0 down). When no leading sign is used, the number is considered positive. A number is reffered to as unsigned if it is positive.

The most common data types used for values of XML elements are:

If none of the built-in types suits you, you can create your own.

Complex Types

Consider the following XML document:

<?xml version="1.0" encoding="utf-8" ?>
<video>
    <title>The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
</video>

Notice that the video element has two child elements named title and director. When an XML element has a child element, that parent is said to have a complex type. In your XML schema, to create a tag for a parent element, create a tag named complexType and close it. This would be done as follows:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="video">
    <xs:complexType></xs:complexType>
  </xs:element>
</xs:schema>

Between the start and end-tags of the complex type, you will create the tag(s) for the child element. But first, you must indicate that you are about to specify the order of the child elements. This is done by creating a tag named sequence. This can be done as follows:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="video">
    <xs:complexType>
      <xs:sequence>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Remember that a parent element can have a value. Here is an example:

<?xml version="1.0" encoding="utf-8" ?>
<video>The Distinguished Gentleman
    <director>Jonathan Lynn</director>
</video>

When create a schema for the element, add a name flag and assign the name of the element to it. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="video">
    <xs:complexType>
      <xs:sequence name="video">
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Between the start and the end tags of the sequence, create a simple type tag for each child element of the corresponding complex tag. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="video">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="title" type="xs:string" />
        <xs:element name="director" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

The sequence tag indicates in what order the tag should appear in the XML document.

Consider the following XML document:

<?xml version="1.0" encoding="utf-8" ?>
<videos>
    <video>
	<title>The Distinguished Gentleman</title>
	<director>Jonathan Lynn</director>
	<length>112</length>
	<rating>R</rating>
	<widescreen>true</widescreen>
    </video>
    <video>
	<title>Godzilla</title>
	<director>Roland Emmerich</director>
	<length>139</length>
	<rating>PG-13</rating>
    </video>
</videos>

From what we have learned so far (namespaces, prefixes, the schema keyword, the element keyword, the complexType keyword, and the sequence keyword), it schema document can be created as follows:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="videos">
    <xs:complexType>
      <xs:sequence>
	<xs:element name="video">
	  <xs:complexType>
	    <xs:sequence>
	      <xs:element name="title" type="xs:string" />
	      <xs:element name="director" type="xs:string" />
	      <xs:element name="length" type="xs:unsignedInt" />
	      <xs:element name="rating" type="xs:string" />
	      <xs:element name="widescreen" type="xs:boolean" />
	    </xs:sequence>
	  </xs:complexType>
	</xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Now consider the following XML document:

<?xml version="1.0" encoding="utf-8" ?>
<videos>
    <video>
        <title>The Distinguished Gentleman</title>
        <director>Jonathan Lynn</director>
        <length>112</length>
        <actors>
            <actor>Eddie Murphy</actor>
	    <actor>Lane Smith</actor>
            <actor>Sheryl Lee Ralph</actor>
    	</actors>
    	<rating>R</rating>
    	<widescreen>false</widescreen>
    </video>
    <video>
    	<title>Godzilla</title>
	<director>Roland Emmerich</director>
	<length>139</length>
	<actors>
	    <actor>Matthew Broderick</actor>
	    <actor>Jean Reno</actor>
	</actors>
    	<rating>PG-13</rating>
    	<widescreen>true</widescreen>
    </video>
</videos>

Notice that the actors element, which is nested in the video element, has children elements of its own. For any node that has at least one child node, create a complexType start-tag and close it. Inside this complex type, create a sequence start-tag and close it. Inside the new sequence tag, create an element tag for each child element and specify its data type. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="videos">
	<xs:complexType>
	    <xs:sequence>
		<xs:element name="video">
		    <xs:complexType>
			<xs:sequence>
			    <xs:element name="title" type="xs:string" />
			    <xs:element name="director" type="xs:string" />
			    <xs:element name="length" type="xs:unsignedByte" />
			    <xs:element name="actors">
			        <xs:complexType>
				    <xs:sequence>
					<xs:element name="actor" type="xs:string" />
				    </xs:sequence>
				</xs:complexType>
			    </xs:element>
			    <xs:element name="rating" type="xs:string" />
			    <xs:element name="widescreen" type="xs:boolean" />
			</xs:sequence>
		    </xs:complexType>
		</xs:element>
	    </xs:sequence>
	</xs:complexType>
    </xs:element>
</xs:schema>

XML Attributes and Schemas

As you are aware already, an XML element can have one or more attributes. Here is an example:

<?xml version="1.0" encoding="utf-8" ?>
<video title="The Distinguished Gentleman" />

As you should know already, once an element has an attribute, it is said to have a complex type. Therefore, before creating a schema tag for the attribute, you must first create a complex parent type. Then create a child tag. Instead of the element keyword, use attribute. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="video">
    <xs:complexType>
      <xs:attribute name="title" type="xs:string">
    </xs:complexType>
  </xs:element>
</xs:schema>

In the same way, if a child element has an attribute, first create a complexType start-tag for it and close it. Inside this complex type, create a sequence start-tag and close it. Inside the new sequence tag, create an attribute tag for each attribute and specify its data type.

Options on Tags of an XML Schema

The Default Value of an Element or Attribute

By default, when creating an element or an attribute, you can give it any value you want. In fact, you create an element on one section of the document and give it a value, then create the same element in another section of the document but not give it a value. Here is an example:

<?xml version="1.0" encoding="utf-8" ?>
<students>
    <student>
	<firstname>John</firstname>
	<lastname>Sandt</lastname>
	<gender>Male</gender>
    </student>
    <student>
	<firstname>Sherrie</firstname>
	<lastname>Springfield</lastname>
	<gender />
    </student>
</students>

If you have an element or an attribute that usually receive the same value, you can specify a default value for it. To do this, when creating the schema of the element or attribute, add a flag named default and assign the desired value to it. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="students">
	<xs:complexType>
	    <xs:sequence>
		<xs:element maxOccurs="unbounded" name="student">
		    <xs:complexType>
			<xs:sequence>
			    <xs:element name="firstname" type="xs:string" />
			    <xs:element name="lastname" type="xs:string" />
			 <xs:element name="gender" type="xs:string" default="Male" />
			</xs:sequence>
		    </xs:complexType>
		</xs:element>
	    </xs:sequence>
	</xs:complexType>
    </xs:element>
</xs:schema>

The Minimum Number of Occurrences of an Element

Consider the following XML document:

<?xml version="1.0" encoding="utf-8" ?>
<products>
    <product>
	<number>283974</number>
	<dateacquired>2011-05-12</dateacquired>
	<name>Shirt with Epaullette</name>
	<size>14</size>
	<unitprice>59.95</unitprice>
    </product>
    <product>
	<number>485117</number>
	<dateacquired>2011-05-12</dateacquired>
	<name>Striped Pants</name>
	<size>Medium</size>
	<unitprice>78.50</unitprice>
    </product>
</products>

Notice that the product element has two child nodes. When creating an XML document, you must want to require that the a certain element have at least a certain number of child elements. In that case, if the element has less than that number of child elements, the document would be in violation of the rule and therefore would be valid.

To specify that an element must have at least a certain number of child node, when creating its schema tag, add an attribute named minOccurs and assign the desired number to it. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="products">
	<xs:complexType>
	    <xs:sequence>
		<xs:element minOccurs="4" name="product">
		    <xs:complexType>
		        <xs:sequence>
			    <xs:element name="number" type="xs:unsignedInt" />
			    <xs:element name="dateacquired" type="xs:date" />
			    <xs:element name="name" type="xs:string" />
			    <xs:element name="size" type="xs:string" />
			    <xs:element name="unitprice" type="xs:decimal" />
			</xs:sequence>
		    </xs:complexType>
		</xs:element>
	    </xs:sequence>
	</xs:complexType>
    </xs:element>
</xs:schema>

This code indicates that the products element must have at least 4 child nodes. Since the products element in the above XML document has only two child nodes, the element violates the rule and would produce an error.

The Maximum Number of Occurrences of an Element

When creating an XML document, by default, you can create as many elements as you want and each element can have as many child nodes as necessary. If you want, you can limit the number of child nodes an element can have. To do this, when creating the schema tag of the element, add an attribute named maxOccurs and assign the desired value to it. The value can be either an insigned integer or unbounded. This can be done as follows:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="products">
	<xs:complexType>
	    <xs:sequence>
		<xs:element maxOccurs="4" name="product">
		    <xs:complexType>
		        <xs:sequence>
			    <xs:element name="number" type="xs:unsignedInt" />
			    <xs:element name="dateacquired" type="xs:date" />
			    <xs:element name="name" type="xs:string" />
			    <xs:element name="size" type="xs:string" />
			    <xs:element name="unitprice" type="xs:decimal" />
			</xs:sequence>
		    </xs:complexType>
		</xs:element>
	    </xs:sequence>
	</xs:complexType>
    </xs:element>
</xs:schema>

In this case, the products element must not have more than 4 child nodes. In the XML document, if you create more child nodes for the element than the number specified in maxOccurs, the document would be violating the riule and would be considered invalid.

Since the minOccurs and the maxOccurs flags are not mutually exclusive, you can use them both on the same element. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="products">
	<xs:complexType>
	    <xs:sequence>
		<xs:element minOccurs="1" maxOccurs="6" name="product">
		    <xs:complexType>
		        <xs:sequence>
			    <xs:element name="number" type="xs:unsignedInt" />
			    <xs:element name="dateacquired" type="xs:date" />
			    <xs:element name="name" type="xs:string" />
			    <xs:element name="size" type="xs:string" />
			    <xs:element name="unitprice" type="xs:decimal" />
			</xs:sequence>
		    </xs:complexType>
		</xs:element>
	    </xs:sequence>
	</xs:complexType>
    </xs:element>
</xs:schema>

Requiring an Attribute in an Element

Consider the following XML document:

<?xml version="1.0" encoding="utf-8" ?>
<products>
    <product number="283974">
	<dateacquired>2011-05-12</dateacquired>
	<name>Shirt with Epaullette</name>
	<unitprice>59.95</unitprice>
    </product>
    <product>
	<dateacquired>2011-05-12</dateacquired>
	<name>Striped Pants</name>
	<unitprice>78.50</unitprice>
    </product>
</products>

Notice that the first product element has an attribute named number while the second product element does not have that attribute. When creating the schema of an element, you can ask that a certain attribute be required. To take care of this, when creating the schema of the attribute, add an attribute named use and assign a value named required to it. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="products">
	<xs:complexType>
	    <xs:sequence>
		<xs:element maxOccurs="unbounded" name="product">
		    <xs:complexType>
			<xs:sequence>
			    <xs:element name="dateacquired" type="xs:date" />
			    <xs:element name="name" type="xs:string" />
			    <xs:element minOccurs="0" name="size" type="xs:string" />
			    <xs:element name="unitprice" type="xs:decimal" />
			</xs:sequence>
		  <xs:attribute name="number" type="xs:unsignedInt" use="required" />
		    </xs:complexType>
		</xs:element>
	    </xs:sequence>
	</xs:complexType>
    </xs:element>
</xs:schema>

Once this flag is set, whenever the element of that attribute is created, that attribute must be added. Otherwise, the element would violate the rule.

Setting an Attribute as Optional

Once again, consider the above XML document. Notice that the first product element has an attribute named number but the second product element does not have that attribute. When creating the schema of an element, you can add a certain attribute to a certain element but omit that attribute for the same element in another section of the document. In this cas, the attribute is said to be optional. To indicate this, when creating the schema of the attribute, add the use attribute and assign a value named optional to it. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="products">
	<xs:complexType>
	    <xs:sequence>
		<xs:element maxOccurs="unbounded" name="product">
		    <xs:complexType>
			<xs:sequence>
			    <xs:element name="dateacquired" type="xs:date" />
			    <xs:element name="name" type="xs:string" />
			    <xs:element name="unitprice" type="xs:decimal" />
			</xs:sequence>
		  <xs:attribute name="number" type="xs:unsignedInt" use="optional" />
		    </xs:complexType>
	        </xs:element>
	    </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

The optional value is the default value of the use flag.

Preventing the Use of an Element or Attribute

If you want to prevent the use of an element from a certain element in your XML document, create its tag in the XML schema document. Add both the minOccurs and the maxOccurs attributes and set the value of each to 0. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="products">
	<xs:complexType>
	    <xs:sequence>
		<xs:element maxOccurs="unbounded" name="product">
		    <xs:complexType>
			<xs:sequence>
			    <xs:element name="dateacquired" type="xs:date" />
				<xs:element name="name" type="xs:string" />
				<xs:element name="unitprice" type="xs:decimal" />
			    </xs:sequence>
		<xs:attribute name="number" type="xs:unsignedInt" use="optional" />
			</xs:complexType>
		    </xs:element>
		<xs:element name="closedcaption" minOccurs="0" maxOccurs="0" />
	    </xs:sequence>
	</xs:complexType>
    </xs:element>
</xs:schema>

To prevent the use of a certain attribute, in the schema document, create its tag and add the use attribute. Assign a value as prohibited to itnamed onsider the above

 Setting a Constant Value

Consider the following document:

<?xml version="1.0" encoding="utf-8" ?>
<students>
    <student>
	<firstname>John</firstname>
	<lastname>Sandt</lastname>
	<gender>Male</gender>
    </student>
    <student>
	<firstname>Sherrie</firstname>
	<lastname>Springfield</lastname>
	<gender>Female</gender>
    </student>
</students>

Notice that the first gender element has a value of Male and the second has a value of Female. We could have created one more gender element and give it any value. In a typical XML document, especially in the absence of a schema, you can give any value to an element. In some cases, you can specify a constant value that must always be used for an element. To do this, when creating the schema tag of the element, add an attribute named fixed and assign the desired value to it. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="students">
	<xs:complexType>
	    <xs:sequence>
		<xs:element maxOccurs="unbounded" name="student">
		    <xs:complexType>
			<xs:sequence>
			    <xs:element name="firstname" type="xs:string" />
			    <xs:element name="lastname" type="xs:string" />
			 <xs:element name="gender" type="xs:string" fixed="Female" />
			</xs:sequence>
		    </xs:complexType>
		</xs:element>
	    </xs:sequence>
	</xs:complexType>
    </xs:element>
</xs:schema>

Once this is done, if a new element is created and it uses a value other than the one specified in the fixed flag, the document would be violating the rule.

The fixed flag can also be applied to an attribute, in which case you would require that an attribute always carry a certain value. In fact, you can add the use flag and specify whether the attribute must be required or it would be optional.


Home Copyright © 2014-2020, FunctionX