Elements and Attributes

Elements and Attributes

As shown in Figure 12-6(a), schemas consist of elements and attributes. Elements are either simple or complex. Simple elements have a single data item. In Figure 12-6(a), the elements LastName, FirstName, Nationality, DateOfBirth, and DateDeceased are all simple elements.

Complex elements contain other elements that can be either simple or complex. In Figure 12-6(a), the Artist element is complexType. It contains a sequence of five simple

Part 5 Database Access Standards

Figure 12-6

Use of an XML Schema

(a) XML Schema Document

(b) Graphical Representation of the XML Schema

(c) Schema-Valid XML Document

elements: LastName, FirstName, Nationality, DateOfBirth, and DateDeceased. Later you will see examples of complex types that contain other complex types.

Complex types can have attributes. Figure 12-6(a) defines the attribute ArtStyle. The creator of an XML document uses this attribute to specify a characteristic about an artist; in this case, his or her style. The example document in Figure 12-6(c) specifies the ArtStyle for this artist (Miro) as Modern.

Chapter 12 Database Processing with XML

Elements and attributes both carry data, and you may be wondering when to use one or the other. As a general rule, for database/XML applications,

use elements to store data and attributes to store metadata. For example, define an ItemPrice element to store the value of a price, and define a Currency attribute to store the currency type of that price, such as USD, Aus$, or Euros.

The XML standards do not require that elements and attributes be used in this way. It is a matter of style, and in subsequent sections we will show how it is possible to cause SQL Server to place all of the column values in attributes, to place all of them in elements, or to mix them up so that some columns are placed in attributes and others are placed in elements. Thus, these decisions are a matter of design choice rather than XML standard.

By default, the cardinality of both simple and complex elements is 1.1, meaning that a single value is required and no more than a single value can be specified. For the schema in Figure 12-6(a), the minOccurs = “0” expressions indicate that the defaults are being overridden for Birthdate and DeceasedDate so that they need not have a value. This is similar to the NULL constraint in SQL schema definitions.

Figure 12-6(b) shows the XML Schema in a diagram format as drawn by Altova’s XMLSpy XML editing tool (see www.altova.com/xml-editor/ ). Being able to see an XML Schema as a diagram often makes it easier to interpret exactly what the XML Schema is specifying. In this diagram, note that required elements (NOT NULL in SQL terms) are indicated with solid lines and box outlines, whereas optional elements (NULL in SQL terms) are indicated by dashed line and box outlines.

You can find out more about this excellent product from a small company, which is not yet owned by Microsoft or some other behemoth corporation,

from the Web site www.altova.com. A 30-day trial version is available. You can process all of the XML code in this chapter using XMLSpy.

Figure 12-6(c) shows an XML document that is valid on the schema shown in Figure 12-6(a). Observe that the value of the ArtStyle attribute is given with the heading of the Artist element. Also note that a namespace of xsi is defined. This namespace is used just once—for the noNamespaceSchemaLocation attribute. Do not be concerned about the name of this attri- bute; it is simply a means of telling the XML parser where to find the XML Schema for this document. Focus your attention on the relationship of the structure of the document and the description in the XML Schema.