XML Schema is an XML-based language for defining validation rules (“schema”) in XML-formatted documents. It was the first XML schema language to achieve recommendation status from the W3C. This language is an alternative to the DTD, whose syntax is not based on the XML format.
It was widely used for the development of the Electronic Invoice.
A file containing definitions in XML Schema language is called XSD (XML Schema Definition), which describes the structure of an XML document.
In mid-1999, the W3C consortium (responsible for several WEB standards) first published the XML Schema. Being the first initiative to present an alternative to the DTD standard. After several revisions, in 2001, the final XML Schema recommendation was released and in 2004 there were some adaptations. Since then, several manufacturers have included this standard in their products. There are several APIs for validation, databases that support XML data types, as well as native databases that only work with XML and have XML Schema as their basic consistency control mechanism.
The validation of XML files can be done through a specific application or even some other adopted standard. The main standards currently are:
DTD (Document Type Definition)
XSD (XML Schema Definition)
XDR (XML Data Reduced)
When an XML document is well formed, it can be validated against rules that can be defined using the XML Schema language.
The use of DTDs was of great importance when the XML standard emerged. Because it was the standard for document validation of the predecessor language of XML (SGML) and was used as a form of validation also for XML. Although capable of validating XML files, the DTD has many limitations. There is no broad set of types (all data is interpreted as text), bringing unpleasant effects like validation, comparison, etc. They also don’t support namespaces, force elements to always appear in the specified order, etc. Leading to disuse.
The XSD (XML Schema Definition) or XS (XML Schema) standard has been the official W3C recommendation since 2001 for validation. This pattern manages to overcome the limitations of the DTD, in addition to providing several functionalities, it is possible to build own types derived from the basic types, perform relationships between data elements within XML (similar to relationships between tables), etc.
Structure of an XSD document
At its core it is an XML document. That is, it must obey the same rules as an XML document. An XSD document also has other needs that an XML document does not. In order to meet these needs, it is necessary to define the parts of an XSD document.
Declaration of elements
Elements are declared using the “element” tag. The main attributes of the tag are:
name: specifies the name of the element
type: specifies the element's data type
minOccurs: specifies the minimum number of times the element can appear
maxOccurs: specifies the maximum number of times the element can appear (the word unbounded can be used to specify an unlimited number).
The declaration below creates an element called “address” type string, which can appear at least zero times and at most one time:
In general, attribute declarations look a lot like element declarations. These declarations have some attributes. The main ones are:
name: specifies the attribute name
type: specifies the data type of the attribute
use: specifies the use of the attribute (required, optional or prohibited)
Ex.: declares two attributes. The first is of type date and is optional. The second is of type string and is required.