WebReference.com - Part 2 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (5/6)
Professional XML Schemas
We declare attributes in a similar way to declaring elements. The key differences are:
They cannot contain any child information items. Attribute values are always simple types.
They are unordered; we cannot specify the order in which attributes should appear on a parent element.
This means that the value of the
type attribute on an attribute declaration
is always a simple type Â a restriction upon the value of the attribute. If we do not specify a type,
then by default it is the simple version of the ur-type definition, whose name is anySimpleType.
This represents any legal character string in XML that matches the
Char production in the
XML 1.0 Recommendation, but we need to be aware that if we need to use characters such as angled brackets
([ ]) or an ampersand (&), these should be escaped using the escape characters or numeric character
references defined in the XML 1.0 Recommendation.
Attributes are added to an element inside the complex type definition for that element; they are added after the content of the element is defined within the complex type:
<?xml version = "1.0" ?> <schema> <element name = "Customer"> <complexType> <sequence> <element name = "FirstName" type = "string" /> <element name = "MiddleInitial" type = "string" /> <element name = "LastName" type = "string" /> </sequence> <attribute name = "customerID" type = "integer" /> </complexType> </element> </schema>
Here we can see that we have added the
customerID attribute to the
Customer element by including its declaration at the end of the complex type.
Global versus Local Attribute Declarations
As with element declarations, attribute declarations can either be local or global. If they are global declarations they are direct children of the schema element, meaning that any complex type definition can make use of the attribute.
As with global and local element declarations, you should be aware that, if your instance documents make use of namespaces, there are greater differences between local and global attribute declarations. This is because globally declared attributes 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.
Occurrence of Attributes
By default, when we declare an element to carry an attribute, its presence in an
instance document is optional. While there is no provision for
maxOccurs attributes on our attribute declarations, because an attribute can only appear
once on any given element, we might want to specify that an attribute must appear on a given
If we want to indicate that an attribute's presence is required, or explicitly state that
an attribute is optional, we can add an attribute called
use to the attribute declaration,
which can take one of the following values:
requiredwhen indicating that an attribute must appear
optionalwhen it can either appear once or not at all (the default value)
prohibitedwhen we want to explicitly indicate that it must not appear
Note that we cannot add the
useattribute to globally declared attributes.
For example, if we just want to ensure that an attribute is present on the element,
we can just add the
use attribute to the attribute declaration with a value of
<attribute name="dateReceived" use="required" />
If the attribute is optional, we can use the value of optional, although this is not required as it is the default value:
<attribute name="child" use="optional" />
Created: October 22, 2001
Revised: October 22, 2001