Next page Previous page Start of chapter End of chapter

Global versus local descriptions

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:

In summary, global descriptions enhance readability and reusability of the schema, but local declarations are sometimes handy. We recommend the following best practices:

  1. use global descriptions as much as possible since they enhance readability of the resulting schema and promote reuse of schema fragments;
  2. use local declarations for elements and attributes that must be overloaded. Moreover, in case your XML application employs namespaces, local declarations for attributes allow you to get rid of the prefixes for attributes as soon as the attribute attributeFormDefault is set to unqualified (notice that this is the default);
  3. if your XML application employs namespaces and your schema uses local elements, set elementFormDefault to qualified to establish the normal behaviour for namespaces.
Next page Previous page Start of chapter End of chapter
Caffè XML - Massimo Franceschet