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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 16: A Brief XML Primer<br />

The contents of the Note elements, such as “The customer called … ” are neither an element nor an<br />

attribute, yet they are valid XML data.<br />

Be aware that such data exists in XML, but <strong>SQL</strong> <strong>Server</strong> will not output data in this format using any of<br />

the automatic styling methods. The row/column approach of RDBMS systems lends itself far better to<br />

elements and attributes. To output data such as our notes, you would need to use some of the methods<br />

that allow for more explicit output formats of your XML (and these can be non-intuitive at best) or perform<br />

a transformation on the output after you select it. We will look at transformations as the last item in<br />

this chapter.<br />

Being Valid vs. Being Well Formed — Schemas and DTDs<br />

Just because an XML document is well formed does not mean that it is valid XML. Now, while that is<br />

sinking in on you I’ll tell you that no XML is considered “valid” unless it has been validated against<br />

some form of specification document. Currently, there are only two recognized types of specification<br />

documents — a Document Type Definition, or DTD, and an XML schema.<br />

The basic premise behind both varieties of validation documents is much the same. While XML as a language<br />

defines the most basic rules that a XML document must comply with, DTDs and XML schemas<br />

seek to define what the rules are for a particular class of XML document. The two approaches are implemented<br />

somewhat differently and each offers distinct advantages over the other:<br />

❑ DTDs: This is the old tried and true. DTDs are utilized in SGML (XML is an SGML application<br />

— you can think of SGML being a superset of XML, but incredibly painful to learn), and<br />

have the advantage of being a very well-known and accepted way of doing things. There are<br />

tons of DTDs already out there that are just waiting for you to utilize them.<br />

The downside (you knew there had to be one — right?) is that the “old” is operative in my “old<br />

tried and true” statement. Not that being old is a bad thing, but in this case, DTDs are definitely<br />

not up to speed with what else has happened in document technology. DTDs do not really allow<br />

for such seemingly rudimentary things as restricting data types. You’ll find that DTDs — at least<br />

in terms of being used with XML — are largely being treated as deprecated at this point in favor<br />

of XML schemas.<br />

❑ XML schemas: XML schemas have the distinct advantage of being strongly typed. What’s cool<br />

about them is that you can effectively establish your own complex data types — types that are<br />

made up based on combinations of one or more other data types (including other complex data<br />

types) or require specialized pattern matching (for example, a Social Security number is just a<br />

number, but it has special formatting that you could easily enforce via an XML schema). XML<br />

schemas also have the advantage, as their name suggests, of being an XML document themselves.<br />

This means that a lot of the skills in writing your XML documents also apply to writing<br />

schemas (though there’s still plenty to learn) and that schemas can, themselves, be self-describing<br />

— right down to validating themselves against yet another schema.<br />

What <strong>SQL</strong> Ser ver Brings to the P arty<br />

486<br />

So, now we have all the basics of what XML is down. What we need is to understand the relevance in<br />

<strong>SQL</strong> <strong>Server</strong>.

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

Saved successfully!

Ooh no, something went wrong!