Updated April 13, 2023
Introduction to XML Schema
XML Schema is defined as language for specifying structure or constraint of XML data & documents like a database schema used for describing the data of database. It is also like DTD but provides more control on XML structure.The most popular schema language is XML Schema Definition (XSD), used to support namespaces & define the data types, attributes and elements.
Syntax:
It uses XML syntax which mean you don’t need to learn another language if you have knowledge of XML. Because of this we can use XML editor to edit schema files & parser to parse files. Using the XML DOM we can manipulate Schema & using XSLT we can transform our schema. As it is written in XML, so it can be easily & quickly extend for further usage. Basically it uses a legitimate format that an XML document can take.
<?xml version ="1.0" encoding ="UTF-8"?>
<xs:schemaxmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:elementname="employee">
<xs:complexType>
<xs:sequence>
<xs:elementname="empname"type="xs:string"/>
<xs:elementname="company"type="xs:string"/>
<xs:elementname="contactno"type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
How does XML Schema works?
XML Schema works in the following manner:
- XML document data is concluding a new XML Schema, if there is no schema or DTD i.e. Document Type Definition within XML document.
- XML document has a DTD associated then the related XML Schema can be created by the conversion of external DTD as well as internal subset.
- XML document has a XDR i.e. XML Data Reduced schema which is inline then the related, can be created by the conversion of the XDR schema.
XML Schema Data Types
Given below are the 2 types of data types:
- Simple Types
- Complex Types
1. Simple Types
It allows us to have the text based values which may not only shown in an element / attribute but also restrict us from keeping it empty. In this way it appears different from DTDs. Eg: numbers are restricted within a specific attribute. Under XSD, we have totalNineteen(19) primitive datatypes that is anyURI, base64Binary, boolean, date, dateTime, decimal, double, duration, float, hexBinary, gDay, gMonth, gMonthDay, gYear, gYearMonth, NOTATION, QName, string, and time.
Using the above mentioned primitives new data types can be built by following three ways:
- Set of permitted values will be reduced i.e. Restriction.
- Sequence of values will be allowed i.e. List.
- Choice of values from different types will be allowed i.e. Union.
Within the specification, total 25 derived types are mentioned, and remaining derived types can be mentioned in schemas created by users. The ways for data type’s constraint include the min & max value specification, regular expressions, string length constraints, and the decimal digit constraint. Definition 1.1 adds the arbitrary constraint specification which is also known as XSD Assertion.
2. Complex Types
It allows us to have an element’s the permitted content, which means it holds multiple element, attributes & text children. Also it contains additional subtypes & allows us to leave empty. Along with it, complex type definition contains uses of attribute set and a model of content.
Given below are types of content model:
- Content model with no text is called Element-only content.
- Content model with text & no child elements is called Simple content.
- Content model with no text & no child element is called Empty content.
- Content model with both text & child element is called Mixed content.
Example of XML Schema
Given below is the example mentioned:
To begin with, let’s create a schema file. We will save the file as student.xsd.
Code:
<?xml version ="1.0" encoding ="UTF-8"?>
<xs:schemaxmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.educba.com"xmlns="http://www.educba.com"elementFormDefault="qualified">
<xs:elementname="student">
<xs:complexType>
<xs:sequence>
<xs:elementname="stdname"type="xs:string"/>
<xs:elementname="school"type="xs:string"/>
<xs:elementname="phone"type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Later we will try to see the xml file using XML schema or XSD file, we will save it as student.xml.
Code:
<?xml version ="1.0" encoding ="UTF-8"?>
<studentxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.educba.com"xsi:schemaLocation="http://www.educba.com student.xsd">
<stdname>Abhi</stdname>
<school>Holycross</school>
<phone>1234567890</phone>
</student>
Finally we use the following java based validator to validate student.XML file against student.XSD.
- Use any text editor like Notepad++ to create a java based validator, save it as java.
- Import all the necessary namespaces including xml.validation.schema.
- Then create a class ValidateStudentSchema.
- Next step is to create a boolean method called checkXMLSchema.
- Pass 2 string parameters to checkXMLSchema method i.e. xsdPath&xmlPath.
- Inside the method create an object of SchemaFactory class, which is a schema compiler.
- This schema compiler will be used to read the external representation on schema & based on that we will validate xml file against xsd.
- Don’t forget to use try catch block to catch an exception if something goes wrong.
- Before closing the method return true.
- Finally call the method checkXMLSchema in the main function of ValidateStudentSchema class.
Once you are done with above code the next step is to compile the schema file in console.
Open command prompt & access the path in which the ValidateStudentSchema.java file is stored.
d:\EduCBA>javacValidateStudentSchema.java
Then write ValidateStudentSchema by passing two parameters i.e. students.xsd and students.xml
d:\EduCBA>java ValidateStudentSchema students.xsd students.xml
Output:
students.xsd is suitable for student.xml
Conclusion
From the above details we can conclude that XML Schema specifies the XML documents format & content. However, it is mainly used as a descriptions type language which is platform-independent. Also in this schemas, to exchange data in with respect type descriptions we use the XML documents. So to build your own XML Schema based systems we should consider the 2 rules first to describe type systems is the primary objective later on we can consider it for purposes such as document layout and second here in the application development, we should write schemas for documents. In other words there will be no use of document which has no schema attached to it.
Recommended Articles
This is a guide to XML Schema. Here we discuss the introduction to XML Schema, how does it works, data types and example respectively. You may also have a look at the following articles to learn more –