WebReference.com - Part 2 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (1/6)
Professional XML Schemas
The declaration of an element involves associating a name with a type. Earlier, we saw
how to declare an element using an element called
element, and that its name is given as
the value of the
name attribute that the element declaration carries. The type meanwhile
would be a simple type if the element had text-only content, otherwise it would be a complex type. The
type of the element can be given in one of two ways:
A type definition can be anonymous, and nested inside the element declaration, as we saw with the child elements of
Customerin the first example.
A type can be referred to, by putting the name of the type as the value of a
typeattribute, as we have been doing with the value
In the following example we can see a mix of the two approaches. The
element declaration contains an anonymous type, while the child elements are all given a simple
<element name = "Address"> <complexType> <sequence> <element name = "Street" type = "string" /> <element name = "Town" type = "string" /> <element name = "City" type = "string" /> <element name = "StateProvinceCounty" type = "string" /> <element name = "Country" type = "string" /> <element name = "ZipPostCode" type = "string" /> </sequence> </complexType> </element>
Here is an example of an
Address element that conforms to this schema:
<Address> <Street>10 Elizabeth Place</Street> <Town>Paddington</Town> <City>Sydney</City> <StateProvinceCounty>NSW</StateProvinceCounty> <Country>Australia</Country> <ZipPostCode>2021</ZipPostCode> </Address>
If we do not specify a type, then the element can contain any mix of elements, attributes and text. This is known as the ur-type type in XML Schema, although you do not actually refer to it by name, it is just the default if you do not specify a type.
Global versus Local Element Declarations
It is important to distinguish between the global and local element declarations:
Global element declarations are children of the root
Local element declarations are nested further inside the schema structure and are not direct children of the root
Once elements have been declared globally, any other complex type can use that element declaration, by creating a reference to it. This is especially helpful when an element and its content model are used in other element declarations and complex type definitions, as they enable us to re-use the content model (A content model simply refers to anything within an element declaration that affects the structure of the element in the instance document. This could be attributes or other elements within an element).
You should be aware that, if your instance documents make use of namespaces, there are greater differences between local and global element declarations. This is because when you use namespaces, globally declared elements must be explicitly qualified in the instance document, whereas local declarations should not always be qualified. We look into the issues that this introduces and the ways in which it might affect how you write XML Schemas in Chapter 6.
Created: October 22, 2001
Revised: October 22, 2001