In this tutorial, the basic concept of XML Schema is described. XML schemas are themselves XML documents. They are XML-based alternatives to DTDs. They also provide a much more powerful means to define the XML document structure. XML Schema documents usually have the filename extension “.xsd“. The following example is an XML Schema document called “book.xsd“:
<xs:element name="book"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string"/> <xs:element name="info" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
The schema above is interpreted like this:
- defines the element called “book“
- the “book” element is a complex type
- the complex type is a sequence of elements
- the element “title” is of type string
- the element “author” is of type string
- the element “info” is of type string
- the element “body” is of type string
XML DTD vs. XML Schema
Both Document Type Definitions (DTD’s) and XML Schemas (XSD’s, also known as WXS) are industry-standard ways to define XML-based data models. There are many technical benefits of migrating older DTDs to XML Schema, including:-
- XML Schemas use XML syntax; so we don’t have to learn a new language, we can use our XML editor to edit our Schema files, we can use our XML parser to parse our Schema files, we can manipulate our Schema with the XML DOM, and we can also transform our Schema with XSLT.
- Support for primitive (built-in) data types (e.g. xs:integer, xs:string, xs:date, and so on), which facilitates using XML in conjunction with other typed-data, including relational data.
- The ability to define custom data types, using object-oriented data modeling principles: encapsulation, inheritance, and substitution.
- XML Schemas are extensible; so we can reuse our Schema in other Schemas, we can create our own data types derived from the standard types and we can also refer multiple schemas in the same document.