Defining XML Schemas - XML

XML schemas are used to define the structure of an XML document. In this context, schemas are similar to DTDs that we discussed earlier. Because XML documents are platform independent, they are used extensively to transfer data over a network. This implies that an XML document should adhere to a standard structure. This standard structure is referred to as the XML schema.

When an XML document is transferred over a network, the receiving application might need to process the data to produce a result. However, first the application needs to validate the data in the XML document. Validating the data ensures that no errors are generated while processing the data. In addition, validating the data ensures that the application does not produce erroneous results after processing the data. To validate the data, the receiving application verifies that the data adheres to the XML schema. If the structure of the XML document is not validated, an error is produced.

Defining XML Schemas

An XML schema is a document written in XML syntax that defines the structure of other XML documents. An XML schema consists of elements, attributes, and data types that are allowed to be included in an XML document. In addition, XML schemas define the rules that you need to follow while designing the structure of an XML document. An XML document that adheres to an XML schema is called the document instance of the schema and is an example of a valid document.

As discussed earlier, XML schemas provide a standard against which an XML document is validated. The following section discusses how to validate XML documents in detail.

Validating XML Documents

When an XML document is created and needs to be transferred over a network, both the sending and receiving applications need to mutually agree on a set of elements, attributes, data types, and the structure of the XML document. All this information is included in the XML schema on which the XML document is based.

However, doing this limits the user to use only the components that are included in the XML schema. Therefore, the XML schema constrains the user, as described in the following list:

  • Data type constraint. A data type constraint defines the permissible data types that you can include in an XML document.
  • Content type constraint. As discussed, an XML schema defines the structure of XML data. Therefore, the content type constraint defines the structure and sequence of the data in an XML document.

When an XML document is transferred, the parsers of the receiving application validate the data based on the data type and content type constraints. The parsers then verify the validity of the document.

An example of an XML schema is a DTD. A DTD also defines the structure of a document; therefore, a DTD is used to validate an XML document. However, DTDs have some limitations because of which W3C had to look for an alternative in the form of XML schemas.

The following section compares traditional DTDs with XML schemas.

Comparing DTDs with XML Schemas

Before comparing XML schemas with DTDs, we will list the problems that users face while working with DTDs. This will help you analyze the advantages of XML schemas over DTDs.

Limitations of Using DTDs

Following are some of the limitations of using DTD:

  • DTDs are not written using XML syntax. However, to write a DTD, you use another syntax called Extended Backus Naur Form (EBNF). The EBNF syntax is different from XML and might be difficult for some users to write or understand.
  • DTDs define limited data types that you can use in an XML document. As discussed, DTDs define only the structure, elements, and attributes that you can use in an XML document and support only a few data types—primarily text. Therefore, all non-textual data needs to be transferred to a data type that is defined in a DTD so that a user can work with non-textual data.
  • DTDs cannot be inherited from an existing DTD. This implies that you need to create DTDs again and cannot reuse an existing DTD.
  • DTDs do not support namespaces. Therefore, to validate an XML document that is based on a namespace, you first need to explicitly declare all the elements and attributes of the namespace in a DTD.

The following section discusses how schema can be a solution to the preceding problems.

XML Schema as a Solution to the Limitations of DTDs

The following list describes the advantages of XML schemas over DTDs:

  • XML schemas are written using XML syntax, which makes it easier for users to create and understand an XML schema. In addition, they are extensible because you can use your own tags.
  • XML schemas support a rich set of data types. The data types that XML schemas support include integers, Boolean, date, time, and so on. In addition, XML schemas support user-defined data types.
  • XML schemas can be inherited from an existing schema. Therefore, you can reuse an existing schema to customize it, as required.
  • XML schemas support namespaces. You can include a namespace in an XML schema so that you can validate an XML document against the elements or attributed defined in the namespace. You do not need to explicitly include all elements declared in an XML namespace in a schema.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

XML Topics