XML Free Tutorial

Web based School

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
<?xml version="1.0"?> <beginnersbook> <to>My Readers</to> <from>Chaitanya</from> <subject>A Message to my readers</subject> <message>Welcome to beginnersbook.com</message> </beginnersbook> XML Schema file: bb.xsd <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.beginnersbook.com" xmlns="https://www.beginnersbook.com" elementFormDefault="qualified"> <xs:element name="beginnersbook"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="subject" type="xs:string"/> <xs:element name="message" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Explanation:
<xs:element name=”beginnersbook”> defines that beginnersbook is the name of an element.
<xs:complexType> 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)
<xs:sequence> It defines that the complex type element “beginnersbook” is a sequence of elements
<xs:element name=”to” type=”xs:string”> It defines that the element “to” is of type string
<xs:element name=”from” type=”xs:string”> It defines that the element “from” is of type string
<xs:element name=”subject” type=”xs:string”> It defines that the element “subject” is of type string
<xs:element name=”message” type=”xs: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.

Previous