XML - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

XML Schema

XML Schema

shape Description

In the previous chapter, SPLessons covered the concept of DTD which allows to put constraints on XML file. The present chapter discusses about the alternative way of enforcing rules and constraints on the XML data and it is called as XML Schema. XML Schema was introduced by W3C which is based on XSD (XML Schema Definition Language) XML Schema can be defined as a separate document that describes the structure of the XML document. XML Schema can be said as an alternative to using DTD's. Syntax: [xml] <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> [/xml]

DTD or XSD?

  • XML Schema allow to constrain the XML documents content just like DTD's do. But, Schema is much more powerful and sophisticated than DTD.
  • Schema's allow much finer level of control than DTD's provide with. Schema and XML files are always stored separately.
  • Schema files are written using the same XML syntax. Whereas a DTD uses its own custom syntax. Schema is little harder and more verbose to write than DTD.

shape Example

In the below example, note is the root element and all the elements like to, from, heading and body are set to String type and also the document followed a sequence of placing the elements in a correct order which has entire control over the file. [xml] <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> [/xml]

XML Schema Datatypes

shape Description

Datatypes are one of the big addition to XML Schema language over the DTD. Some of them are:
  • A document can be validated by deciding which particular type of data can be used in a particular marked text.
  • They defines the data restrictions.
  • Datatypes will help the data to convert between different data types.
XSD language separates the datatypes into two types. They are:

Simple DataType

Simple datatype element is used only in the text context. Numbers, Strings, Dates, etc are few pre-defined simple datatypes. For Example, [xml] <xs:element name="phone_number" type="xs:int" /> [/xml]

Complex DataType

Complex Data is a mixed element, sequences element that have to be in a particular sequence for the particular number of things happening. Complex datatypes are built upon simple datatypes. In the below example, Address is the root element and all the elements like name, company and phone are set to String type and also the file did followed a sequence which is nothing but a schema of complex datatype. [xml] <xs:element name="Address"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="company" type="xs:string" /> <xs:element name="phone" type="xs:int" /> </xs:sequence> </xs:complexType> </xs:element> [/xml]

Summary

shape Key Points

  • XML Schema describes the structure of the XML document.
  • Schema files are written using the same XML syntax.
  • Datatypes will help the data to convert between different data types.