XML Schema Definition
In the previous tutorial, we learned about XML DTD which is used for XML validation. Similar to DTD, XML Schema is also used to check whether the given XML document is “well formed” and “valid”. In this guide, we will learn what is an XML Schema, how it is better than DTD and Schema examples.
XML XSD (XML Schema Definition)
XML schema is an alternative to DTD. An XML document is considered “well formed” and “valid” if it is successfully validated against XML Schema. The extension of Schema file is .xsd.
An example of XML Schema
A Message to my readers
Welcome to beginnersbook.com
XML Schema file: bb.xsd
defines that beginnersbook is the name of an element.
This is the next line after the element “beginnersbook”. It defines the type of element “beginnersbook”, it says that the type of this element is “complexType” (we will discuss this type later in this same tutorial)
It defines that the complex type element “beginnersbook” is a sequence of elements
It defines that the element “to” is of type string
It defines that the element “from” is of type string
It defines that the element “subject” is of type string
It defines that the element “message” is of type string
XML Schema Data types
In the above example, we have seen that the root element “beginnersbook” is complexType. In XML schema an element belongs to either of the following two types.
1. simpleType – A singleType element can contain text, they do not contain other elements. In the above example, the elements to, from, subject and message are simpleType element.
2. complexType – A complexType element can contain attributes, other elements, and text. In the above example, the element beginnersbook is of type complexType because it contains other elements.
Advantages of using XML Schema over DTD
1. Schema uses XML as language so you don’t have to learn new syntax.
2. XML schema supports data types and namespaces.
3. You can use XML parser to parse the XML schema as well.
4. Just like XML, the XML schema is extensible which means you can reuse the schema in other schema, as well as you can reference more than one schemas in a single XML document.