Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Chapter 16: A Brief XML Primer Namespaces 484 With all this freedom to create your own tags, to mix and match data from other sources, and just otherwise do your own thing, there are bound to be a few collisions here and there about what things are going to use what names in which places. For example, an element with a name of “letter” might have entirely different structure, rules, and meaning to an application built for libraries (for whom a letter is probably a document written from one person to another person) than it would to another application, say one describing fonts (which might describe the association between a character set and a series of glyphs). To take this example a little further, the nature of XML is such that industry organizations around the world are slowly agreeing on naming and structure conventions to describe various types of information in their industry. Library organizations may have agreed on element formats describing books, plays, movies, letters, essays, and so on. At the same time, the operating systems and/or graphics industries may have agreed on element formats describing pictures, fonts, and document layouts. Now, imagine that we, the poor hapless developers that we are, have been asked to write an application that needs to render library content. Obviously, library content makes frequent use of things like fonts — so, when you refer to something called “letter” in your XML, are you referring to something to do with the font or is it a letter from a person to another person (say, from Thomas Jefferson to George Washington)? We have a conflict, and we need a way to resolve it. That’s where namespaces come in. Namespaces describe a domain of elements and attributes and what their structure is. The structure that supports letters in libraries would be described in a libraries namespace. Likewise, the graphics industry would likely have their own namespace(s) that would describe letters as they relate to that industry. The information for a namespace is stored in a reference document, and can be found using a Uniform Resource Identifier (URI) — a special name, not dissimilar from a URL — that will eventually resolve to our reference document. When we build our XML documents that refer to both library and graphics constructs, then we simply reference the namespaces for those industries. In addition, we qualify elements and attributes whose nature we want the namespace to describe. By qualifying our names using namespaces, we make sure that, even if a document has elements that are structurally different but have the same name, we can refer to the parts of our document with complete confidence that we are not referring to the wrong kind of element or attribute. To reference a namespace to the entire document, we simply add the reference as a special attribute (called xmlns) to our root. The reference will provide both a local name (how we want to refer to the namespace) and the URI that will eventually resolve to our reference document. We can also add namespace references (again, using xmlns) to other nodes in the document if we want to apply only that particular namespace within the scope of the node we assign the namespace, too. What follows is an example of an XML document (technically, this is what we call a schema) that we will be utilizing later in the chapter. Notice several things about it as relates to namespaces: The document references three namespaces — one each for XDR (this happens to be an XDR document), a Microsoft data type namespace (this one builds a list about the number and nature of different data types), and, last, but not least, a special SQL namespace used for working with SQL Server XML integration. Some attributes (including one in the root) are qualified with namespace information (see the sql:relation attribute, for example).
The sql data type references a couple of special attributes. We do not have to worry about whether the Microsoft data types namespace also has a field or relation data type because we are fully qualifying our attribute names. Even if the data types namespace does have an attribute called field, our XML parser will still know to treat this element by the rules of the sql namespace. Element Content Chapter 16: A Brief XML Primer Another notion of XML and elements that deserves mention (which is close to all it will get here) is the concept of element content. Elements can contain data beyond the attribute level and nested elements. While nested elements are certainly one form of element content (one element contains the other), XML also allows for row text information to be contained in an element. For example, we could have an XML document that looked something like the following: The customer called in today and placed another order. Says they really like our work and would like it if we would consider establishing a location closer to their base of operations. Followed up with the customer on new location. Customer agrees to guarantee us $5,000 per month in business to help support a new office. 485
- Page 471 and 472: Transaction 4 This transaction wasn
- Page 473 and 474: Oops — problem!!! Transaction 2 h
- Page 475 and 476: The only cure for this is setting y
- Page 477 and 478: Exclusive Locks Exclusive locks are
- Page 479 and 480: Also: ❑ The Sch-S is compatible w
- Page 481 and 482: The syntax for switching between th
- Page 483 and 484: As with most things in life, howeve
- Page 485 and 486: purchased. Process 2 records sales;
- Page 487: Chapter 14: Transactions and Locks
- Page 490 and 491: Chapter 15: Triggers the world’s
- Page 492 and 493: Chapter 15: Triggers WITH ENCRYPTIO
- Page 494 and 495: Chapter 15: Triggers FOR|AFTER The
- Page 496 and 497: Chapter 15: Triggers 458 To illustr
- Page 498 and 499: Chapter 15: Triggers 460 IF EXISTS
- Page 500 and 501: Chapter 15: Triggers ❑ Feeding de
- Page 502 and 503: Chapter 15: Triggers Trigger Firing
- Page 504 and 505: Chapter 15: Triggers Like regular t
- Page 506 and 507: Chapter 15: Triggers The COLUMNS_UP
- Page 508 and 509: Chapter 15: Triggers This is the sa
- Page 510 and 511: Chapter 15: Triggers 472 we have th
- Page 512 and 513: Chapter 16: A Brief XML Primer So,
- Page 514 and 515: Chapter 16: A Brief XML Primer Figu
- Page 516 and 517: Chapter 16: A Brief XML Primer Elem
- Page 518 and 519: Chapter 16: A Brief XML Primer ❑
- Page 520 and 521: Chapter 16: A Brief XML Primer 482
- Page 524 and 525: Chapter 16: A Brief XML Primer The
- Page 526 and 527: Chapter 16: A Brief XML Primer 2. W
- Page 528 and 529: Chapter 16: A Brief XML Primer SQL
- Page 530 and 531: Chapter 16: A Brief XML Primer So,
- Page 532 and 533: Chapter 16: A Brief XML Primer If,
- Page 534 and 535: Chapter 16: A Brief XML Primer 496
- Page 536 and 537: Chapter 16: A Brief XML Primer Note
- Page 538 and 539: Chapter 16: A Brief XML Primer RAW
- Page 540 and 541: Chapter 16: A Brief XML Primer AUTO
- Page 542 and 543: Chapter 16: A Brief XML Primer EXPL
- Page 544 and 545: Chapter 16: A Brief XML Primer Chec
- Page 546 and 547: Chapter 16: A Brief XML Primer 508
- Page 548 and 549: Chapter 16: A Brief XML Primer 510
- Page 550 and 551: Chapter 16: A Brief XML Primer 512
- Page 552 and 553: Chapter 16: A Brief XML Primer The
- Page 554 and 555: Chapter 16: A Brief XML Primer 516
- Page 556 and 557: Chapter 17: Reporting for Duty, Sir
- Page 558 and 559: Chapter 17: Reporting for Duty, Sir
- Page 560 and 561: Chapter 17: Reporting for Duty, Sir
- Page 562 and 563: Chapter 17: Reporting for Duty, Sir
- Page 564 and 565: Chapter 17: Reporting for Duty, Sir
- Page 566 and 567: Chapter 17: Reporting for Duty, Sir
- Page 568 and 569: Chapter 17: Reporting for Duty, Sir
- Page 570 and 571: Chapter 17: Reporting for Duty, Sir
Chapter 16: A Brief XML Primer<br />
Namespaces<br />
484<br />
With all this freedom to create your own tags, to mix and match data from other sources, and just otherwise<br />
do your own thing, there are bound to be a few collisions here and there about what things are going<br />
to use what names in which places. For example, an element with a name of “letter” might have entirely<br />
different structure, rules, and meaning to an application built for libraries (for whom a letter is probably a<br />
document written from one person to another person) than it would to another application, say one<br />
describing fonts (which might describe the association between a character set and a series of glyphs).<br />
To take this example a little further, the nature of XML is such that industry organizations around the<br />
world are slowly agreeing on naming and structure conventions to describe various types of information<br />
in their industry. Library organizations may have agreed on element formats describing books, plays,<br />
movies, letters, essays, and so on. At the same time, the operating systems and/or graphics industries<br />
may have agreed on element formats describing pictures, fonts, and document layouts.<br />
Now, imagine that we, the poor hapless developers that we are, have been asked to write an application<br />
that needs to render library content. Obviously, library content makes frequent use of things like fonts —<br />
so, when you refer to something called “letter” in your XML, are you referring to something to do with<br />
the font or is it a letter from a person to another person (say, from Thomas Jefferson to George Washington)?<br />
We have a conflict, and we need a way to resolve it.<br />
That’s where namespaces come in. Namespaces describe a domain of elements and attributes and what<br />
their structure is. The structure that supports letters in libraries would be described in a libraries namespace.<br />
Likewise, the graphics industry would likely have their own namespace(s) that would describe<br />
letters as they relate to that industry. The information for a namespace is stored in a reference document,<br />
and can be found using a Uniform Resource Identifier (URI) — a special name, not dissimilar from a<br />
URL — that will eventually resolve to our reference document.<br />
When we build our XML documents that refer to both library and graphics constructs, then we simply<br />
reference the namespaces for those industries. In addition, we qualify elements and attributes whose<br />
nature we want the namespace to describe. By qualifying our names using namespaces, we make sure<br />
that, even if a document has elements that are structurally different but have the same name, we can<br />
refer to the parts of our document with complete confidence that we are not referring to the wrong kind<br />
of element or attribute.<br />
To reference a namespace to the entire document, we simply add the reference as a special attribute<br />
(called xmlns) to our root. The reference will provide both a local name (how we want to refer to the<br />
namespace) and the URI that will eventually resolve to our reference document. We can also add namespace<br />
references (again, using xmlns) to other nodes in the document if we want to apply only that particular<br />
namespace within the scope of the node we assign the namespace, too.<br />
What follows is an example of an XML document (technically, this is what we call a schema) that we will<br />
be utilizing later in the chapter. Notice several things about it as relates to namespaces:<br />
The document references three namespaces — one each for XDR (this happens to be an XDR document),<br />
a <strong>Microsoft</strong> data type namespace (this one builds a list about the number and nature of different data types),<br />
and, last, but not least, a special <strong>SQL</strong> namespace used for working with <strong>SQL</strong> <strong>Server</strong> XML integration.<br />
Some attributes (including one in the root) are qualified with namespace information (see the<br />
sql:relation attribute, for example).