17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

If we have more than one customer, that’s not a problem — we just add another customer node:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Indeed, this can go to unlimited levels of hierarchy (subject, of course, to whatever your parser can handle).<br />

We could, for example, add a level for individual line items in the order.<br />

Determining Elements vs. Attributes<br />

Chapter 16: A Brief XML Primer<br />

The first thing to understand here is that there is no hard and fast rule for determining what should be<br />

an element vs. an attribute. An attribute describes the properties of the element that it is an attribute of.<br />

Child elements — or child “nodes” — of an element do much the same thing. So how, then, do we decide<br />

which should be which? Why are attributes even necessary? Well, like most things in life, it’s something<br />

of a balancing act.<br />

Attributes make a lot of sense in situations where the value is a one-to-one relationship with, and is<br />

inherently part of, the element. In AdventureWorks<strong>2008</strong>, for example, we have only one customer number<br />

per customer ID — this is ideal for an attribute. As we are transforming our relational data to XML,<br />

the columns of a table will often make good attributes to an element directly related to individual rows<br />

of a table.<br />

Elements tend to make more sense if there is more of a one-to-many relationship between the element<br />

and what’s describing it. In our example earlier in the chapter, there are many sales orders for each customer.<br />

<strong>Tech</strong>nically speaking, we could have had each order be an attribute of a customer element, but<br />

then we would have needed to repeat much of the customer element information over and over again.<br />

Similarly, if our AdventureWorks<strong>2008</strong> database allowed for the notion of customers having aliases (multiple<br />

account numbers — similar to how they have multiple contacts), then we may have wanted to have an<br />

AccountNumber element under the customer and have its attribute describe individual instances of names.<br />

Whichever way you go here, stick to one rule I’ve emphasized many times throughout the book — be<br />

consistent. Once something of a given nature is defined as being an attribute in one place, lean toward<br />

keeping it an attribute in other places unless its nature is somehow different in the new place you’re<br />

using it. One more time: Be consistent.<br />

483

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!