There are two styles to write type and element descriptions in XML Schema: global and local. So far, we have seen schemas in global style.
In global style, all elements and attributes of the schema are declared as immediate children of the root schema element with a type attribute that contains the corresponding type name. Moreover, both user-defined simple and complex types are also defined globally as immediate children of the root schema element with an attribute name for the type name. In the type definitions, elements and attributes are referred to by using an element or attribute element with an attribute ref for the referred name. A typical example follows:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="author" type="xs:string"/> <xs:element name="title" type="xs:string"/> <xs:element name="year" type="xs:gYear"/> <xs:element name="item" type="itemType"/> <xs:attribute name="isbn" type="xs:string"/> <xs:complexType name="itemType"> <xs:sequence> <xs:element ref="author" maxOccurs="unbounded"/> <xs:element ref="title"/> <xs:element ref="year"/> </xs:sequence> <xs:attribute ref="isbn"/> </xs:complexType> </xs:schema>
In the local style (or inline style) of writing schemas, types are defined inside the corresponding element declaration. Moreover, these inlined type definitions contain element and attribute declarations (and not references) that are used to describe the type. The same example shown above is rewritten in local style in the following:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="item"> <xs:complexType> <xs:sequence> <xs:element name="author" type="xs:string" maxOccurs="unbounded"/> <xs:element name="title" type="xs:string"/> <xs:element name="year" type="xs:gYear"/> </xs:sequence> <xs:attribute name="isbn" type="xs:string"/> </xs:complexType> </xs:element> </xs:schema>
Notice that the element item is the only global description and has no type attribute. Its type is now anonymous and is defined inside the element declaration.
Which style to use is mainly a matter of personal preference. Normally, the style is neither fully global nor fully local, by it mixes global and local descriptions. However, there are some differences between the two styles:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.dimi.uniud.it/francesc/biblio" xmlns:b="http://www.dimi.uniud.it/francesc/biblio"> <xs:element name="author" type="xs:string"/> <xs:element name="title" type="xs:string"/> <xs:element name="year" type="xs:gYear"/> <xs:element name="item" type="b:itemType"/> <xs:attribute name="isbn" type="xs:string"/> <xs:complexType name="itemType"> <xs:sequence> <xs:element ref="b:author" maxOccurs="unbounded"/> <xs:element ref="b:title"/> <xs:element ref="b:year"/> </xs:sequence> <xs:attribute ref="b:isbn"/> </xs:complexType> </xs:schema>By associating the schema with a target namespace, every global description has been implicitly associated with that namespace (it is not necessary to explicitly qualify the names of global descriptions). This is the reason why a reference to a global description must be qualified with the target namespace prefix, like in the type attribute of the element item or in the ref attributes of the element references in the definition of type itemType.
<?xml version="1.0"?> <b:item xmlns:b="http://www.dimi.uniud.it/francesc/biblio" b:isbn="0-596-00764-7"> <b:author>Elliotte Harold</b:author> <b:title>XML in a nutshell</b:title> <b:year>2004</b:year> </b:item>The following schema introduces the following modifications: the author element and the isbn attribute have been inlined. Moreover, the attribute elementFormDefault has been added with value qualified:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.dimi.uniud.it/francesc/biblio" xmlns:b="http://www.dimi.uniud.it/francesc/biblio" elementFormDefault="qualified"> <xs:element name="title" type="xs:string"/> <xs:element name="year" type="xs:gYear"/> <xs:element name="item" type="b:itemType"/> <xs:complexType name="itemType"> <xs:sequence> <xs:element name="author" type="xs:string" maxOccurs="unbounded"/> <xs:element ref="b:title"/> <xs:element ref="b:year"/> </xs:sequence> <xs:attribute name="isbn" type="xs:string"/> </xs:complexType> </xs:schema>The following is a valid instance with respect to the modified schema. The locally declared element author is still qualified, as requested by the elementFormDefault attribute. However, the locally declared attribute isbn is not qualified, as requested by the default value of the attributeFormDefault attribute (unqualified):
<?xml version="1.0"?> <b:item xmlns:b="http://www.dimi.uniud.it/francesc/biblio" isbn="0-596-00764-7"> <b:author>Elliotte Harold</b:author> <b:title>XML in a nutshell</b:title> <b:year>2004</b:year> </b:item>
In summary, global descriptions enhance readability and reusability of the schema, but local declarations are sometimes handy. We recommend the following best practices: