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.
Parts
» This page intentionally left blank
» Reporting and Data Mining Database Applications
» Database Applications and SQL
» Reading Specified Columns and Rows from a Single Table
» “Does Not Work with Microsoft Access ANSI-89 SQL”
» Processing SQL Statements in Microsoft Access 2010
» Using SQL in Microsoft SQL Server 2008 R2
» Using SQL in Oracle Database 11g
» Using SQL in Oracle MySQL 5.5
» Wildcards in SQL WHERE Clauses
» Using SQL Built-in Functions
» SQL Expressions in SQL SELECT Statements
» Querying Multiple Tables with Subqueries
» Querying Multiple Tables with Joins
» Comparing Subqueries and Joins
» Finding Functional Dependencies
» Eliminating Anomalies from Multivalued Dependencies
» The Multivalue, Multicolumn Problem
» The General-Purpose Remarks Column
» R Diagrams Using the IE Crow’s Foot Model
» The Multivalued Attribute Pattern
» The Archetype/Instance Pattern
» The Student Acceptance Letter
» X This is a warning, no further action is required.
» 1:1 Relationships Between Strong Entities
» M Relationships Between Strong Entities
» Relationships in Mixed Entity Designs
» Representing Ternary and Higher-Order Relationships
» Relational Representation of the Highline University Data Model
» Surrogate Key Database Design
» Column Properties for the View Ridge Database Design Tables
» Variations in SQL Data Types
» Implementing Data Constraints
» Populating the View Ridge Database Tables
» Using Triggers to Provide Default Values
» The WORK_AddWorkTransaction Stored Procedure
» • If a PROJECT row is deleted, then the project has been canceled, and it is unneces-
» Reducing Cardinalities (with Data Loss)
» Optimistic Versus Pessimistic Locking
» Declaring Lock Characteristics
» Processing Rights and Responsibilities
» Recovery via Rollback/Rollforward
» Maintaining the Data Repository
» Types of Distributed Databases
» • Express Edition. This free, feature-limited version is available for download. It
» SQL Server 2008 R2 SQL Statements and SQL Scripts
» Creating the View Ridge Database Table Structure
» Populating the VRG Tables with Data
» The Stored Procedure InsertCustomerAndInterests
» The Stored Procedure InsertCustomerWithTransaction
» A Trigger for Setting Default Values
» A Trigger for Enforcing a Data Constraint
» A Trigger for Enforcing a Required Child Constraint
» Creating an ODBC Data Source Name
» Materializing XML Documents with XSLT
» Using the SQL SELECT . . . FOR XML Statement
» Multitable SELECT with FOR XML
» A Schema with Two Multivalued Paths
» Problems with Operational Data
» Using SQL for Market Basket Analysis
Show more