Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

XML Schema


What is XML schema

XML schema is a language which is used for expressing constraint about XML documents. There are so many schema languages which are used now a days for example Relax- NG and XSD (XML schema definition).

An XML schema is used to define the structure of an XML document. It is like DTD but provides more control on XML structure.


Checking Validation

An XML document is called “well-formed” if it contains the correct syntax. A well-formed and valid XML document is one which have been validated against Schema.

Visit http://www.xmlvalidation.com to validate the XML file against schema or DTD.


XML Schema Example

Let’s create a schema file.

employee.xsd

  1. <?xml version=“1.0”?>
  2. <xs:schema xmlns:xs=“http://www.w3.org/2001/XMLSchema”
  3. targetNamespace=“http://www.Kreationnext.com”
  4. xmlns=“http://www.Kreationnext.com”
  5. elementFormDefault=“qualified”>
  6. <xs:element name=“employee”>
  7.   <xs:complexType>
  8.     <xs:sequence>
  9.       <xs:element name=“firstname” type=“xs:string”/>
  10.       <xs:element name=“lastname” type=“xs:string”/>
  11.       <xs:element name=“email” type=“xs:string”/>
  12.     </xs:sequence>
  13.   </xs:complexType>
  14. </xs:element>
  15. </xs:schema>

Let’s see the xml file using XML schema or XSD file.

employee.xml

  1. <?xml version=“1.0”?>
  2. <employee
  3. xmlns=“http://www.Kreationnext.com”
  4. xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  5. xsi:schemaLocation=“http://www.Kreationnext.com employee.xsd”>
  6.   <firstname>vimal</firstname>
  7.   <lastname>jaiswal</lastname>
  8.   <email>vimal@Kreationnext.com</email>
  9. </employee>

Test it Now


Description of XML Schema

<xs:element name=”employee”> : It defines the element name employee.

<xs:complexType> : It defines that the element ’employee’ is complex type.

<xs:sequence> : It defines that the complex type is a sequence of elements.

<xs:element name=”firstname” type=”xs:string”/> : It defines that the element ‘firstname’ is of string/text type.

<xs:element name=”lastname” type=”xs:string”/> : It defines that the element ‘lastname’ is of string/text type.

<xs:element name=”email” type=”xs:string”/> : It defines that the element ’email’ is of string/text type.


XML Schema Data types

There are two types of data types in XML schema.

  1. simpleType
  2. complexType

simpleType

The simpleType allows you to have text-based elements. It contains less attributes, child elements, and cannot be left empty.

complexType

The complexType allows you to hold multiple attributes and elements. It can contain additional sub elements and can be left empty.

Comments

comments