WebReference.com - Part 2 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (1/6) | WebReference


current pageTo page 2To page 3To page 4To page 5To page 6

Professional XML Schemas

Element Declarations

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:

In the following example we can see a mix of the two approaches. The Address element declaration contains an anonymous type, while the child elements are all given a simple type of string:

<element name = "Address">
         <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" />

Here is an example of an Address element that conforms to this schema:

   <Street>10 Elizabeth Place</Street>

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:

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.

current pageTo page 2To page 3To page 4To page 5To page 6

Created: October 22, 2001
Revised: October 22, 2001
