XML Overview

  An XML Schema, also known as an XML Schema Definition (XSD), is a way to define the structure, elements, and constraints of an XML document. It is used to specify the structure of an XML document so that the parser can understand the structure of the document and validate it against a set of rules.


XML Schemas are themselves XML documents and are used to define the elements, attributes, and data types used in XML documents. A schema defines the structure of an XML document in terms of elements, attributes, and data types. Each element in the schema corresponds to an element in the XML document and each attribute corresponds to an attribute in the XML document.

An XML Schema defines the structure of an XML document using a set of rules specified using a combination of elements and attributes. 

The main elements used in XML Schema are:

<element>: This element defines an element in the XML document. It has a name attribute that specifies the name of the element and a type attribute that specifies the data type of the element.

<attribute>: This element defines an attribute of an element in the XML document. There is a name attribute that specifies the name of the attribute and a type attribute that specifies the data type of the attribute.

<complexType>: This element is used to define a complex data type composed of other data types.

<simpleType>: This element is used to define simple data types, which are data types that are not composed of other data types.

<restriction>: This element is used to define restrictions for elements and attributes. For example, it can be used to specify that an element can only contain certain values, or that an attribute can only contain certain data types.

XML Schema also includes many built-in data types such as strings, integers, dates, and times.
You can also define your own custom data types by specifying combinations of constraints or simple types.

XML Schema also includes a number of built-in constraints such as maxLength, minLength, and pattern that can be used to validate element and attribute values.

Finally, an XML Schema can be used to validate an XML document against it. This process, called validation, ensures that the XML document conforms to the structure and constraints defined by the schema. This can be done using an XML parser or validator.

Overall, XML Schema provides a way to define the structure, elements, and constraints of XML documents, facilitating validation and processing of XML documents in a consistent and predictable manner.



The following example of an XML schema, defines the structure of an XML document for a library catalog:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="catalog">

    <xs:complexType>

      <xs:sequence>

        <xs:element name="book" maxOccurs="unbounded">

          <xs:complexType>

            <xs:sequence>

              <xs:element name="title" type="xs:string"/>

              <xs:element name="author" type="xs:string"/>

              <xs:element name="publisher" type="xs:string"/>

              <xs:element name="year" type="xs:integer"/>

            </xs:sequence>

            <xs:attribute name="id" type="xs:string" use="required"/>

          </xs:complexType>

        </xs:element>

      </xs:sequence>

    </xs:complexType>

  </xs:element>

</xs:schema>


This schema defines the structure of catalog items, including book items. 
The book element has several child elements: title, author, publisher, year, and attribute id. The Title, Author, Publisher, and Year elements are of type String and the Year element is of type Integer. 
The schema indicates that the id attribute is required and that the book element may appear multiple times.

Here is an example of an XML document that conforms to this schema.

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <book id="bk101">
    <title>XML for Dummies</title>
    <author>John Doe</author>
    <publisher>Wiley</publisher>
    <year>2021</year>
  </book>
  <book id="bk102">
    <title>Learning XML</title>
    <author>Jane Smith</author>
    <publisher>O'Reilly</publisher>
    <year>2022</year>
  </book>
</catalog>

This XML document contains two book elements, each with its own set of child elements and attributes that conform to the structure defined by the schema.

Note that this is a simple example. XML Schemas can be more complex and specific, depending on the needs of your application.

Also, although this example uses the XML Schema language, there are other languages ​​that define XML Schema, such as Relax NG and Schematron.
Previous Post Next Post