XML Schema
XML Schema
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.
<?xml version="1.0"?>
<beginnersbook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="bb.xsd">
<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"
xmlns="https://www.beginnersbook.com">
<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:
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.
minOccurs can be assigned any non-negative integer value (e.g. 0, 1, 2, 3... etc.), and
maxOccurs can be assigned any non-negative integer value or the special string constant
"unbounded" meaning there is no maximum so the element can occur an unlimited number of
times.
Sample XSD Description
maxOccurs="unbounded" />