XSD or XML Schema Definition is a schema definition document which will provide the definition of different type of objects and entities in XML language. XSD is very popular for data exchange especially in enterprise environments because of different advantages. In this tutorial, we will examine XSD, XSD syntax, XSD usage.
What Is XML?
Before explaining the XSD we should explain the XML language. XML the short form of the eXtensible Markup Language which is designed to extend different data types in a structured manner. XML is very popular because of its clarity and structured definition. Below we can find some XML statements.
<xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/>
What Is XML Schema?
XML Schema is the complete XML document that describes the XML document. XML schema contains the version, schema URI data types, etc. Below we can see a complete XML schema that simply defines the Person
entity.
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="surname" type="xs:string"/> <xs:element name="age" type="xs:string"/> <xs:element name="city" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
What Is XML Schema Definition?
Well, actually we have learned the XSD or XML Schema Definition because the XML Schema language also named XSD. A typical XSD contains the following elements and information about the XML.
Elements
that can appear in an XML document.Attributes
that can be used for elements in an XML document.Data Types
for defined elements and attributes.- Default and fixed values for elements and attributes.
XSD
version will set the XML version.
XSD Syntax
XSD has a very structured syntax. As a markup language like HTML, most of the elements are defined with start and end tags. XSD express the hierarchy between elements in order to create a child and parent relation. This hierarchical usage provides the ability to create complex data structures easily and relate them to each other.
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="surname" type="xs:string"/> <xs:element name="age" type="xs:string"/> <xs:element name="city" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
<xs:element name="Person">
will create a data type named `Person` and the details will be defined inside this tag.<xs:complexType
is used to create multiple different elements insidePerson
data type.<xs:sequence>
creates a sequence where we can put multiple elements.<xs:element name="name" type="xs:string"/>
is an element definesPerson
data type. It is named `name` and stored as a string.
Use XSD In XML Document
We can use the XSD or XML Definition in an XML document while exchanging or storing information. The following XML document uses the previously defined Person
data type. We will just put some data into this Person object which are name, surname, age, and city.
<?xml version="1.0"?> <!DOCTYPE note SYSTEM "https://www.w3schools.com/xml/note.dtd"> <Person> <name>İsmail</name> <surname>Baydan</surname> <age>36</age> <city>Ankara</city> </Person>