xml
  1. xml-schema

XML Schema

  • Schema is a tool used for validating XML documents against a defined structure.
  • It is an essential tool for ensuring the compliance of XML documents to a specific set of rules.
  • This tutorial explains the syntax, example, output, explanation, use, important points, and summary of Schema.

Syntax

The syntax for Schema is as follows:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="elementName">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="subElementName" type="subElementType" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

The syntax consists of several parts, including the schema, element, and complexType.

  • Schema: The xs:schema tag defines the schema for the XML document and the xmlns:xs attribute specifies the XML schema namespace.

  • Element: The xs:element tag is used to define an XML element and acts as a container for other tags within the schema.

  • ComplexType: The xs:complexType tag is used to define a complex type element that contains other elements.

  • Sequence: The xs:sequence tag is used to specify that the elements appear in a specific order.

  • Element Name: The name attribute specifies the name of the element being defined.

  • Sub-Element Name: The name attribute specifies the name of the sub-element being defined.

  • Sub-Element Type: The type attribute specifies the data type of the sub-element.

  • MinOccurs: The minOccurs attribute specifies the minimum number of times the sub-element can occur.

  • MaxOccurs: The maxOccurs attribute specifies the maximum number of times the sub-element can appear.

Example

Here is a basic example of Schema that validates an XML document containing a list of books:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="books">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" type="bookType" maxOccurs="unbounded" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="bookType">
        <xs:sequence>
            <xs:element name="title" type="xs:string" />
            <xs:element name="author" type="xs:string" />
            <xs:element name="price" type="xs:float" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

This example defines a structure for an XML document that contains a list of books. Each book element contains a title, author, and price element.

Output

The output of Schema validation is a binary result - either validation is successful or fails. If the XML document conforms to the schema, then validation succeeds. Otherwise, it fails and produces an error message highlighting the issue with the document's structure.

Explanation

Schema validation checks whether an XML document is in compliance with a pre-defined structure. In essence, it ensures that the document follows the guidelines defined in the schema. The schema defines the mandatory and optional fields, the data types that should be used, and the order in which the elements should appear. By validating an XML document against a schema, we are checking that it meets the requirements of the schema.

Use

Schema validation is used in various scenarios, including:

  • Ensuring compliance with a specific data format or structure that must be followed.
  • Validating the structure and integrity of an XML document to ensure it doesn't have errors.
  • Ensuring consistent data format across multiple data sources to facilitate integration.

Important Points

Here are some important points to keep in mind when working with Schema:

  • The schema document should be well-formed, meaning it contains no XML syntax errors.
  • Schemas are used to validate XML documents, not modify them.
  • The schema validation process requires both the schema document and a target XML document to validate.

Summary

Schema is an important tool for validating XML documents against a defined structure. It is used to ensure compliance with a specific data format or structure requirements, validate the structure and integrity of an XML document, and facilitate integration by ensuring data consistency. The Syntax for Schema consists of several parts, including Schema, Element, ComplexType, Sequence, Element Name, Sub-Element Name, Sub-Element Type, MinOccurs, and MaxOccurs. Schema validation produces a binary result- either validation succeeds or fails. If the validation fails, it produces an error message highlighting the issue with the document's structure.

Published on: