XML Schema


 

XML Schema

An XML Schema defines how to structure an XML document and it can be used in place of DTD. XML Schema is based on XML.

An XML Schema defines how to structure an XML document and it can be used in place of DTD. XML Schema is based on XML.

XML Schema

An XML Schema defines how to structure an XML document and it can be used in place of DTD. XML Schema is based on XML. XML Schema language is known as XML Schema Definition (XSD).

The Purpose of XML Schema

Some of the main purposes of XML Schema are to define:

1. Elements which can occur in the xml document.
2. The child elements for an elements
3. The order of child elements
4. The number of child elements
5. Whether the element is empty or it can have some text
6. The type of data elements or attributes can have
7. Attributes which can be used for the elements used in a document
8. Default and fixed values for elements and attributes

Power of XML Schema

XML Schemas are richer and more powerful than DTDs . Support for data types in XML Schemas is one of its greatest strength.

XML Schema has many advantages:

1. Supports data types
2. Supports namespaces
3. Provides built-in primitive data types which includes byte, string, and integer, floating point numbers, country codes and language codes etc.
4. Provides the ability to define custom data types
5. Easy to describe allowable content in the document
6. Easy to validate the correctness of data
7. Object Oriented approach like inheritance and encapsulation can be used in creating the document
8. Easier to convert data between different data types
9. Easy to define data formats
10. Easy to define restrictions on data
11. It is written in xml so any xml editor can be used to edit xml schema
12 Xml Parser can be used to parse the schema file
13. They are extensible as they are written in XML

Simple xml document, creating dtd and schema

book.xml

<?xml version="1.0"?>
<book>
<book-name>My XML Book</book-name>
<book-author>RoseIndia</book-author>
<short-desc>This book is good for beginners in XML. This book helps beginners to learn xml easily and use XML concepts in their real development.</short-desc>
</book>

Following is the DTD file for the above xml file:

book-dtd.dtd

<!ELEMENT book (book-name, book-author, short-desc)>
<!ELEMENT book-name (#PCDATA)>
<!ELEMENT book-author (#PCDATA)>
<!ELEMENT short-desc (#PCDATA)>

Equivalent schema file for the above xml file is as below:

book-schema.xsd

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

<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="book-name" type="xs:string"/>
<xs:element name="book-author" type="xs:string"/>
<xs:element name="short-desc" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

Referencing dtd and schema in an xml file

A DTD can be referenced in an xml file as below:

<?xml version="1.0"?>
<!DOCTYPE book SYSTEM "book-dtd.dtd">

<book>
<book-name>My XML Book</book-name>
<book-author>RoseIndia</book-author>
<short-desc>This book is good for beginners in XML. This book helps beginners to learn xml easily and use XML concepts in their real development.</short-desc>
</book>

A schema file can be referenced in an xml file as below:

<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="book-schema.xsd">
<book-name>My XML Book</book-name>
<book-author>RoseIndia</book-author>
<short-desc>This book is good for beginners in XML. This book helps beginners to learn xml easily and use XML concepts in their real development.</short-desc>
</book>

Validating an XML Instance Document

Various xml validation tools are available which can be used to validate xml document. As a programmer point of view various xml validation APIs are available to check validation.

Ads