Home >>XML Tutorial >XML Schema
XML schema is a language used to describe XML documents constraints. There are too many scheme languages that are used for example Relax- NG and XSD (XML schema definition) for a few days now.
The structure of an XML document is specified with an XML schema. It is like DTD but offers more XML structure control.
If an XML document includes the correct syntax, it is considered "well-formed." A well-formed, valid XML document is one validated against schema.
Visit http://www.xmlvalidation.com to validate the XML file against schema or DTD.
Let's create a schema file.
employee.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.phptpoint.com"
xmlns="http://www.phptpoint.com"
elementFormDefault="qualified">
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Let's see the xml file using XML schema or XSD file.
employee.xml
<?xml version="1.0"?>
<employee
xmlns="http://www.phptpoint.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.phptpoint.com employee.xsd">
<firstname>phptpoint</firstname>
<lastname>noida</lastname>
<email>phptpoint@phptpoint.com</email>
</employee>
<xs:element name="employee"> :It determines employee name of element.
<xs:complexType> : This determines that the 'employee' aspect is type complex.
<xs:sequence> :This determines that the form of complex is a sequence of elements.
<xs:element name="firstname" type="xs:string"/> :This determines the string / text type is the element 'firstname.'
<xs:element name="lastname" type="xs:string"/> :This determines the string / text sort is the element 'lastname.'
<xs:element name="email" type="xs:string"/> :This determines the string / text sort is the element 'email.'
There are two types of data types in XML schema.
The simpleType lets you have elements based on text. It has less features, less kid components and can not be left empty.
The complexType lets you keep multiple elements and attributes. This may contain more sub-elements and may be left empty.