The Root Element
The root element of a W3C XML Schema is “schema”. It defines the XML Schema namespace. The schema element is the only element in the XML Schema namespace. All other elements in the XML Schema namespace are derived from the schema element.
The XML Schema Definition
XML Schema: The W3C Standard for XML Data Integration
From the W3C XML Schema Definition page:
The World Wide Web Consortium’s XML Schema Definition Language (W3C XML Schema) is a language for describing the structure and constraining the contents of XML documents. By using W3C XML Schema, Web authors and application developers can easily describe which elements are allowed in a document, in what order these elements must appear, which attributes are associated with each element, and so on.
The XML Schema
The XML Schema language is also referred to as XML Schema Definition (XSD). It was published as a W3C Recommendation on and a second edition was published on .
XML Schema provides a mechanism to formally describe the structure of an XML document. An XML schema can be used to define:
- The structure of an XML document: defining which elements and attributes are allowed, in what order, and with what kind of data.
- The data typing of elements and attributes: for example, defining whether an element should contain a date or a string.
- Default and fixed values for elements and attributes.
- Element and attribute groups: for example, to describe repeating structures.
- Alternative structures using conditional type assignment: defining choices between different element types.
- Derivation by extension or restriction: allowing complex types to be defined by adding or removing restrictions from simpler base types.
XML Schema is built in such a way that allows for the evolution of an XML document over time, making it well suited for the purpose of describing structured data that might need to change in the future.
The Root Element’s Attributes
The root element of a W3C XML Schema has three attributes: xmlns, xs, and xmlns:xs. These three attributes are used to define the namespace of the XML Schema. The xmlns attribute is used to define the XML namespace. The xs attribute is used to define the XML Schema namespace. The xmlns:xs attribute is used to define the XML Schema namespace prefix.
The xs:schema Attribute
There are several attributes that may be specified on the xs:schema element, including:
- targetNamespace – Specifies the target namespace of the schema. This is the namespace to which elements and attributes in the schema will belong.
- elementFormDefault – Specifies whether elements in the schema will be qualified (i.e. have a namespace prefix) or unqualified (i.e. will not have a namespace prefix). The default value is “unqualified”.
- attributeFormDefault – Specifies whether attributes in the schema will be qualified (i.e. have a namespace prefix) or unqualified (i.e. will not have a namespace prefix). The default value is “unqualified”.
- xmlns:xsi – Specifies the XML Schema instance namespace. This is used for attributes in the schema that are in the XML Schema namespace, such as xsi:schemaLocation .
- xsi:schemaLocation – Specifies a list of URIs for schemas that are associated with this schema
The xmlns:xs Attribute
xmlns:xs is the namespace declaration for XML Schema. It is used to qualify XML Schema elements and attributes.
The targetNamespace Attribute
The targetNamespace attribute is used to specify the namespace of the XML schema. By default, the targetNamespace is the empty string.
The elementFormDefault Attribute
The elementFormDefault attribute is used to specify whether element declarations in the schema will be treated as qualified or unqualified. The default value is unqualified, which means that elements will not be namespace-qualified by default. This can be changed to qualified, in which case all elements will be namespace-qualified by default unless they explicitly set the form attribute to unqualified.
The attributeFormDefault Attribute
The attributeFormDefault attribute is used to indicate the default value for the form attribute on attributes. If not specified, the default value is unqualified (that is, the attribute is not namespace-qualified).
The version Attribute
The root element (html) has a version attribute. The value of this attribute must be a string. This string must be an XML version number, such as “1.0”.
If the attribute is specified, its value must match one of the following:
The finalDefault Attribute
The finalDefault attribute is used to make an element non-extendable. It can be set on the root element of a schema to prevent it from being extended by any other schema. This is useful if you want to ensure that your schema can only be used as-is, without modification.
The blockDefault Attribute
The blockDefault attribute, on the root element coffee, defines the default behavior for whether child elements should be displayed as block or inline elements. If not specified, the default value is “block”.
The id Attribute
The id attribute must be unique within the document. That is, no two elements can have the same id value.
The Root Element’s Sub-Elements
The annotation Element
The annotation element is a sub-element of the root element. It is used to store annotations, or comments, about the document. Annotations are not part of the document itself and do not affect its meaning or interpretation.
The include Element
The element allows schemas to be combined. An included schema must be valid. The inclusion happens before any other compilation steps, so it is not possible to reference components in the included schema from other schemas in the set.
The element has two attributes:
-schemaLocation: a URI reference that indicates the location of the schema to be included.
-namespace: a URI that indicates the target namespace of the included schema. If omitted, it defaults to the namespace of the including schema.
The import Element
The import element allows you to import other schema documents into your schema. This is useful if you want to split up your schema into multiple files, or if you want to reuse parts of a schema in multiple schema documents.
The import element has two attributes:
-namespace: The namespace of the schema document being imported. This must be specified.
-location: The location (URL) of the schema document being imported. This is optional; if it is not specified, the namespace attribute will be used to determine the location of the document.
Here is an example of how to use the import element:
The redefine Element
The redefine element is used to change the definitions of components from a different namespace. This allows you to customize the components without having to create new ones from scratch. The redefine element must have a schemaLocation attribute that specifies the location of the schema file that contains the component definitions that you want to change.
The alternative Element
The alternative element is a child of the root element. It encompasses a list of choices from which only one must be selected. The maxOccurs attribute of the element allows you to specify how many times you can select an element from the list of choices (the default is 1).
The sequence Element
The sequence element contains a list of elements that must appear in a specific order. The order in which the elements appear is important. The maxOccurs attribute of the element allows you to specify how many times you can select an element from the list of choices (the default is 1).
The choice Element
The choice element contains a list of elements from which only one must be selected. The order in which the elements appear is not important. The maxOccurs attribute of the choice element specifies how many times you can select an element from the list of choices (the default is 1).